Switches and sub menus outputs are out of control

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


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

#include <iostream>
using namespace 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;
                          break;
                      }
                  }
              }
          }
          
          case 4:
          {
              cout << "Yes I am" << endl;
              break;
          }
          
          case 5:
          {
              cout << "You too" << endl;
              break;
          }
      }
  }
}


Last edited on
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 nested switch or while block breaks out of that switch or while block, but it does not break out of the enclosing switch 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:

1
2
3
4
5
6
7
8
9
10
while(foo)
{
    switch(bar)
    {
    case 1:
        /* ... */
        goto endLoop;
    }
}
endLoop:
Last edited on
Learn to use functions more.

I don't like this:

while (menu != 5)

I prefer:

1
2
3
4
5
6
7
8
9
10
bool Quit {false};

while (!Quit) {
/// ...

case 5: {
Quit = true;
// ...
}
}


Always put a default: case in, to catch bad input.
@kigar64551
Which Line should I put a break for case 3?
Which Line should I put a break for case 3?

Right after the nested while block in case #3?

(Assuming that you don't want case #3 to "fall through" to case #4)


Also, no curly braces are needed around each case.

Just do:

1
2
3
4
5
6
7
8
9
10
11
switch (foo)
    /* ... */
case 3:
    while (bar)
    {
        /* ... */
    }
    break; // <-- prevent case #3 from falling trough to case #4
case 4:
    /* ... */
}
Last edited on
Ok case 4 output doesnt undesireable appear anymore after I exit the submenu of case 3 but I cannot enter case 3 again when I press 3 again of input
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
#include <iostream>
using namespace 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;
                          break;
                      }
                  }
              }
              endloop:
              break;
          }
          
          case 4:
              cout << "Yes I am" << endl;
              break;
          
          case 5:
              cout << "You too" << endl;
              break;
      }
  }
}
Last edited on
1
2
3
4
5
6
case 'c':
{
    cout << endl;
    goto endloop;
    break;
}

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
}
Last edited on
@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.
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
#include <iostream>
using namespace 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;
         
      }
  }
}
Last edited on
You only need the goto trickery, if you want to break out of the while loop from within a nested switch 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 enclosing while loop.
Last edited on
@kigar64551
Ok then why my case 3 cannot enter again after I exit case 3?
How do I able to enter case 3 again the second time after I exit case 3?
After the final break in case #3, the "outer" switch block ends.

It then depends on the enclosing "outermost" while whether we re-enter that switch block again or not...
Last edited on
@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?
Make sure that while (menu != 5) is still true, so that "outermost" while doesn't exit yet?

...because only then switch (menu) will be entered once again!

Or maybe you need yet another for(;;) { /*... */ } directly loop enclosing your switch (menu) block?
Last edited on
@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:

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
#include <iostream>
using namespace 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;
		}
	}
}

Topic archived. No new replies allowed.