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!
#include <iostream>
#include <cmath>
#include <iomanip>
#include <ctime>
usingnamespace std;
//Global Constants
char exit;
//Function Prototypes
//Execution
int main(int argc, char** argv) {
//Declare Variables
unsignedintshort 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;
}
//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.
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.
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.