You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言宿舍管理程序菜单异常:选择0无法返回主菜单求助

问题分析与解决方案

你的问题出在内层循环的逻辑设计上,导致输入0后无法正确返回主菜单,反而重复执行firstChoice()。让我拆解一下问题根源:

核心错误

当你在主菜单选择选项1后,代码执行流程是:

  1. 调用firstChoice()录入信息
  2. 读取_1returnMenu的值
  3. 判断while (_1returnMenu != 0)——如果你此时输入0,这个条件直接为假,内层循环根本不会执行!
  4. 执行外层case 1的break,回到外层while (main_menu_choice !=0)循环
  5. 此时main_menu_choice仍然是你最初选择的1,所以外层循环会再次进入case 1,重新调用firstChoice()

这就是为什么输入0后没有返回主菜单,反而重复执行录入函数的原因。

额外问题:未定义student变量

你的firstChoice()函数中直接使用了student.id、student.name等成员,但代码中没有定义对应的结构体和变量,这会导致编译错误。必须先补充结构体定义。

修正后的代码

下面是修复后的完整代码,我调整了循环逻辑,同时补充了结构体定义:

#include <stdio.h>
#include <stdlib.h>

// 补充student结构体定义
typedef struct {
    int id;
    char name[50];
    char gender[10];
    char roomDetails[100];
    int amountDue;
    int paymentMade;
} Hosteler;

Hosteler student; // 定义全局变量,后续也可以改为局部变量传入函数,更符合编程规范

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:
                // 使用do-while循环确保至少处理一次输入选择
                do {
                    firstChoice();
                    // 将输入提示移到这里,避免函数内重复(也可以保留在函数内,看你的需求)
                    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");
                    scanf("%d", &_1returnMenu);

                    // 处理无效输入
                    while (_1returnMenu != 1 && _1returnMenu != 0) {
                        printf("Invalid option, please try again: \n");
                        scanf("%d", &_1returnMenu);
                    }

                    // 如果选择0,返回主菜单并读取新的选择
                    if (_1returnMenu == 0) {
                        mainMenu();
                        scanf("%d", &main_menu_choice);
                    }
                } while (_1returnMenu == 1); // 只有选择1时才继续录入
                break;

            case 2:
                // 后续功能可以在这里补充实现
                printf("Check room availability feature to be implemented\n");
                mainMenu();
                scanf("%d", &main_menu_choice);
                break;

            case 3:
                printf("Payment Facility feature to be implemented\n");
                mainMenu();
                scanf("%d", &main_menu_choice);
                break;

            case 4:
                printf("Search room details feature to be implemented\n");
                mainMenu();
                scanf("%d", &main_menu_choice);
                break;

            case 5:
                printf("Cancel booking feature to be implemented\n");
                mainMenu();
                scanf("%d", &main_menu_choice);
                break;

            case 6:
                printf("Change room type feature to be implemented\n");
                mainMenu();
                scanf("%d", &main_menu_choice);
                break;

            case 0:
                exit(0);

            default:
                printf("Invalid option, please try again: \n");
                scanf("%d", &main_menu_choice);
                break;
        }
    }
    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);
}

void mainMenu(){
    printf("\nWelcome! 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");
}

关键修改点

  1. 替换内层循环为do-while:确保无论第一次输入什么,都会进入循环处理逻辑,避免输入0时跳过返回主菜单的步骤。
  2. 补充Hosteler结构体定义:解决编译错误,让student变量合法。
  3. 优化输入提示逻辑:把是否继续录入的提示移到main函数的循环中(也可以保留在firstChoice里,看你偏好),让流程更清晰。
  4. 处理无效输入:增加了对非1/0输入的校验,避免无效值导致的逻辑混乱。
  5. 完善其他菜单选项的占位处理:避免选择其他选项时程序无响应。

现在你运行代码,录入信息后输入0,就能正确返回主菜单了。

内容的提问来源于stack exchange,提问作者Bao Hsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:38:48