Uncontrolable flowing of screen when wrong input (char instead of int)

I know this is a very common error for beginners, but I'm just trying to find some clever ways of fixing it. It seems strange to me that if you program calls for an int and the user enters a char that it will loop continually as in many programs do. I know it depends on the specifics of the program but what causes this? and what are some ways around it.

Here is one method that I found:
What do you all think of it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char c[1000];
cout << "Enter 1 for option 1 " << endl;
cout << "Enter 2 for option 2 " << endl;
cout << "Enter 3 for option 3 " << endl;
cout << "Enter 4 for option 4 " << endl;
cout << " Type 'Exit' to exit " << endl;

    enter:
    cin.getline(c, 1000);
    if ( ((c[0] == 'e') || (c[0] == 'E')) && ((c[1] == 'x') || (c[1] == 'X')) && ((c[2] == 'i') || (c[2] == 'I')) && ((c[3] == 't') || (c[3] == 'T')) )
        {exit(0);}
    if (c[1] == NULL)
        {choice = c[0] - 48; }
    if ((choice < 1) || (choice > 4))
        { goto enter; }

I've noticed that the following does not work!


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 choice;
while(1){
cout << "Enter 1 for option 1 " << endl;
cout << "Enter 2 for option 2" << endl;
cout << "Enter 3 for option 3 " << endl;
cout << "Enter 4 for option 4 " << endl;
cin >> choice; 

if ( ( choice < 1 ) || ( choice > 4 ) )
        {          
            choice = 0;
        }

switch(choice)
     {
          case 1:
                      option1();
                      break;
          case 2: 
                      option2();
                      break;
          case 3: 
                     option3();
                     break;
          case 4:
                     option4();
                     break;
          default:
                      cout << "Invalid Selection. " << endl;
                      
                      break;
     }
}


Why?

edit: I just didn't copy and paste right. It still doesn't work.
Last edited on
You're not giving the user a chance to enter anything.
It is because std::cin is entering at error state. You can check for that by checking !std::cin.good() and if that is true, use .clear() to fix it and then prompt for input again.

You can also check and get input at the same time by just doing if(std::cin >> var)
In the first example, he uses cin.getline(c, 1000);, and in the second cin >> choice;.

However, the problem is that you are trying to use formatted input when the user is using line input.

That is, the user expects that he must press Enter after every input. This is a line-based input method.

Your program should also get input in this way, and then parse the string using formatted input methods.

1
2
3
4
5
6
7
8
9
int x;
string users_input; // could be anything!
do
  {
  cout << "Please enter an integer> " << flush;
  getline( cin, users_input );
  }
while (!(istringstream( users_input ) >> x));
cout << "Good job! You entered the integer '" << x << "'.\n";

You'll notice that this example accepts nothing less than an integer.
(It may accept more. For example, "42 is a valid answer" works, accepting the value '42' and ignoring everything else. See http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827 for more. And here's a related post: http://www.cplusplus.com/forum/beginner/13562/ .)


The problem with the infinite looping is a direct consequence of failing to observe the above rubric. Given something like:

1
2
3
4
5
6
7
8
// B A D   C O D E !
int num;
do
  {
  cout << "Enter any number except '12'> " << flush;
  cin >> num;
  }
while (num != 12);

This code fails catestrophically if the user enters anything other than a valid number.

Why? Because once an invalid input is encountered, the input stream (in this case, cin) is signaled to a fail state -- meaning that it will not receive any more input, and you will loop forever. Unless, by some miracle, the num variable happened to have the random value '12' before your user entered bad input.

You must always check against bad input.
And you must always accept input according to the user's expectations.

Hope this helps.

[edit] Aw man! Too slow...
Last edited on
firedraco I like your solution only thing is it doesn't seem to work
I used cin.clear() to reset the flags but it still loops infinitely.
You must always check against bad input.

Using cin.clear() won't check for bad input, it will just clear error flags.
Topic archived. No new replies allowed.