Just starting C++ and programming.Writing a game program as follows
char letter;
cin >> letter;
I have a while loop that allows only e for exit and p for play which works fine. If user enters a, n, and so on they get a cout<< " You must enter e or p."; How ever my program goes whack if they enter jjjj or any string with more than one character. I have tried setw and such to no avail. How can I make my program only except only one character and disregard multiple key strokes?
Here is my code, I tried your suggestion but if I entered rr or dhfu or anything except a single character the game starts playing itself over and over again at light speed. What I need is a way to only accept one key stroke if user enters more than one. Thanks
char letter;// To hold cin value.
while (letter != 'e')//while loop.
{
cout << " This is a dice game that you play with the computer as your opponent.\n";
cout<< " The highest total resulting from a roll of two dice wins the game!!\n\n";
cout << " Press r to roll the dice or e to exit the game, then press enter. ";
cin >>letter;
srand((unsigned)time(0)); //Initialize random number generator (not sure if I am using this correctly).
if (letter == 'e')// An if statement, player enters e the program terminates.
break;
if (letter != 'r')/* An if Statement, player enters anything except p a heckling message
is displayed and computer beeps 3 times. Player enters p the program continues*/
i tried it nothing i think that this "while (letter != 'e')//while loop." is wrong you could put "while (letter==e) //exit'//while loop."why dont use this i think