Palindrome program not giving proper output

I did write this program for homework, but I am stuck, and am just looking for some advice or direction. When I enter a string of integers, say 1 2 3 2 1, my output is 50 51 49 50 etc... and when I enter a string of characters it always cuts the first character out. Any advice, or hint as to where my problem lies would be greatly appreciated. Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

// initiating function template 'palindrome' and function template 'printVector'
template< typename T > bool palindrome( const vector< T > &vec );
template< typename P > void printVector( const vector< P > &vec);

void ChoiceIntegers(); // initiate Integers function
void ChoiceCharacters(); // initiate Characters function

int main() // function main begins program execution
{
	int choice = 1; // declare variable 'choice' as an integer and initialize it to 1

	// program introduction message to the user
	cout << "****************************************\n";
	cout << "*                                      *\n";
	cout << "*  Welcome to the Palindrome Program!  *\n";
	cout << "*                                      *\n";
	cout << "****************************************\n";

	cout << endl << endl; // additional spacing

	while( choice != 3 ) // while loop begins for main menu options
	{
		// main menu options requesting the user to enter a number between one and three
		cout << "\n\nPlease choose from the following menu options (1-3):\n\n";
		cout << "1) Enter a string of integers.\n";
		cout << "2) Enter a string of characters.\n";
		cout << "3) Exit the program.\n";

		cin >> choice; // user input

		if( choice < 1 || choice > 3 ) // if user enters something besides a 1, 2, or a 3...
		{
			// display error message to the user and...
			cout << "\n\nThat is not a number between 1 and 3... Please try again/n/n";
			continue; // return to the main menu

		} // end if

		if( choice == 1 ) // if user inputs a 1...
			ChoiceIntegers(); // call to function ChoiceIntegers
		if( choice == 2 ) // if user inputs a 2...
			ChoiceCharacters(); // call to function ChoiceCharacters
		if( choice == 3 ) // if user inputs a 3...
			break; // ends program

	} // end while loop for the main menu

} // end function main

void ChoiceIntegers() // function ChoiceIntegers
{
	vector< int > integers; // create a vector of integers
	int i; // declare variable 'i' as an integer

	char answer = 'Y'; // declare variable 'answer' as a character and initialize it to Y

	while( answer == 'Y' || answer == 'y' ) // while loop for answer
	{
		cout << "\nPlease enter a string of integers: "; // ask user of a string of integers
		cin >> i; // user input

		while( ( i = static_cast< int > ( cin.get() ) ) != '\n' ) // while loop for input
		{
			if( i != ' ' ) // if user input is not an empty space...
				integers.push_back( i ); // push the integer into the vector of integers

		} // end while loop for input

		printVector( integers ); // call to template function printVector for vector of integers
		// outputs whether or not our vector of integers is a palindrome
		cout << ( palindrome( integers ) ? " is " : " is not " ) << "a palindrome.\n\n";

		// ask user if they would like to enter another string of integers
		cout << "Would you like to enter another string of integers(y/n)?: ";
		cin >> answer; // user input

		if( answer == 'N' || answer == 'n' ) // if user inputs no...
			break; // break out of function ChoiceIntegers

	} // end while loop for answer

} // end function ChoiceIntegers

void ChoiceCharacters() // function ChoiceCharacters
{
	vector< char > characters; // create a vector of characters
	char c; // declare variable 'c' as a character

	char answer = 'Y'; // declare variable 'answer' as a character and initialize it to Y

	while( answer == 'Y' || answer == 'y' ) // while loop for answer
	{
		cout << "\nPlease enter a string of characters: "; // ask user of a string of integers
		cin >> c; // user input

		while( ( c = static_cast< char > ( cin.get() ) ) != '\n' ) // while loop for input
		{
			if( c != ' ' ) // if user input is not an empty space...
				characters.push_back( static_cast< char > ( c ) ); // push the character into the 
																   // vector of characters

		} // end while loop for input

		printVector( characters ); // call to template function printVector for vector of characters
		// output whether or not our vector of characters is a plaindrome
		cout << ( palindrome( characters ) ? " is " : " is not " ) << "a palindrome.\n\n";

		// ask user if they would like to enter another string of characters
		cout << "Would you like to enter another string of characters(y/n)?: ";
		cin >> answer; // user input

		if( answer == 'N' || answer == 'n' ) // if user inputs no...
			break; // break out of function ChoiceCharacters

	} // end while loop for answer

} // end function ChoiceCharacters

template< typename T >
bool palindrome( const vector< T > &vec ) // function template 'palindrome'
{
	vector< T > :: const_reverse_iterator backward = vec.rbegin();
	vector< T > :: const_iterator forward = vec.begin();

	while( backward != vec.rend() && forward != vec.end() )
	{
		if( *backward != *forward )
			return false;

		++backward;
		++forward;

	} // end while loop

	return true;

} // end function template 'palindrome'

template< typename P >
void printVector( const vector< P > &vec ) // function template 'printVector'
{
	vector< P > :: const_iterator forward;

	for( forward = vec.begin(); forward != vec.end(); ++forward )
		cout << *forward << ' ';

} // end function template 'printVector' 
Last edited on
cin >> i; // user input Here you read the first int or char you entered and you don't do anything with it
(That's why the first character is always missing). Just remove the line.

and

while( ( i = static_cast< int > ( cin.get() ) ) != '\n' )
This line is the reason your integeres are not "1 2 3 2 1" but 50 51.. and so on.

the follwing:http://www.cplusplus.com/forum/beginner/119677/
my second comment I explained what happens when you cast for a example a
'1' (char) to an int

edit: I know it compiles anyway but your function printVector and the other one are template functions. You should call them by passing a datatype for "P" or "T"
printVector<int>( integers );

Have fun
Last edited on
Wow! I got rid of the cin line, and read your second post from the link you posted, and now my program works. Thank you so much. It's crazy how something so simple can cause such a headache.
Topic archived. No new replies allowed.