Question about "Back" option in menu.

This is a discussion on Question about "Back" option in menu. within the C++ Programming forums, part of the General Programming Boards category; I'm working on a text-based program that is written almost exclusively in functions. I'm using switch statements for the menu ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    18

    Question about "Back" option in menu.

    I'm working on a text-based program that is written almost exclusively in functions.
    I'm using switch statements for the menu choices. Each case input will call the appropriate function.

    So here's an example.


    Code:
    int main()
    {
    // options for load_file(), new_file(), and exit()
    welcome();
    
    // options for set_name(), set_gender(), save_file(), exit()
    main menu();
    }

    So as you can see. Functions welcome() and main menu(), branch off into smaller functions depending on user input. What I'm trying to figure out is how to allow the user to "back" out to the master functions. So lets say they select load_file() accidentally and want to return to welcome(); Or they select set_gender() and want to return to main_menu();

    Would I have to write a loop every time a function branches into another function, or could I simply have the "back" option just re-call the previous function to start over from there?

    Also, is there anything wrong or sloppy about using a function based program like this? I'm new to coding, and it seemed like the cleanest way in my mind. I have a pretty nice looking flow chart, but I wouldn't be surprised if it is a completely rookie way to do things.

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When a function returns, it goes back to where it came from. So if your welcome menu calls load_file, then when load_file returns you are back to the welcome function.

    That does mean that your menu printer/chooser code should be in a loop, so that when a function returns you get to see the menu again.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by tabstop View Post
    When a function returns, it goes back to where it came from. So if your welcome menu calls load_file, then when load_file returns you are back to the welcome function.

    That does mean that your menu printer/chooser code should be in a loop, so that when a function returns you get to see the menu again.
    Thank you! That answers my question.
    So what's the smallest amount of code to achieve that?

    Code:
    do 
    {
    main_menu();
    }while (choice != 5);
    and I can just have the input associated with the "back option" to set choice to 5 and return?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could do that, although if you're going to check for a return value you should make it possible to obtain a return value (either by passing a parameter via pointer or by assigning the return value to a variable). I would suggest having the loop in main_menu itself; the function shouldn't return until the menu is done.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Well you could do it elegantly with a bit of refactoring and redesign. If you use a stack you can implement a back function quite easily and it does not require tons of functions. Your menus should run off of a data-driven model. If they can do this then you have a menu class that can read the data, make decisions off of the data, etc. Implement a back operation in this kind of system is as simple as popping a menu off the stack.

    Your so-called 'simple' approach is actually the brute force hard way to go about doing this.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    18

    Talking

    Quote Originally Posted by Bubba View Post
    Well you could do it elegantly with a bit of refactoring and redesign. If you use a stack you can implement a back function quite easily and it does not require tons of functions. Your menus should run off of a data-driven model. If they can do this then you have a menu class that can read the data, make decisions off of the data, etc. Implement a back operation in this kind of system is as simple as popping a menu off the stack.

    Your so-called 'simple' approach is actually the brute force hard way to go about doing this.
    I guess its like I'm moving a mound of dirt with a shovel instead of a tractor. The shovel might be a barbaric way of doing it, but the concept is easy. A tractor on the other hand would be a very elegant way of doing it, but I would have to learn how to operate a tractor.

    In other words, I'm new to programming and have no idea how to use stacks.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    In other words, I'm new to programming and have no idea how to use stacks.
    And this is a great time to start learning about them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with number menu
    By j4k50n in forum C Programming
    Replies: 12
    Last Post: 04-17-2007, 01:33 AM
  2. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-07-2005, 11:05 PM
  3. A tiny question about menu
    By Kelvin in forum Windows Programming
    Replies: 1
    Last Post: 08-05-2002, 03:53 PM
  4. Problem in Getting Menu Option
    By seh_hui in forum C Programming
    Replies: 3
    Last Post: 12-06-2001, 07:39 PM
  5. quick question about C Dos text menu pgm i was doing
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21