I'm stuck with switch case

Hi guys. I'm a new member in this forum but I start to learn C++ reading your posts.

I'm pretty new to C++ and I got a problem trying to put a sub menu in a simple program.

Basically everything is working but once I enter [1] ( Place new order ) , get to the color menu and enter [0] to go back to the main menu ( make the bool false )
is not showing me the menu on the terminal.

But if I press any of the three choice on the main menu ( even if I can't see it ) is working.

Sorry for my English I guess is a bit messy.

Anyone can show me my mistake?

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 #include <iostream>
#include <fstream>
#include <string>
#include <windows.h> 
using namespace std ;
int menu ( int answer ) ;
int NO ( int answer ) ;
int TO ( int answer ) ;
int AO ( int answer ) ;
int menu1 ( int answer1 ) ;
int RED ( int answer1 ) ;
int BLUE ( int answer1 ) ;
int GREEN ( int answer1 ) ;
int main ()
{
	int answer ;
	bool keepLooping = true ;
	cout << "\n\n\n\n\t\t[1] Place new order\n\t\t[2] Display today's oder/s\n\t\t[3] Display all order/s\n\t\t[0] Quit\n\n" ;
	while (keepLooping)
	{
			cin >> answer ;
			int menu ( int answer) ;
		if ( answer > 0 && answer < 4 )
			{
						switch ( answer )
						{
						case 1 :
							NO ( answer ) ;
							break ;
						case 2 :
							TO ( answer ) ;
							break ;
						case 3 :
							AO ( answer ) ;
							break ;
						}
					}
				keepLooping = true ;
				}
		else if ( answer == 0 )
			{
				keepLooping = false ;
			}
		else
			{
				Beep(900,400); 
				Beep(900,400);																				// BEEP
				system ( "cls" ) ;
				cout << "\n\n\tWrong input, please select one of the following menu: " ;
				cout << "\n\n\t\t[1] Place new order\n\t\t[2] Display today's oder/s\n\t\t[3] Display all order/s\n\t\t[0] Quit\n\n" ;
			}
	}
	cout << endl << endl ;
	system ( "cls" ) ;
	cout << "\a\n\n\tBye Bye see you tomorrow.\n\n" ;
	system ("pause") ;
	return 0 ;
}
int NO ( int answer )
{
	int answer1 ;
	bool keepLooping1 = true ;
	system ( "cls" ) ;
	cout << "\n\tPlease select the carpet colour from the following:\n\n\t\t[1] Red\n\t\t[2] Blue\n\t\t[3] Green\n\t\t[0] Back to the Main Menu\n\t\t" ;
	while (keepLooping1)
	{
		cin >> answer1 ;
			int menu1 ( int answer1 ) ;
				if ( answer1 < 4 && answer1 > 0 )
				{
						switch ( answer1 )
						{
						case 1 :
							RED ( answer1 ) ;
							break ;
	
						case 2 :
							BLUE ( answer1 ) ;
							break ;
	
						case 3 :
							GREEN ( answer1 ) ;
							break ;
						}
					
				keepLooping1 = true ;
				}
				else if ( answer1 == 0 )
				{
					keepLooping1 = false ;											
				}
				else
				{
				Beep(900,400); 
				Beep(900,400);												// BEEP
				system ( "cls" ) ;
				cout << "Wrong input!\n\tPlease select the carpet colour from the following:\n\n\t\t[1] Red\n\t\t[2] Blue\n\t\t[3] Green\n\t\t" ;
				}
	}
	return answer ;	
}

												// TODAY ORDERS
int TO ( int answer )
{
	cout << "Today order/s are:" ;
	return answer ;
}

												// ALL ORDERS
int AO ( int answer )
{
	cout << "All the orders are: " ;
	return answer ;
}


												// CARPET COLOURS MENU

int RED ( int answer1 ) 
{
	cout << "RED" ;
	return answer1 ;
}

int BLUE ( int answer1 )
{
	cout << "BLUE" ;
	return answer1 ;
}

int GREEN ( int answer1 )
{
	cout << "GREEN" ;
	return answer1 ;
}
Last edited on
line 22 , 25 , 70 , 73 : you declare menu & menu1 but you not define this function

function menu1 return a int type
you can not define a function inside another function
maybe you wanted to do this?
1
2
3
4
5
6
7
8
9
int menu1( int answer )
{ 
      switch( answer )
      {
              case 1:
                        return red( answer ) ;
               // code 
      }
}
Last edited on
is better like that ? I just modified the code but still doesn't work the sub menu.
Last edited on
Lines 22 and 70 don't actually do anything. They just declare the menu() and menu1() functions, they don't call them. That's a good thing actually, because you haven't defined those functions.

You can simplify the checking and switching code by using the default case in the switch statement. For example, you could replace lines 23-53 with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
switch (answer) {
        case 0:
            keepLooping = false;
            break;
        case 1:
            NO(answer);
            break;
        case 2:
            TO(answer);
            break;
        case 3:
            AO(answer);
            break;
        default:
            Beep(900, 400);
            Beep(900, 400);                      // BEEP
            system("cls");
            cout << "\n\n\tWrong input, please select one of the following menu\
: ";
            cout <<
                "\n\n\t\t[1] Place new order\n\t\t[2] Display today's oder/s\n\\
t\t[3] Display all order/s\n\t\t[0] Quit\n\n";
            break;
        }

Basically everything is working but once I enter [1] ( Place new order ) , get to the color menu and enter [0] to go back to the main menu ( make the bool false ) is not showing me the menu on the terminal.

The problem is that the code that displays the menu (line 18) is outside the loop at line 19. Just swap lines 18 and 19. You have the same problem at lines 66 & 67.

Finally, the menus will be easier to read if you do them like this:
1
2
3
4
5
        cout << "\n\n\n\n"
             << "\t\t[1] Place new order\n"
             << "\t\t[2] Display today's oder/s\n"
             << "\t\t[3] Display all order/s\n"
             << "\t\t[0] Quit\n\n";
Yeah cool!
Now is working and looking much better!!

Thanks guys!

Topic archived. No new replies allowed.