Calling main menu is displaying sub menu

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeevan83
    New Member
    • Aug 2014
    • 9

    Calling main menu is displaying sub menu

    i am doing an airplain seat booking system. Here I have a main menu of chooseflight() and submenu of printmenu(). Whenever I press 'f' from printmenu's options I should return to mainmenu. But I cannot. Please help me!

    Code:
    int main()
    {
        chooseflight();
        fflush(stdin);
        return 0;
    }
     
    void chooseflight(void)
    {
        char selectflight;
        printf("a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram\n");
            scanf("%c",&selectflight);
            switch(selectflight)
            {
                case 'a':
                 puts("Welcome to flight 102 service");
                 while(1){
                 printmenu102();
                 }
                 break;
                 case 'b':
                 puts("Welcome to flight 311 service");
                 while(1){
                 printmenu311();
                 break;
                 }
                 case 'c':
                 puts("Welcome to flight 444 service");
                 while(1){
                 printmenu444();
                 }
                 break;
                 case 'd':
                 puts("Welcome to flight 519 service");
                 while(1){
                 printmenu519();
                 }
                 break;
                 case 'e':
                 Quitprogram();
                 break;
            }
    }
     
    void printmenu102()
    {
            char lablename;
     
        printf("a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu\n");
     
        scanf("%c",&lablename);
        switch(lablename)
        {
            case 'a':
                Noofemptyseats102();
                break;
                case 'b':
                Listofemptyseats102();
                break;
                case 'c':
                Alphabeticallistofseats102();
                break;
                case 'd':
                Assingseats102();
                break;
                case 'e':
                Deleteseats102();
                break;
                case 'f':
                Quittotopmenu();
                break;
        }
    }
     
    void Quitprogram(void)
     {
     exit(EXIT_FAILURE);
     }
     
     void Quittotopmenu(void)
     {
    chooseflight();
     }
    My output is:
    a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram

    a

    Welcome to flight 102 service

    a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu

    a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu

    f

    a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram

    a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It looks like you are managing these menus by having chooseflight call itself.

    I suggest you handle the top menu like:

    Code:
    int main()
    {
    
       while(1)
       {
          switch(choice)
          {
             case 'a':
                  callchoicea();
           }      break;
         }
    
    }
    When callchoicea() returns, that choice is done.

    The fact that callchoicea() many have sub menus does not alter the fact that when it returns, the choice is done. So to get to the top menu, you just return in callchoicea().

    This avoids recursion (having the function call itself) and keeps the top menu inside one infinite while loop.

    callchoicea() would follow the same logic pattern as main().

    Comment

    • Jeevan83
      New Member
      • Aug 2014
      • 9

      #3
      Thanks bt somehow I solved it yesterday night and did samething like you! :)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Another trick is to have your menu programs communicate by using menu choices that are not entered by the user. The menu function can return a choice that the upper menu can service even though it is not entered by the user. A good example is to abort the program or to signal database repair without actually calling the repair function.

        Look up the Finite State Machine.

        Comment

        Working...