Switch statement disables cin?

I have a program that is basically a while loop meant to continue until input is -1. Inside the while loop is an input (for the user to enter -1 or any other value) and then a switch statement to execute various other functions depending on the input. If the user enters an unspecified input (for example, "e"), then they are supposed to get an "Invalid input" message and then the loop starts over.

However, if I try to enter "e" for the input, it does display the message, but then it keeps reprinting "Enter paycode: " and then "Invalid input" over and over again without stopping at the cin statement like it's supposed to, going into an infinite loop I can't break out of.

This has got to be something incredibly obvious for me to be having such a hard time with it. Here's the relevant part of my code:

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
int main()
{
    int input = 0; // User-input number to select the menu item
       // Loop that repeats the menu and options until the user enters -1
    while ( input != -1 )
    {
          cout << "Enter paycode: ";
          cin >> input; // User enters the paycode
          switch ( input )
          {         // Break the switch and lead to the end of the program
                 case -1: break;
                    // Call manager function, then break the switch
                 case 1: manager(); break;
                    // Call hourly function, then break the switch
                 case 2: hourly(); break;
                    // Call commission function, then break the switch
                 case 3: commission(); break;
                    // Call pieceworker function, then break the switch
                 case 4: pieceworker(); break;
                    // Default to invalid paycode error statement
                 default: cout << "Invalid paycode\n"; break;
          }
          cout << endl;
    }  // End program
    cout << "Goodbye!\n\n";
    system("pause");
    return 0;
}


All help is appreciated.
Problem is, that cin expecting int input, and when you insert a character, it block itself, and return 0. You must then call cin.clear() to unblock it, and using again. You also have to read unwanted input (characters).

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
int main()
{
    int input = 0; // User-input number to select the menu item
       // Loop that repeats the menu and options until the user enters -1
    while ( input != -1 )
    {
          cout << "Enter paycode: ";
		  if (!(cin >> input)) // User enters the paycode
		  {
			  cin.clear();
			  while (cin.get()!= '\n');
			  cout << "Invalid Input!\n";
			  continue;
		  }
          switch ( input )
          {         // Break the switch and lead to the end of the program
                 case -1: break;
                    // Call manager function, then break the switch
                 case 1: manager(); break;
                    // Call hourly function, then break the switch
                 case 2: hourly(); break;
                    // Call commission function, then break the switch
                 case 3: commission(); break;
                    // Call pieceworker function, then break the switch
                 case 4: pieceworker(); break;
                    // Default to invalid paycode error statement
                 default: cout << "Invalid paycode\n"; break;
          }
          cout << endl;
    }  // End program
    cout << "Goodbye!\n\n";
    system("pause");
    return 0;
}
Thanks, your modification works. However, would you mind explaining to me what some of it does? I am unclear on what (!(cin >> input)) means, what cin.clear() or cin.get() does, and why your while statement has no commands within it (it immediately terminates with a semicolon).

Thank you very much!
When you use cin object (cin >> var1) it returns istream &, so you can write cin >> var1 >> var2; but if it gets invalid input (e.g. chars instead of number), it returns NULL (0), and block itself. So command
if (!(cin >> input)) is true when input is ok (because it returns valid istream &), but when input is invalid, it returns NULL (== false).
When cin is blocked, any attempt to use it (cin >> input) fail (it does nothing). To unblock it, and using again you have to call clear function cin.clear(). But in this moment we still have chars in input stream, and we need them get away. That does this code:
while (cin.get()!= '\n');

cin.get(); reads a single char from input stream, and returns it, so we can test, if there is more chars or not (because the last is '\n'). While cycle has empty body, because command we need to do every loop if already part of the condition. It's just shorter entry of:

1
2
3
4
char Ch;
do
   cin.get(&Ch); //reads a single char (or Ch = cin.get();)
while (Ch != '\n');


I hope I explain it (with my english...:-))
I think I understand now, thank you very much :-)
Correction:
Command if (cin >> input) is true when input is ok (because it returns valid istream &), but when input is invalid, it returns NULL (== false).
operator ! flips true -> false, and vice versa
Topic archived. No new replies allowed.