gpt4 book ai didi

c - 我正在尝试用 C 语言制作一个菜单,但我的代码没有按照我想要的方式工作

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:46 24 4
gpt4 key购买 nike

我正在为我的编程课做作业,我处理的第一件事当然是制作主菜单。但出于某种原因,该程序没有按我预期的方式运行。

#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 details of hosteler”时,只需输入一些随机信息,在输入所需信息后,程序会询问我是否要继续使用它与否。如果我按 1,它会要求我再次填写所需信息,如果我按 0,它应该返回主菜单。但问题是,当我输入 0 时,它不会返回主菜单,而是再次运行 firstChoice() 函数,而不是在 switch 语句中运行 case 0。我一直在挠头 2 个小时,试图找出问题所在,但我似乎无法找到问题所在。如果我无法正确表达我的问题,我感到非常抱歉,在此先感谢您!

最佳答案

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

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" );
}

关于c - 我正在尝试用 C 语言制作一个菜单,但我的代码没有按照我想要的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com