Error Catching Statement

A few questions with this menu template I made and would like to improve.

Problem 1:
My number one issue here is that if the user enters a character accidentally for the menu option, I get a a unbreakable loop. I would like to add some sort of statement that can catch this and loop the user back to the menu.

Problem 2:
I actually just fixed it.

Ill take any other advice for this menu. It will be the template to a problem that I am starting. However, realize there is a lot I haven't learned yet!

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <cmath>
#include <iomanip>
#include <ctime>
using namespace std;
//Global Constants
char exit;
//Function Prototypes
//Execution
int main(int argc, char** argv) {
    //Declare Variables
    unsigned int short choose;
    //Welcome statement to not be looped
    cout<<"Welcome! I liked this problem, had issues with this, etc etc"<<endl;
    //Prompt user for number of problem to execute
    cout<<endl;
    cout<<endl;
    do {                                                //MENU DO LOOP BEGIN
    cout<<"Choose from the following list"<<endl;
    cout<<"1. Savitch 8th Edition Chapter 4 Problem 1-MPG"<<endl;
    cout<<"2. Savitch 8th Edition Chapter 4 Problem 2-MPG2"<<endl;
    cout<<"3. Savitch 8th Edition Chapter 4 Problem 4-Inflation"<<endl;
    cout<<"4. Savitch 8th Edition Chapter 4 Problem 5-Inflation2"<<endl;
    cout<<"5. Savitch 8th Edition Chapter 4 Problem 9-Size"<<endl;
    cout<<"6. Savitch 8th Edition Chapter 4 Problem 10-Size2"<<endl;
    cout<<"7. PrimerPlus 6th Edition Chapter 5 Prb 1-Sums"<<endl;
    cout<<"8. PrimerPlus 6th Edition Chapter 5 Prb 3-Add"<<endl;
    cout<<"9. PrimerPlus 6th Edition Chapter 5 Prb 10-Rows"<<endl;
    cout<<"10. Savitch 8th Edition Chapter 5 Problem 15-Twinkie"<<endl;
    cout<<"11. Exit Program - All"<<endl;
    cin>>choose;
    //Catch invalid input
    if (!(choose<=11)) {
        cout<<"Not an option!"<<endl;
    }
    //Utilize switch to implement the menu
    switch(choose) {
        
        case 1:{ 
///////////////////////////////PROBLEM 1//////////////////////////////////////// 
            cout<<"Welcome to Option 1.";
            cout<<endl;
            //START CODE//////////////////////////////
            do {
    
            //FINISH CODE/////////////////////////////  
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem 1"<<endl;break;
        } //End option 1
        
        case 2:{
////////////////////////////////PROBLEM 2///////////////////////////////////////
            cout<<"Welcome to Option 2.";
            cout<<endl;
            do {
            //START CODE//////////////////////////////
           
            //FINISH CODE/////////////////////////////  
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem 2"<<endl;break;
        } //End option 2
////////////////////////////////////////////////////////////////////////////////        
       
        case 3:{
////////////////////////////////PROBLEM 3///////////////////////////////////////
            cout<<"Welcome to Option 3.";
            cout<<endl;
            do {
            //START CODE//////////////////////////////
                      
            //FINISH CODE/////////////////////////////
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem"<<endl;break;
        } //End option 3
////////////////////////////////////////////////////////////////////////////////
        
        case 4:{
////////////////////////////////PROBLEM 4///////////////////////////////////////
            cout<<"Welcome to Option 4.";
            cout<<endl;
            do {
            //START CODE//////////////////////////////
                       
            //FINISH CODE/////////////////////////////
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem 4"<<endl;break;
        } //End option 4
////////////////////////////////////////////////////////////////////////////////
        
        case 5:{
////////////////////////////////PROBLEM 5///////////////////////////////////////
            cout<<"Welcome to Option 5.";
            cout<<endl;
            do {
            //START CODE//////////////////////////////
                
            //FINISH CODE/////////////////////////////
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem 5"<<endl;break;
        } //End option 5
////////////////////////////////////////////////////////////////////////////////        
       
        case 6:{
////////////////////////////////PROBLEM 6 //////////////////////////////////////
            cout<<"Welcome to Option 6.";
            cout<<endl;
            do {
            //START CODE//////////////////////////////
    
            //FINISH CODE/////////////////////////////
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem 6"<<endl;break;
        } //End option 6
////////////////////////////////////////////////////////////////////////////////
        
        case 7:{
////////////////////////////////PROBLEM 7///////////////////////////////////////
            cout<<"Welcome to Option 7.";
            cout<<endl;
            do {
            //START CODE//////////////////////////////
            
            //FINISH CODE/////////////////////////////
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem 7"<<endl;break;
        } //End option 7
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
        
        case 10:{
////////////////////////////////PROBLEM 10 /////////////////////////////////////
            cout<<"Welcome to Option 10.";
            cout<<endl;
            do {
            //START CODE//////////////////////////////
            
            //FINISH CODE/////////////////////////////
            cout<<"Would you like to run again? Y for Yes, N for No"<<endl;
            cin>>exit;
            } while ((exit!='n')&&(exit!='N'));
            cout<<endl;
            cout<<endl;
            cout<<"End problem"<<endl;break;
        } //End option 10
///////////////////////////////////////////////////////////////////////////////       
        case 11: {
            cout<<"Good Bye!"<<endl;break;
        }
////////////////////////////////////////////////////////////////////////////////        
    } // switch statement end bracket
////////////////////////////////////////////////////////////////////////////////   
    } while (choose!=11);
////////////////////////////////////////////////////////////////////////////////        
//Exit Stage Right
    return 0;
}
1
2
3
4
    //Catch invalid input
    if (!(choose<=11)) {
        cout<<"Not an option!"<<endl;
    }


Right now the code is just printing a message if the user entered an invalid value. You could use a short while loop instead of the if statement to make sure they enter a valid option.

1
2
3
4
5
   
    while (!(choose>=1 && choose<=11)) {
        cout<<"Not an option! Please re-enter: "<<endl;
        cin >> choose;
    }


I would consider breaking out some of the code (the menu display, the actual problems which aren't in this code yet) into separate functions.
Thank you! Yeah, we haven't learned functions yet. I will probably go over the chapter today because thats our next lesson.

Thanks again!
Hmm actually this doesn't seem to fix my original issue, which is to correct the user if they enter a character versus an integer. Either of these two codes produces an infinite loop if the user enters a character by mistake.
Ah ok, not just a number outside of the range. Adding in the clear and ignore should help.

1
2
3
4
5
6
    while (!(choose>=1 && choose<=11)) {
    	cin.clear();
    	cin.ignore();
        cout<<"Not an option! Please re-enter: "<<endl;
        cin >> choose;
    }
Cool, that did it. I must ask, what is cin.clear(); cin.ignore(); actually doing? If you can elaborate?

Thanks again!
The clear resets the "fail" state of the input which was triggered by a character being entered for an integer variable (type mismatch). The ignore clears the input buffer.
Thank you!
Topic archived. No new replies allowed.