Menu Hierarchy in C: avoid stack issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roguefeebo
    New Member
    • Apr 2007
    • 8

    Menu Hierarchy in C: avoid stack issues

    Hello everyone, thanks for looking. I'll get right to the point: I'm having problems trying to figure out a hierarchy of menus in C; command-line style.

    Say, for example the program analyzes a file of names/letters to figure out the patterns of letters within the names. The user should be able to do many things with the information such as add names, delete names, and add new letters (a letter is defined as any subset of a group of consonants or vowels) just to name a few.

    I have all the underlying functions done (successfully analyzing the file various ways and saving the info).

    The main problem I have is that I don't want the user to be able to navigate the menus in a way that it places a ton of menus on the stack.

    An ideal use case stack would look like this:

    Main Menu
    --Names Database Menu
    ----Sort Names Menu
    ------Sort Names by length function
    ------(Original Sort Names Database removed from function stack)
    ----Sort Names Menu
    ----*Return to Main Menu option*
    ----(Original Names Database Menu removed from function stack
    ----(Second Sort Names Menu removed from function stack)
    Main Menu

    The problem I'm having is to figure out an intuitive method that would avoid a situation that has the potential to create a huge stack like this:

    Main Menu
    --Names Database Menu
    ----Sort Names Menu
    ------Sort Names by length function
    ----Sort Names Menu
    ------*Return to Main Menu Option chosen*
    ------Main Menu
    --------etc, etc

    I'm wondering if I should have an array of function pointers for each menu available (which is a lot) or if I should have different switch functions for each menu display function, or the other 6 options I can think of that I can't seem to work an easily understandable bit of pseudocode for.

    Any help at all would be appreciated... I don't need working code. I'm just not sure how menus are really done in the real world and figured it would be an easy suggestion from those more experienced.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Hi,

    well,on your place I would create functions for menu creation with variable
    number of options.With this you would be able to disable some previous option.

    So we will have next situation:


    menu;

    choice 1;

    menu;//with some options disbanded

    choice 2;

    .
    .
    .


    If u are not sure or u don't know how to create such a function post here and
    ask a specific question.

    And when posting code please use code tags (# on main toolbar in message window)

    Savage

    Comment

    • roguefeebo
      New Member
      • Apr 2007
      • 8

      #3
      Thanks for the fast response, :) I wasn't sure if the code segments applied to what I was typing before. I'll try to use it more often to make things clearer

      As for the problem, in main I have:

      Code:
            do
            {
               exit = mainMenu();
            } while(exit != 1);
      And mainMenu():

      Code:
      int mainMenu()
      {
      
           char choice;
            
           printf("a.  Enter Names Database\n");
           printf("b.  Enter Letters Database\n");
           printf("c.  Start Random Name Generator\n");
           printf("q.  Quit Program\n");
           
           scanf("%c", &choice);
           
           switch(choice)
           {
             case 'a':
                  {
                       nameMainMenu();
                       return 0;
                       break;
                  } //end case a
             case 'b':
                  {
                       letterMainMenu();
                       return 0;
                       break;
                  } //end case b
             case 'c':
                  {
                       printf("Starting Random Generator.\n");
                       //I plan on having the Name Generator as a separate program that starts up on this call... I don't have a clue how to do it so haven't got to that yet.
                       return 0;
                  } //end case c
             case 'q':
                  {
                       printf("Quitting program now...\nPress any key to continue...\n");
                       return 1;
                       break;
                  } //end case q
             default:
                  {
                       printf("Incorrect option chosen.  Please choose another option.");
                       return 0;
                       break;
                  } //end default
           } //end switch
           
      } //end function mainMenu
      With underlying menus:
      Code:
      char nameMainMenu()
      {
           char choice;
           
           printf("a.  Add Names File to Database\n");
           printf("b.  Search For a Name\n");
           printf("c.  Add a Name to Database\n");
           printf("d.  Delete Name From Database\n");
           printf("e.  Erase Names Database\n");
           printf("f.  Modify Name in Database\n");
           printf("g.  Sort Names for Display and Saving\n");
           printf("s.  Save Names Database\n");
           printf("m.  Return to Main Menu\n");
           
           scanf("%c", &choice);
           
           switch(choice)
           {
             case 'a':
                  {
                    analyzeNameMenu();
                    break;
                  } //end case a
            /*snipped a bunch of case statements*/
             case 'm':
                  {
                    mainMenu();
                    break;
                  } //end case m
             default:
                  {
                    printf("Incorrect option chosen.  Choose another option.\n");
                    nameMainMenu();
                    break;
                  } //end default
           } //end switch
                
      } //end function nameMainMenu
      And continuing on from there to other sub-menus. The problem I saw with the switch statements is that they just keep piling on top of each other since the previous function hasn't returned any value nor completed the call.

      Will it correct itself if I set each menu to have a return to a loop similar to main in another function? Are these switch statements that spaghetti fairly quickly the best way to make menu hierarchies? Thanks a bunch!

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        1)Is it practical?


        So let me get it clear:

        with: do{
        exit=menu();

        }while(exit!=1) ;

        u will call this function and if it return 0 u will again call that function that's
        very practible if u menu will have a constant number of options but in your case
        you want to dismiss some options so this wont work.

        So instead u will need some type of identifer which will in depandacne of it's value call menu with different options.

        2.)
        Here comes experiance of those quite experienced pogrammers.On ur place
        they would build up a totaly modular menu function with variable number of options,which will quite lower the code number of lines.

        So instead of ur it would take a look as:

        int menu(int nOfOptions,char *text,....);

        with such menu function there is a great posibility of changing number of options
        and text for each option using opereator '...'

        and now to the code:

        Code:
        i=0;//this represent indentifer.
        do{
              //because ur menu has need of changing options it will require:
              if(i==0) call ur previous menu;
              else call other menu.
        
        }while(!exit);
        3.This is also a answe to this question.

        Savage

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          About this topic:

          printf("Startin g Random Generator.\n");
          //I plan on having the Name Generator as a separate program that
          starts up on this call... I don't have a clue how to do it so haven't got to
          that yet.
          return 0;
          Use execlp or spawnlp:

          if the program name is generator.exe then it would be like:

          Code:
          spawnlp(P_OVERLAY,"generator.exe",argv[1],argv[2],NULL);
          Note: Must define main as int main( int argc,char *argv[]) and run programm from DOS command line.

          Savage

          Comment

          • roguefeebo
            New Member
            • Apr 2007
            • 8

            #6
            Awesome, this explains a bunch. Thanks a lot!

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Allways a pleasure to help!!!!

              :D

              Savage

              Comment

              Working...