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.
#include <iostream>
#include <vector>
#include <iterator>
usingnamespace 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 )
returnfalse;
++backward;
++forward;
} // end while loop
returntrue;
} // 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'
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.
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 );
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.