Reputation: 39
I have a problem about using while switch cases ı have a code like this and I wanted to see if the user wrote other than 1,2,3,4,5, then I wanted to just print "Please enter option" and asked for new input which means I don't want to go back to the choice menu, I know it looks like quite easy and I tried to use a bool and used in inside the while but I got stuck in an infinite loop.
cout << "Welcome to program." << endl;
while (true)
cout << "Please make a choice from the following menu: " << endl;
cout << " Press 1 for this.... ," << endl;
cout << "Press 2 for this.... . ," << endl;
cout << "Press 3 for this.... . ," << endl;
cout << "Press 4 for this.... . ," << endl;
cout << "Press 5 for this.... ." << endl;
cout << endl;
cin >> num;
cout << endl;
switch (num)
{
case 1:
cout << "Success!" << endl;
break;
case 2 :
cout << "You presed 2 " << endl;
break;
case 3 :
cout << "You pressed 3 " ;
break;
case 4:
cout << "You pressed 4 " ;
cout << endl;
break;
case 5 :
cout << "You pressed 5 " ;
default:
cout << "Please enter a valid option!" << endl; // only print this while user write other than cases
cin >> num;
cout << endl;
Upvotes: 0
Views: 2730
Reputation: 5202
Anything you don't want repeated, don't put in a loop. It's that simple. Present the full menu and first prompt outside the loop. Your default case will print its message only if a bad number was entered. I also shrunk the switch because all that extra typing was unnecessary. [[fallthrough]]
is an excellent tool, but it requires C++17.
If all you want is a valid input, you really shouldn't bother with a while (true)
infinite loop. Just iterate as long as your input is invalid.
#include <iostream>
int main() {
int num = 0;
std::cout << "Welcome to the program.\n";
std::cout << "Please make a choice:\n"
<< "1.\n2.\n3.\n4.\n5.\n\n"
<< "Please enter option: ";
while (num < 1 || num > 5) {
std::cin >> num;
switch (num) {
case 1:
std::cout << "Success!\n";
break;
case 2:
[[fallthrough]]; // C++17 needed for this, otherwise leave empty
case 3:
[[fallthrough]];
case 4:
[[fallthrough]];
case 5:
std::cout << "You pressed " << num << ".\n";
break;
default:
std::cerr << "Invalid entry!\nPlease enter a **valid** option: ";
}
}
}
Upvotes: 0
Reputation: 5786
while (true)
I can see that you are not using brackets after the while loop, so how are you expecting it to loop properly. It will only execute the immediate next line of code if there are no brackets. So, encapsulate your code inside of the while(true)
Apart from that, you are never breaking from the while loop, so you would be stuck in the while loop. You don't have a condition inside the while loop that ever breaks you out of the loop anytime. The break
s in the switch-case block will only break you out of the switch case block, but you will still remain inside the while loop. So, If you want to break out of the loop, you must break out of the loop outside the switch block.
You could probably maintain a flag which indicates if any of option 1,2,3,4,5 executes, then you can break out of the loop. Or you could think of a logic that suits your use-case. I just pointed out where you were going wrong.
cout << "Welcome to program." << endl;
int num=3;
while (true) {
if(num>=1&&num<=5){
cout << "Please make a choice from the following menu: " << endl;
cout << " Press 1 for this.... ," << endl;
cout << "Press 2 for this.... . ," << endl;
cout << "Press 3 for this.... . ," << endl;
cout << "Press 4 for this.... . ," << endl;
cout << "Press 5 for this.... ." << endl;
cout << endl;
}
cin >> num;
cout << endl;
switch (num)
{
case 1:
cout << "Success!" << endl;
break;
case 2 :
cout << "You presed 2 " << endl;
break;
case 3 :
cout << "You pressed 3 " ;
break;
case 4:
cout << "You pressed 4 " ;
cout << endl;
break;
case 5 :
cout << "You pressed 5 " ;
break;
default:
cout << "Please enter a valid option!" << endl; // only print this while user write other than cases
continue;
}
}
The above code will loop till the user enters an invalid option and break out of the loop only once a correct option has been entered by the user.
Upvotes: 2