繁体   English   中英

我正在尝试使用C制作菜单,但是我的代码无法按照我想要的方式工作

[英]I'm trying to make a menu in C and my code is not working the way I want it to

我正在为我的编程课分配作业,我要做的第一件事当然是制作主菜单。 但是由于某种原因,该程序无法按我预期的方式运行。

#include <stdio.h>
#include <stdlib.h>
void mainMenu();
void firstChoice();
int main()
{
    int main_menu_choice;
    int _1returnMenu;
    mainMenu();
    scanf("%d", &main_menu_choice);
    while (main_menu_choice != 0){
        switch(main_menu_choice){
        case 1:
        firstChoice();
        scanf("%d", &_1returnMenu);
        while (_1returnMenu != 0){
            switch (_1returnMenu){
            case 1:
            firstChoice();
            scanf("%d", &_1returnMenu);
            break;
            case 0:
            mainMenu();
            scanf("%d", &main_menu_choice);
            break;
            default:
            printf("Invalid option, please try again: \n");
            scanf("%d", &_1returnMenu);
            break;
            }
        }
        break;
        case 0:
        exit(0);
        default:
        printf("Invalid option, please try again: \n");
        scanf("%d", &main_menu_choice);
        }
    }
    return 0;
}

void firstChoice(){
    printf("Enter the necessary information \n");
    printf("Please enter the student's ID: \n");
    scanf("%d", &student.id);
    printf("Please enter the student's name: \n");
    scanf("%s", student.name);
    printf("Please enter the gender of the student: \n");
    scanf("%s", student.gender);
    printf("Please enter the details of the room: \n");
    scanf("%s", student.roomDetails);
    printf("Please enter the amount due of the student: \n");
    scanf("%d", &student.amountDue);
    printf("Please enter the amount paid by the student: \n");
    scanf("%d", &student.paymentMade);
    printf("Do you want to store another hosteler's information? \n");
    printf("Type 1 if you wish to continue, Type 0 to go back to the main menu: \n");
    return;
}

void mainMenu(){
    printf("Welcome! This program will help you in managing the hostel booking 
    of Wisdom College \n");
    printf("Type the number of your option: \n");
    printf("1. Store details of hosteler \n");
    printf("2. Check room availability \n");
    printf("3. Payment Facility \n");
    printf("4. Search room details of hosteler \n");
    printf("5. To cancel booking of a hosteler \n");
    printf("6. Change room type \n");
    printf("0. Exit the program \n");
    return;
}

好的,所以当我运行程序并选择第一个选项“ 1.Store hosteler的详细信息”时,只需输入一些随机信息,在输入所需信息后,程序将询问我是否要继续使用它。 如果我按1,它将要求我再次填写所需的信息,如果我按0,则应返回主菜单。 但问题是,当我输入0时,它不会返回主菜单,而是仅再次运行firstChoice()函数,而不是在switch语句中运行case 0。 我已经花了两个小时挠头试图找出问题所在,但我似乎无法找到问题所在。 如果无法正确表达我的问题,我们深感抱歉,并提前致谢!

以下建议的代码显示了如何编写菜单显示和处理

int done = 0;
while( !done )
{
    mainMenu();
    if( scanf("%d", &main_menu_choice) != 1 )
    {
        fprintf( stderr, "scanf for menu choice failed\n" );
        exit( EXIT_FAILURE );
    }

    switch(main_menu_choice)
    {

    case 0:
        done = 1;
        break;

    case 1:
        firstChoice();
        break;

    case 2:
        checkRoomAvalability();
        break;

    case 3:
        paymentFacility();
        break;

    case 4:
        switchRoomDetails();
        break;

    case 5:
        cancelBooking();
        break;

    case 6:
        changeRoomType();
        break;         

    default:
        puts("Invalid option, please try again: \n");
        break;
    }
}


void mainMenu()
{
    puts(  "Welcome!"
           " This program will help you in managing"
           " the hostel booking of Wisdom College \n"
           "Type the number of your option: \n"
           "1. Store details of hosteler \n"
           "2. Check room availability \n"
           "3. Payment Facility \n"
           "4. Search room details of hosteler \n"
           "5. To cancel booking of a hosteler \n"
           "6. Change room type \n"
           "0. Exit the program \n" );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM