bryanlloydharris
asked on
simple menu program
Hi,
I'm trying to create a simple menu program, where the user can type a number and the program displays another menu based on what the user chooses. However, I think there must be a better way than what I have tried because my program keeps crashing(do I need to flush a buffer or something?). Can someone show me a better way to achieve a simple menu program?
To start, I print the main menu to the screen. I want to then clear the screen and display a new menu if the user chooses '1' or '2' etc. Also, if the user is in the menu for choice '1', and then the user chooses '1' again, I want to be able to show another submenu.
So starting off, I draw the screen, then ask for input. To get my input I'm going to do this:
while(!_kbhit()) ;
mychar = _getch();
while (1)
{
switch (mychar)
{
case '1':
current_menu = set_menu(current_menu, '1');
break;
default:
current_menu = "main menu";
break;
}
while(!_kbhit()) ;
mychar = _getch();
}
Also you should know the contents of the set_menu() function:
string set_menu(string current_menu, char user_choice)
{
if(current_menu == "main menu")
switch (user_choice)
{
case '1':
return "another menu choice 1.";
default:
return "main menu";
}
}
Would it be possible for you to show me a better way of doing this? Or is there some example code where I can try and get a better idea for how to do a simple menu? I feel like I am making it too complicated.
Thanks,
Bryan
I'm trying to create a simple menu program, where the user can type a number and the program displays another menu based on what the user chooses. However, I think there must be a better way than what I have tried because my program keeps crashing(do I need to flush a buffer or something?). Can someone show me a better way to achieve a simple menu program?
To start, I print the main menu to the screen. I want to then clear the screen and display a new menu if the user chooses '1' or '2' etc. Also, if the user is in the menu for choice '1', and then the user chooses '1' again, I want to be able to show another submenu.
So starting off, I draw the screen, then ask for input. To get my input I'm going to do this:
while(!_kbhit()) ;
mychar = _getch();
while (1)
{
switch (mychar)
{
case '1':
current_menu = set_menu(current_menu, '1');
break;
default:
current_menu = "main menu";
break;
}
while(!_kbhit()) ;
mychar = _getch();
}
Also you should know the contents of the set_menu() function:
string set_menu(string current_menu, char user_choice)
{
if(current_menu == "main menu")
switch (user_choice)
{
case '1':
return "another menu choice 1.";
default:
return "main menu";
}
}
Would it be possible for you to show me a better way of doing this? Or is there some example code where I can try and get a better idea for how to do a simple menu? I feel like I am making it too complicated.
Thanks,
Bryan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This was very helpful, thank you for the tip!
Bryan