如何使用 Switch 语句将菜单函数的 Int 值传回 Main 以访问其他菜单函数?
How Can I Pass a Menu Function Int Value Back To Main To Access Other Menu Functions Using Switch Statement?
我正在为学校项目的一部分做这件事,我很迷茫,这只是它的开始。我们的教授希望我们有 4 个菜单功能。每个菜单都有访问程序中其他功能的选项。首先,如果我们想开始或退出,我们会被要求有程序状态。那没问题。我的问题是当我 运行 主菜单功能和 select 一个选项时,我无法选择 return 到 main 到 运行 开关盒以访问其他菜单。现在我的所有其他菜单都写着“即将推出……”,这样我就知道我做对了。一旦我通过这部分,我会添加更多。这是我第一次 post 来到这里,如果 post 的代码太多,我深表歉意。我非常感谢任何帮助。谢谢。
#include <stdio.h>
#include <stdlib.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;
int n = mainMenu();
switch(n)
{
case 1: while(FManOption != 3)
{
FManOption = FishermanMenu();
switch(FManOption)
{
case 1: //get a fisherman
//count fisherman
break;
case 2: //prompt for a ssn, validate, search
//if found display everything about this fisherman
break;
case 3: //hit any key to go back to main menu
//reset FManOption
break;
default: "error!";
}//end switch(FManOption)
}//end while(FManOption != 3)
break;
default: printf("error");
}
return 0;
}
int mainMenu()
{
int option = 0;
printf("-------Welcome to the Fishing Tournament Main Menu!-------\n\n");
do
{
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: ");
if (scanf("%d", &option) != 1) /* check scanf() return value for input errors */
{
/* handle input error */
return -1;
}
} while (option < 1 || option > 5); /* check the range of option ( 1 - 5) */
return option; /* finally return the final correct option */
}
int FishermanMenu()
{
printf("Coming soon...");
/*
-1-Register fisherman
-2-Search fisherman
-3-Go back to main menu
*/
//FManOption
}//end Fisherman Menu
你的 mainMenu()
函数除了 0 没有 return 任何东西,此外你忽略了 main 中的 return 值,当你调用 mainMenu();
(顺便说一句,这甚至不应该编译),这可能就是你要找的,
int mainMenu(void)
{
int option = 0;
printf("-------Welcome to the Fishing Tournament Main Menu!-------\n\n");
do
{
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: ");
if (scanf("%d", &option) != 1) /* check scanf() return value for input errors */
{
/* handle input error */
return -1;
}
} while (option < 1 || option > 5); /* check the range of option ( 1 - 5) */
return option; /* finally return the final correct option */
}
int main(void)
{
int n = mainMenu(); /* save the result to n */
/* your code */
return 0;
}
我正在为学校项目的一部分做这件事,我很迷茫,这只是它的开始。我们的教授希望我们有 4 个菜单功能。每个菜单都有访问程序中其他功能的选项。首先,如果我们想开始或退出,我们会被要求有程序状态。那没问题。我的问题是当我 运行 主菜单功能和 select 一个选项时,我无法选择 return 到 main 到 运行 开关盒以访问其他菜单。现在我的所有其他菜单都写着“即将推出……”,这样我就知道我做对了。一旦我通过这部分,我会添加更多。这是我第一次 post 来到这里,如果 post 的代码太多,我深表歉意。我非常感谢任何帮助。谢谢。
#include <stdio.h>
#include <stdlib.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;
int n = mainMenu();
switch(n)
{
case 1: while(FManOption != 3)
{
FManOption = FishermanMenu();
switch(FManOption)
{
case 1: //get a fisherman
//count fisherman
break;
case 2: //prompt for a ssn, validate, search
//if found display everything about this fisherman
break;
case 3: //hit any key to go back to main menu
//reset FManOption
break;
default: "error!";
}//end switch(FManOption)
}//end while(FManOption != 3)
break;
default: printf("error");
}
return 0;
}
int mainMenu()
{
int option = 0;
printf("-------Welcome to the Fishing Tournament Main Menu!-------\n\n");
do
{
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: ");
if (scanf("%d", &option) != 1) /* check scanf() return value for input errors */
{
/* handle input error */
return -1;
}
} while (option < 1 || option > 5); /* check the range of option ( 1 - 5) */
return option; /* finally return the final correct option */
}
int FishermanMenu()
{
printf("Coming soon...");
/*
-1-Register fisherman
-2-Search fisherman
-3-Go back to main menu
*/
//FManOption
}//end Fisherman Menu
你的 mainMenu()
函数除了 0 没有 return 任何东西,此外你忽略了 main 中的 return 值,当你调用 mainMenu();
(顺便说一句,这甚至不应该编译),这可能就是你要找的,
int mainMenu(void)
{
int option = 0;
printf("-------Welcome to the Fishing Tournament Main Menu!-------\n\n");
do
{
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: ");
if (scanf("%d", &option) != 1) /* check scanf() return value for input errors */
{
/* handle input error */
return -1;
}
} while (option < 1 || option > 5); /* check the range of option ( 1 - 5) */
return option; /* finally return the final correct option */
}
int main(void)
{
int n = mainMenu(); /* save the result to n */
/* your code */
return 0;
}