I am doing a simple menu and also checking for errors.
Meaning my menu has only 4 options. If user enters 5, or 6, or A b C d. It will return "An invalid option choosen, please enter again.
Whenever I enter a correct number, it just goes into an infinite loop showing, "You have choosen 1". How do I resolve this? And when I input an invalid option, it will request me to input the correct option again but how do I re-direct it to the correct case rather than it showing to exit Quincy 2005..
I added break; at the end of my switch. The loop ends but as for the checking of my options. When I choose 1, it will show
"you have choose 1"
"you have choosen 2"
"you have choosen 3"
... and also
"Invalid option choosen..."
How do I resolve this for error checking?
When I enter 1, I want to only show You have choosen 1.
And if I enter a wrong option like, 5 or A B C. I want it to show Invalid option choosem please enter your choice again..
How do I re-direct it to choose the main menu again and then go towards the while loop again?
declare a bool variable in main and set it to false bool found=false;
Change your while condition to while((menuChoice == 1 || menuChoice == 2 || menuChoice == 3 || menuChoice == 4) && found==false)
then each case include found=true; before break;
if you want to use the invalid option to be shown then rewrite your while condition to while (found==false)
and include default statement in the switch case to print error message like this
Use a default case in the switch, then you won't need the while loop at all. You will need a while in conjunction with the bool variable mentioned by vichu8888, to ask for valid input again.
Thanks for your help! I successfully stopped the looping but another problem is found.
If i input an invalid choice (i.e 5/a/b/c) It goes straight to invalid choice, enter again, and when I enter a correct option, it goes straight to "Hit enter to exit quincy".
Any way to solve this?
And also when I input choice 1, it is correct, it shows "You have choosen 1" But the message, "Invalid choice comes up too".
The purpose of a default: clause in a switch is to catch all other values - that is the values that don't match any of the other cases. For this reason, it is often used to do error processing. So this is where you put the invalid option code.
use proper brackets for while condition, include break inside each case, include default statement for switch and insert the error statements 56 and 57 in it.
I have done this is another way with the default: clause you guys mentioned.
Everything seems to be fine but I would wish that when I have choosen 1, the interface of test.mainMenu() doesn't show up again. I want it to stop at You have choosen 1 because I will be making it into a option and also letting people input datas into this option.
How do I make it so that the default menu will not appear again?
@TheIdeasMan . Does while condition make any sense to you?
I could understand his while condition part.
Suppose user enters 1, why it execute again. Because when input is 1 the repeat becomes false right? But the program works as expected.
How come this?
Can you explain whats going on in while condition?
@b1b2b3b4
What does your program actually wants to do?
Does it want to loop again only if the user enters wrong input or
it should repeat as long as the user wish?
char menuChoice; // char not int, so we can have a quit option
bool repeat=false;
bool Quit = false;
Test test;
while ( repeat == true || Quit == false) {
test.mainMenu();
cout<<"Please enter your choice : ";
cin>>menuChoice;
switch(menuChoice) {
case'1':
cout<<"You choosen 1"<<endl;
break;
case'2':
cout<<"You chose 2"<<endl;
break;
case'3':
cout<<"You chose 3"<<endl;
break;
case'4':
cout<<"You chose 4"<<endl;
break;
case'Q':
cout<<"You have chosen to Quit"<<endl;
Quit = true; //program continues after the end of the while
//could use exit(0) function to quit altogether
break;
default:
cout<<"Invalid entry!"<<endl;
repeat=true;
break;
}
}
I haven't tested this, but it is what I was thinking of with the comments I made earlier.