Problem with Nested Switch Statements

Hi, i tried to create a nested switch, but I'm getting errors. Please help.Thanks :)

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
void menu() {
	bool checkCall = false;
	int option;

	do {
	cout << " \nSelect an option (use integer value only): ";
	cin >> option;
		
		switch (option) {
			case 1:
				break;
			case 2:
				int options;

				do {
				
				cout << "Select an option (1, 2, 3, or 4): " << endl;
				cin >> options;
				switch (options) { //trying to make another loop
		        case 1:
				if (checkCall) {
					cout << "Hi \n" << endl;
				} else {
					cout << "Bye \n" << endl;
				}
				break;
					default:
						cout << "WRONG" << endl;
				} 
				}
			
			case 3: //ERROR CASE
				if (checkCall) { //MISSING ';' BEFORE '{'
					cout << "\nHello " << endl;
					
				} else { //Illegal else without matching if
					cout << "\nBye " << endl;
							
				}
				break;
			case 4:
				break;
			case 5:
				break;
			case 6:
				break;
			case 7:
				break;
			default:
				cout << "Choose A New Option\n" << endl; 
		}
	} while (option != 7);

	return;
}
i didn't realize this, but the syntax for a do loop is:

1
2
3
do{

}while(expression);


i didn't think that the while statement needed to be there, but it does.
Last edited on
Good and consistent formatting makes it a lot easier to spot mistakes.

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
void menu()
{
    bool checkCall = false;
    int option;

    do
    {
        cout << " \nSelect an option (use integer value only): ";
        cin >> option;

        switch (option)
        {
            case 1:
                break;
            case 2:
                int options;

                do
                {
                    cout << "Select an option (1, 2, 3, or 4): " << endl;
                    cin >> options;
                    switch (options)
                    { //trying to make another loop
                        case 1:
                            if (checkCall)
                            {
                                cout << "Hi \n" << endl;
                            }
                            else
                            {
                                cout << "Bye \n" << endl;
                            }
                            break;
                        default:
                            cout << "WRONG" << endl;
                    } 
                }

            case 3: //ERROR CASE
                if (checkCall)
                { //MISSING ';' BEFORE '{'
                    cout << "\nHello " << endl;
                }
                else
                { //Illegal else without matching if
                    cout << "\nBye " << endl;
                }
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            default:
                cout << "Choose A New Option\n" << endl; 
        }
    } while (option != 7);

    return;
}


You are missing the while statement in your nested do-while loop.
Last edited on
Topic archived. No new replies allowed.