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.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
usingnamespace 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 ;
}
elseif ( 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 ;
}
elseif ( 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 ;
}
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:
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";