I tried to make a simple switch for practice such as making case 3 as the sub menu . After I exit the sub menu of case 3, case 4 output suddenly undesired to appear for no reason and after that I cannot use case 3 output again from the input when I press 3 from the input again.
Keep in mind the program should be a beginners level so dont make it complicated which makes me easier to read as a beginner
You need to put a break in each case, in order to prevent it from "falling through" to the next case.
Note: A break statement within a nestedswitch or while block breaks out of thatswitch or while block, but it does not break out of the enclosingswitch or while block! Your case #3 doesn't have a break.
If you need to break out of a switch or while block at a "higher" level, using a goto statement is an option:
Here the break statement is unreachable, because the goto triggers first.
And again you have superfluous curly braces. Just do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
case 3:
while (foo)
{
switch (bar)
{
/* ... */
case'c':
cout << endl;
goto endloop; // <-- break out of the enclosing while loop (is it actually needed?)
}
}
endloop:
break; // <-- prevent case #3 from falling trough to case #4
}
@kigar64551
Well I still dont understand how do I use goto variable to break out of while of case 3 so I can use case 3 the second time when I press 3 on the input again.
#include <iostream>
usingnamespace std;
int main()
{
char submenu;
int menu;
while (menu != 5)
{
cout << "Menu\n";
cout << "1. How is the weather today?\n";
cout << "2. Can you win?\n";
cout << "3. Sub menu about asking numbers\n";
cout << "4. Are you happy? \n";
cout << "5. Goodbye \n";
cin >> menu;
switch(menu)
{
case 1:
cout << "Today is sunny" << endl;
break;
case 2:
cout << "No I do think so" << endl;
break;
case 3:
while (submenu != 'c')
{
cout << "Sub Menu\n";
cout << "a. How much sugar do you want?\n";
cout << "b. What is your age?\n";
cout << "c. Exit\n";
cin >> submenu;
switch (submenu)
{
case'a':
cout << "100 grams would be good" << endl;
break;
case'b':
cout << "99999 I guess" << endl;
break;
case'c':
cout << endl;
goto Endloop;
}
}
Endloop:
break;
case 4:
cout << "Yes I am" << endl;
break;
case 5:
cout << "You too" << endl;
break;
}
}
}
You only need the goto trickery, if you want to break out of the while loop from within a nestedswitch block. Because, otherwise, the break would only prevent the current case (of the nested switch) from falling through to the next case (of the nested switch), but would not break the enclosingwhile loop.
@kigar64551
So in this case of final break from case 3 and the outer switch block ends, how do I able to enter case 3 again the second time after I exit case 3?
@kigar64551
Yeah while (menu != 5) still true but it cannot enter case 3, the rest of the cases for menu works even when enter their respective inputs.
Maybe you can try out my code to see the problems?
You don't need the goto - these are best avoided. Because submenu is defined outside of the switch, it keeps it's value between cases of 3 - so the second time option 3 is used, submenu still has the value 'c' - so exits!. It's best to define variables as close to their usage as possible:
#include <iostream>
usingnamespace std;
int main() {
int menu {};
while (menu != 5) {
cout << "Menu\n";
cout << "1. How is the weather today?\n";
cout << "2. Can you win?\n";
cout << "3. Sub menu about asking numbers\n";
cout << "4. Are you happy? \n";
cout << "5. Goodbye \n";
cin >> menu;
switch (menu) {
case 1:
cout << "Today is sunny\n";
break;
case 2:
cout << "No I do not think so\n";
break;
case 3:
{
char submenu {};
while (submenu != 'c') {
cout << "Sub Menu\n";
cout << "a. How much sugar do you want?\n";
cout << "b. What is your age?\n";
cout << "c. Exit\n";
cin >> submenu;
switch (submenu) {
case'a':
cout << "100 grams would be good\n";
break;
case'b':
cout << "99999 I guess\n";
break;
case'c':
cout << '\n';
break;
}
}
}
break;
case 4:
cout << "Yes I am\n";
break;
case 5:
cout << "You too\n";
break;
}
}
}