troubleshooting Question

simple menu program

Avatar of bryanlloydharris
bryanlloydharris asked on
C++
2 Comments1 Solution228 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros