gpt4 book ai didi

在另一个函数中调用菜单函数

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

我试图在主函数中调用菜单函数并让它提示,直到用户决定退出,但它似乎没有给我响应。我是这个网站编码的新手,如果有什么,请告诉我。帖子格式我可以改进!

清除键盘功能:

void clearKeyboard(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF);
}

调用菜单的函数:

void ContactManagerSystem(void)
{
int contactchoice;
int done = 1;
char yesno, c;
do {
clearKeyboard();
contactchoice = menu();
switch (contactchoice) {
case 1:
printf("<<< Feature 1 is unavailable >>>\n");
break;
case 2:
printf("<<< Feature 2 is unavailable >>>\n");
break;
case 3:
printf("<<< Feature 3 is unavailable >>>\n");
break;
case 4:
printf("<<< Feature 4 is unavailable >>>\n");
break;
case 5:
printf("<<< Feature 5 is unavailable >>>\n");
break;
case 6:
printf("<<< Feature 6 is unavailable >>>\n");
break;
case 0:
printf("Exit the program? (Y)es/(N)o: ");
scanf("%c%c", &yesno, &c);
if (yesno == 'Y' || yesno == 'y' && c == '\n') {
done = 0;
break;
}
else if (yesno == 'N' || yesno == 'n')
break;
default:
break;
}

} while (done == 1);
}

菜单功能:

int menu(void)
{
int done = 1;
int choice;
char c;
do {
printf("Contact Management System\n");
printf("-------------------------\n");
printf("1. Display contacts\n");
printf("2. Add a contact\n");
printf("3. Update a contact\n");
printf("4. Delete a contact\n");
printf("5. Search contacts by cell phone number\n");
printf("6. Sort contacts by cell phone numbe\n");
printf("0. Exit\n\n");
printf("Select an option:> ");
int rtn = scanf("%d%c", &choice, &c);

if (rtn == EOF || rtn == 0 || c != '\n')
clearKeyboard();
else if (choice >= 0 && choice <= 6 && c == '\n')
done = 0;
else {
clearKeyboard();
printf("*** OUT OF RANGE *** <Enter a number between 0 and 6>: ");
scanf("%d", &choice);
}
} while (done == 1);

return choice;
}

下图是错误的地方,正确的版本应该做案例 1 而不是再次保留提示菜单。

不正确的部分

enter image description here

提前致谢!!!

最佳答案

你的代码有两个问题。

  1. menu() 函数返回 int 并且您在 switch case case '1' 中将 intchar 匹配: 应该是case 1:

  2. 替换下一行

scanf("%c%c", &yesno,&c);scanf("%c%c", &c,&yesno);

希望对你有帮助。

关于在另一个函数中调用菜单函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49397889/

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