我正在为我的编程课做作业,我要做的第一件事当然是制作主菜单。但是由于某种原因,该程序无法按照我的预期方式工作。
#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.hosteler的详细信息”,然后输入一些随机信息,在输入所需的信息后,程序将询问我是否要继续使用是否。如果我按1,它将要求我再次填写所需的信息,如果我按0,则应返回主菜单。
但问题是,当我输入0时,它不会返回主菜单,而是仅再次运行firstChoice()
函数,而不是在switch语句中运行case 0。我已经花了两个小时挠头试图找出问题所在,但我似乎无法找到问题所在。如果无法正确表达我的问题,我感到非常抱歉,并在此先感谢!
答案 0 :(得分: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" );
}