int main()
{
chooseflight();
fflush(stdin);
return 0;
}
void chooseflight(void)
{
char selectflight;
printf("a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram\n");
scanf("%c",&selectflight);
switch(selectflight)
{
case 'a':
puts("Welcome to flight 102 service");
while(1){
printmenu102();
}
break;
case 'b':
puts("Welcome to flight 311 service");
printmenu311();
break;
case 'c':
puts("Welcome to flight 444 service");
printmenu444();
break;
case 'd':
puts("Welcome to flight 519 service");
printmenu519();
break;
case 'e':
Quitprogram();
break;
}
}
void printmenu102()
{
char lablename;
printf("a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu\n");
scanf("%c",&lablename);
switch(lablename)
{
case 'a':
Noofemptyseats102();
break;
case 'b':
Listofemptyseats102();
break;
case 'c':
Alphabeticallistofseats102();
break;
case 'd':
Assingseats102();
break;
case 'e':
Deleteseats102();
break;
case 'f':
Quittotopmenu();
break;
}
}
void Quitprogram(void)
{
exit(EXIT_FAILURE);
}
void Quittotopmenu(void)
{
chooseflight();
}
我的输出是:
a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram
a
Welcome to flight 102 service
a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu
a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu
f
a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram
a) Show number of empty seats b) Show list of empty seats c)Show alphabetical list of seats d) Assign a passenger toa seat e)Delete a seat assignment f) Quittotopmenu
子菜单会自动显示而不会停留在主菜单中。
答案 0 :(得分:2)
我希望这可以帮助您解决问题:
int main() {
//When somthing fails return 0
//Else continue
if ( !chooseflight() ) {
return 0;
}
fflush(stdin);
return 0;
}
void chooseflight(void) {
char selectflight;
printf("a) Flight 102 \
b) Flight 311 \
c) Flight 444 \
d) Flight 519 \
e)Quitprogram\n");
scanf("%c",&selectflight);
fflush(stdin);
switch(selectflight) {
case 'a':
puts("Welcome to flight 102 service");
//When somthing fails return 0
//Else Return 1 Success
if ( !printmenu102() ) {
return 0;
} else {
return 1;
}
break;
case 'b':
puts("Welcome to flight 311 service");
printmenu311();
break;
case 'c':
puts("Welcome to flight 444 service");
printmenu444();
break;
case 'd':
puts("Welcome to flight 519 service");
printmenu519();
break;
case 'e':
Quitprogram();
break;
}
}
void printmenu102() {
char lablename;
printf("a) Show number of empty seats \
b) Show list of empty seats \
c)Show alphabetical list of seats \
d) Assign a passenger toa seat \
e)Delete a seat assignment \
f) Quittotopmenu\n");
scanf("%c",&lablename);
fflush(stdin);
switch(lablename) {
case 'a':
Noofemptyseats102();
break;
case 'b':
Listofemptyseats102();
break;
case 'c':
Alphabeticallistofseats102();
break;
case 'd':
Assingseats102();
break;
case 'e':
Deleteseats102();
break;
case 'f':
//Success
return 1;
break;
}
}
void Quitprogram(void)
{
exit(EXIT_FAILURE);
}
void Quittotopmenu(void)
{
chooseflight();
}
答案 1 :(得分:0)
这很有效!我错过了while循环。我的坏!
while(1)
{
printf("a) Flight 102 b) Flight 311 c) Flight 444 d) Flight 519 e)Quitprogram\n");
scanf("%c",&selectflight);
switch(selectflight)
{
case 'a':
puts("Welcome to flight 102 service");
while(1){
printmenu102();
}
break;
case 'b':
puts("Welcome to flight 311 service");
while(1){
printmenu311();
break;
}
case 'c':
puts("Welcome to flight 444 service");
while(1){
printmenu444();
}
break;
case 'd':
puts("Welcome to flight 519 service");
while(1){
printmenu519();
}
break;
case 'e':
Quitprogram();
break;
}
}