Menu Program Infinite Loop on Invalid Input

May 16, 2016 at 7:34pm
I have modified a menu program, but if you enter an invalid input (with the exception of an invalid integer) it goes into an infinite loop.
I would like to know how to fix this.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream> 

using namespace std;

int main()
{
  int option; 
  do 
  {
     
     cout << "1) Display Most Played MMORPGs " << endl;
     cout << "2) Display Most Played FPS " << endl;
     cout << "3) Display Most Played Survival Games" << endl;
     cout << "4) Exit Program " << endl;
cout << "***Note: Hit Ctrl+C in case of an error*** " << "\n \n " << endl;
     //Prompting user to enter an option according to menu
     cout << "Please select an option : ";
     cin >> option;  

     if(option == 1) 
     {
       
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer 4" << endl;
  cout << "\n \n " << endl;
   }
     else if(option == 2)
     {
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer 4" << endl;
cout << "\n \n " << endl;
}
     else if(option == 3) 
     {
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
cout << "\n \n " << endl;
}
     else if(option == 4) 
     {
       cout << "Terminating Program" << endl;
     }
     else 
     {
       
       cout << "Invalid Option entered" << endl;
     }
  }
  while(option != 4);
May 16, 2016 at 7:53pm
Line 18: If a non-numeric is entered, the cin's fail bit will be set. You need to test the fail bit and if set issue a cin.clear() and cin.ignore ().
http://www.cplusplus.com/reference/istream/istream/ignore/
http://www.cplusplus.com/reference/ios/basic_ios/clear/

18
19
20
21
22
23
24
25
  if (! cin >> option)
  {  // fail bit set
      cin.clear ();
      cin.ignore (256);
  }
  else
  {  // cin operator good
  }



May 16, 2016 at 7:57pm
Sorry, I am new to programming. If I were to simply copy and paste that code into line 19 would it work or would I have to work it into the program in a more complicated way?
Edit: I tested that and it works, but instead of returning the user to the menu it simply displays their invalid input. Also any input afterwards (correct or not) does the same thing and displays their invalid input.
Last edited on May 16, 2016 at 8:01pm
May 16, 2016 at 8:38pm
Lines 20-52 were meant to be within the range of the else
May 17, 2016 at 4:04pm
How do I do that?
Last edited on May 17, 2016 at 4:05pm
May 17, 2016 at 6:25pm
I already told you. Move lines 20-52 of your program to between lines 24 and 25 of the snippet I posted.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream> 
using namespace std;

void do_option (int option)
{   if(option == 1) 
    {   cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer 4" << endl;
        cout << "\n \n " << endl;
    }
    else if(option == 2)
    {   cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer 4" << endl;
        cout << "\n \n " << endl;
    }
    else if(option == 3) 
    {   cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "\n \n " << endl;
    }
    else if(option == 4) 
    {   cout << "Terminating Program" << endl;
        exit (0);
    }
    else 
    {   cout << "Invalid Option entered" << endl;
    }  
}
  
int main()
{   int option; 
    do 
    {   cout << "1) Display Most Played MMORPGs " << endl;
        cout << "2) Display Most Played FPS " << endl;
        cout << "3) Display Most Played Survival Games" << endl;
        cout << "4) Exit Program " << endl;
        cout << "***Note: Hit Ctrl+C in case of an error*** " << "\n \n " << endl;
        //Prompting user to enter an option according to menu
        cout << "Please select an option : ";
        cin >> option;
        if (! cin)
        {   cout << "Not a valid option" << endl;
            cin.clear ();
            cin.ignore (256);
        }
        else
        {   //  Numeric value entered
            do_option (option);
        }
    }
    while (option != 4);
    return 0;
}
Last edited on May 17, 2016 at 6:33pm
May 17, 2016 at 10:20pm
I already told you. Move lines 20-52 of your program to between lines 24 and 25 of the snippet I posted.

That is what i needed to know. Thanks!
May 18, 2016 at 3:14pm
Sorry I wasn't clear about that.
May 18, 2016 at 6:44pm
Hm, when I try to compile it I receive this error (using g++ if that is relevant):

menu.cpp: In function ‘void do_option(int)’:
menu.cpp:27:16: error: ‘exit’ was not declared in this scope
exit (0);
May 18, 2016 at 7:00pm
 
#include <cstdlib> 

May 18, 2016 at 7:12pm
Oh, of course. Thank you.
Edit: I am having the same issue as earlier. Instead of returning the user to the menu it displays their input.
Edit 2: I think I solved it. cin.ignore (256); I changed that 256 to 1 and it seems to work now. Going to play with it some more and make sure it works.
Last edited on May 18, 2016 at 7:29pm
Topic archived. No new replies allowed.