Search code examples
csubmenu

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!

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");
             printmenu311();
             break;
             case 'c':
             puts("Welcome to flight 444 service");
             printmenu444();
             break;
             case 'd':
             puts("Welcome to flight 519 service");
             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

The submenu is automatically being displayed without staying in main menu.


Solution

  • This worked! I missed the while loop. My bad!

    while(1)
            {
        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;
            }
            }