char cPlay = 'y';
while(cPlay == 'y')
...
...
cout << "\nWould you like to play again?\nI will give you an option to pick
<< another level of difficulty(y/n)" <<endl;
cin >> cPlay;
cPlay = tolower(cPlay);
while(cPlay != 'y' && cPlay != 'n')
{
cout << "Unexpected input!\ny/Y = Keep playing\nn/N = Exit";
cin >> cPlay;
cPlay = tolower(cPlay);
}
}
cout << "I hope you enjoyed the first game i ever created =]" << endl;
system("PAUSE");
return 0;
So the problems are: If a user inputs "yes" the game turns into a wild, continuous loop.
If the user inputs "hello", The error pops up 5 times in a row.
I tried using cin >> setw(1) >> cPlay, but that didnt work.
Tried using cin.clear() / cin.ignore() for the hello problem, and couldnt get it to come out right.
So im wondering how i can get by these problems. i only want the error message to pop up once, no matter what the person answers. And if the user types in "yes" i want it to goto the error message. If i just cut off the "es" in "yes", then that would mean im continuing the program even if the user wrote "You Stink"
int main()
{
while(cPlay == 'y')
...
...
cout << "\nWould you like to play again?\nI will give you an option to
<< pick another level of difficulty(y/n)" <<endl;
char szTest[2];
cin.ignore(1,'\n');
cin.getline(szTest, 512);
while(szTest[1] != '\0')
{
ugh:
cout << "Unexpected input!\ny/Y = Keep playing\nn/N = Exit"<<endl;
cin.getline(szTest, 512);
}
cPlay = szTest[0];
cPlay = tolower(cPlay);
if(cPlay != 'y' && cPlay != 'n')
{
goto ugh;
}
}
cout << "I hope you enjoyed the first game i ever created =]" << endl;
cout << "Press ENTER to exit...."
cin.ignore();
return 0;
}
Thank you for the cin.ignore() idea instead of system("PAUSE"), i like it much better ^_^
OH, and i tried out your getline(cin,szString)
Didnt work at all, wondering if it only works with a string variable?