简体   繁体   English

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

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

I'm working on an assignment for my programming class and the first thing I tackled is of course to make the main menu. 我正在为我的编程课分配作业,我要做的第一件事当然是制作主菜单。 But for some reason, the program is not working the way I intend 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;
}

Okay so when I run the program and I choose the first option which is "1.Store details of hosteler" then just put on some random information and after inputting the required info the program will ask me if I want to continue using it or not. 好的,所以当我运行程序并选择第一个选项“ 1.Store hosteler的详细信息”时,只需输入一些随机信息,在输入所需信息后,程序将询问我是否要继续使用它。 If I press 1 it will ask me to fill up the required information again and if I press 0 it should go back to the main menu. 如果我按1,它将要求我再次填写所需的信息,如果我按0,则应返回主菜单。 But the thing is, when I type in 0 it doesn't go back to the main menu, instead it just runs the firstChoice() function again instead of running the case 0 in the switch statement. 但问题是,当我输入0时,它不会返回主菜单,而是仅再次运行firstChoice()函数,而不是在switch语句中运行case 0。 I've been scratching my head for 2 hours trying to figure out what's wrong but I just cant seem to find what's wrong. 我已经花了两个小时挠头试图找出问题所在,但我似乎无法找到问题所在。 I'm so sorry if I wasn't able to word my problem properly, and thanks in advance! 如果无法正确表达我的问题,我们深感抱歉,并提前致谢!

the following proposed code shows how to write the menu display and handling 以下建议的代码显示了如何编写菜单显示和处理

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.

相关问题 我正在尝试在C中运行代码块,但未执行我期望/想要的方式 - I am trying to run a block of code in c but it doesn't execute the way I expect/want 我正在尝试在 Ubuntu 12.04.1 中进行系统调用。 我在编译 hello.c 文件时遇到错误 - I'm trying to make a system call in Ubuntu 12.04.1. I'm running into an error compiling my hello.c file 我正在尝试使用4向循环展开来优化此c代码 - I'm trying to optimize this c code using 4-way loop unrolling 我想创建一个程序来使用C中的字符串函数比较两个字符串。我的代码有什么问题?我是C的初学者 - I want to create a program to compare two strings using string functions in C.What is wrong in my code?I'm a beginner in C 我正在尝试使数组在 c 中打印出来 - I'm trying to make an array print itself in c 我正在尝试在 C 中制作 fork() 进程调用的图表 - I'm trying to make a diagram of fork() process calls in C 我正在尝试在此C代码中找到队列的安全漏洞 - I'm trying to find the security holes in this C code for a queue 我想使用计数排序在 C 中打印一个排序数组,但我的代码不起作用 - I want to print a sorted array in C using count sort but my code is not working 我想用ASCII码更新我的C代码 - I want to update my C code with ASCII code 我正在尝试制作自定义 pow() function 但我的 for 循环不起作用 - C - I am trying to make a custom pow() function but my for loops are not working - C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM