Loop Problem [Help]

I'm building a text based video game. The way the user interacts with the game is through menus, and entering a integer to select an option in the menus. If an invalid character is entered(i.e. a string, or a char that isn't 1-5), if entered the program asks the user to reenter their input, until their input is valid. I can't figure out why it's looping 5 times the user inputs a bad value. Heres a snippit of my code, and output/input.

[edit] took out code, it was too long and I reposted it.
Last edited on
return input > '0' && input < '6';


*cough cough*

char menuChoice = '0';


*cough cough*

...


*cough cough* Integers? *cough cough*

That and you forget to clear(reset it to defult value) your char.



Last edited on
I had to make that variable a char because if it's a integer when cin >> menuChoice happens, then if the user inputs a string or a char, the program will freakout and not allow the user to reenter a integer value for menuChoice. I made it a char to avoid this problem, and the user that still enter integers...

Ok so i figured out that the number of times the error message is printed is number of char's in whatever the user types in. Here's an example of the output. Also I changed the way isValidInput is.
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
bool Menu::isValidInput(char input)
   {
      bool valid = false;
      if(input == '1')
         valid = true;
      else if(input == '2')
         valid = true;
      else if(input == '3')
         valid = true;
      else if(input == '4')
         valid = true;
      else if(input == '5')
         valid = true;
      else
         cout << "\nInvalid Input, please reenter input: ";
      return valid;
   }
   char Menu::gameMenu()
   {
      cout << "1) Move (takes time, could be risky...) " << endl;
      cout << "2) Read technical papers (boost intelligence, takes time) " << endl;
      cout << "3) Search for loose change (boost money, takes time) " << endl;
      cout << "4) View Character " << endl;
      cout << "5) Quit the game " << endl;
      cout << "\nPlease choose an action: ";
      char menuChoice;
      do
      {
         menuChoice = '0';
         cin >> menuChoice;
         cin.clear();
         cin.sync();
      } 
       while(isValidInput(menuChoice) == false);
      
      return menuChoice;
   }

MAIN MENU:
1) Start a New Game of Dunstan and Dragons!
2) View top 5 High Scores
3) Quit

Please choose an option: a

Invalid Input, please reenter input: as

Invalid Input, please reenter input: 
Invalid Input, please reenter input: asd

Invalid Input, please reenter input: 
Invalid Input, please reenter input: 
Invalid Input, please reenter input: asdfg

Invalid Input, please reenter input: 
Invalid Input, please reenter input: 
Invalid Input, please reenter input: 
Invalid Input, please reenter input: 
Invalid Input, please reenter input: 1

Entering the dungeon...



I want it to loop the error message once...
Last edited on
I figured it out.... I just changed everything to string and it works now. It was looping because it could only clear one char at a time, so it I typed asda it would take 4 loops to check all 4 of those characters. Thanks for the response king214
You're reading numbers from the keyboard, so why not use integers...?
Because IF the user enter 's' or 'asdasd' then the variable that is a integer would be assigned as a string, and it then messes up the variable not letting it be reassigned when the user reenters a value.
Topic archived. No new replies allowed.