Menu Error Handling!

closed account (E8AXSL3A)
For my program I pose a question, "Would you like to play a game?," before the program presents the menu options of the types of games to play. I am trying to add error handling to the yes or no question. I suppose that my overall problem/question is how do you clear a cin>> to re-input the menu data to go on to the next step of the game options? Any feedback is welcomed!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	//User Chooses To Play Or Not To Play
	cout << "Would You Like To Play A Game? (Y/N): " << endl;
	cin >> playGame;
	playGame = (toupper(playGame));

	if (playGame == 'Y')
	{
		cout << "Fantastic! What Would You Like To Play Today?\n  P for PIG\n  B for BLACKJACK\n\n  E to EXIT\n" << endl;
		cin >> gameChoice;
		gameChoice = (toupper(gameChoice));
	}
	if (playGame == 'N')
	{
		cout << "We Can Play Another Day... Goodbye!" << endl;
		system("pause");
		return 0;
	}
	if (playGame != 'Y' && playGame != 'N')
	{
		cout << "USER INPUT ERROR. . . Please Re-Enter Answer: ";
		cin.ignore();
		cin >> playGame;
		playGame = (toupper(playGame));
	}
1
2
3
4
5
6
7
playGame = toupper(playGame);
while(playGame != 'Y' || playGame != 'N')
{
  cout << "Error! Type a valid response.." << endl;
  cout << "Try again!" << endl;
  cout << "Would You Like To Play A Game? (Y/N): "; cin >> playGame;
}
Topic archived. No new replies allowed.