当程序首次启动时,我可以成功地从主菜单中选择任何选项。但是,当我从任何子菜单中选择返回主菜单选项时,它会返回到主菜单,但无论之后我再按什么选项,它都将继续循环该菜单,仅允许我选择返回主菜单选项。我应该如何重置选择以使其不再继续循环?我尽力缩短了代码,使其仍然可以编译,但也能演示错误。先行致谢。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
//declare all working variables: mOption, FManOption, COption...etc...
int MOption = 0;
int FManOption = 0;
int FOption = 0;
int COption = 0;
int userChoice = 0;
//declarations for all arrays of struct
//declare a pointer to an array of struct using malloc() for fisherman, fish, catch
//process:
printf("Please select 1 to start the program or 0 to quit: ");
scanf("%d", &userChoice);
while(userChoice != 1 && userChoice != 0)
{
printf("Invalid selection! Please type a 1 or a 0: ");
scanf("%d", &userChoice);
}//end (userChoice != 1 && userChoice != 0)
if(userChoice != 1)
printf("Thank you for wasting my time! Have a great day!");
else
{
MOption = mainMenu();
switch(MOption)
{
case 1: FManOption = FishermanMenu();
while(FManOption != 3)
{
switch(FManOption)
{
case 1: getFisherman();//get a fisherman
//count fisherman
break;
case 2: //prompt for a ssn, validate, search
//if found display everything about this fisherman
break;
case 3: FManOption = mainMenu();
//reset FManOption
break;
default: printf("\nInvalid selection! Please select from one of the menu options\n");
}//end switch(FManOption)
}//end while(FManOption != 3)
break;
}
}
}
int mainMenu()
{
int Option;
printf("\n-------Welcome to the Fishing Tournament Main Menu!-------\n");
printf("1 - Fisherman menu\n");
printf("2 - Fish menu\n");
printf("3 - Tournament(Catch) menu\n");
printf("4 - Close Tournament (determine winner)\n");
printf("5 - Quit Program\n\n");
printf("Please select a menu option: ");
scanf("%d", &Option);
if(Option > 5 || Option < 1)
do /* check scanf() return value for input errors */
{
printf("\nInvalid selection! Please select from one of the menu options\n");
printf("1 - Fisherman menu\n");
printf("2 - Fish menu\n");
printf("3 - Tournament(Catch) menu\n");
printf("4 - Close Tournament (determine winner)\n");
printf("5 - Quit Program\n\n");
printf("Please select a menu option: ");
scanf("%d", &Option);
}
while(Option > 5 || Option < 1);
return Option; /* finally return the final correct option */
}//end main menu
int FishermanMenu()
{
int ManOption;
printf("\n-------Fisherman Menu-------\n");
printf("1 - Register fisherman\n");
printf("2 - Search fisherman\n");
printf("3 - Go back to main menu\n");
printf("Please select a menu option: ");
scanf("%d", &ManOption);
if(ManOption > 5 || ManOption < 1)
do /* check scanf() return value for input errors */
{
printf("\nInvalid selection! Please select from one of the menu options\n");/* handle input error */
printf("1 - Register fisherman\n");
printf("2 - Search fisherman\n");
printf("3 - Go back to main menu\n");
printf("Please select a menu option: ");
scanf("%d", &ManOption);
}
while(ManOption > 5 || ManOption < 1);
return ManOption; /* finally return the final correct option */
}//end Fisherman Menu
scanf
而不检查返回值是不安全的。请参阅此页面以获取更多信息:远离 scanf() 的初学者指南 - Andreas WenzelmainMenu
和FishermanMenu
之前,你已经使用了它们(缺少原型声明),并且在这两个函数的定义中,你只指定了参数的名称,但没有指定参数的类型。此外,虽然你似乎试图让这些函数接受一个参数,但你在调用这两个函数时没有传递任何参数。 - Andreas Wenzel-std=c18 -pedantic-errors
进行编译,则不会接受它们。 - Andreas Wenzelint FishermanMenu();
时,它现在不返回我选择的任何选项,尽管我已将其声明为原型。我需要它返回一个值,以便我可以访问其他菜单选项。 - tlbiro