gpt4 book ai didi

c - 选择菜单选项后再次打印菜单。 ."do while"选择选项后循环无限

转载 作者:行者123 更新时间:2023-11-30 19:49:02 24 4
gpt4 key购买 nike

我想在用户选择“查看”或“出价”后再次打印菜单。我该如何在没有无限循环的情况下做到这一点?我将菜单设置为其自己的函数,然后在我的“主”函数中调用它。

    int menu() {
char sel[6];

printf("Welcome to the Silent Auction!\n");
printf("Please make a selection from the following:\n");
printf("View Auction [VIEW]\n");
printf("Bid on Auction [BID]\n");
printf("Close Auction [CLOSE]\n");

return;
}


menu();
char sel[6];
scanf("%s", &sel);

do {

if (strcmp("VIEW", sel) == 0) {
...
}
if (strcmp("BID", sel) == 0) {
printf("Which auction would you like to bid on?\n");
scanf("%d", &choice);
if ...
} else {
...
} printf("How much would you like to bid?\n");
scanf("%f", &user_bid);
if ...
else
cur_bid[choice] += user_bid;
}
if (strcmp("CLOSE", sel) == 0) {
for...
}

} while (sel != "CLOSE");




return 0;
}

最佳答案

从您的代码来看,有两点需要考虑。一、menu函数不需要返回 int ,但可能是 voidvoid menu() { 。由于您没有阅读此函数内的选择,char sel[6]是多余的。

下一步,为了实现你的目标,在 while 之前语句,您可以调用下一个调用 menu如下图

int     close_flag = 0;

printf("Enter your choice, VIEW / BID / CLOSE\n");
scanf("%6s", sel);

printf("Entered Choice: %s\n", sel);

do {
if(!strcmp(sel, "VIEW"))
{
printf("ENTERED VIEW\n");
}
if(!strcmp(sel, "BID"))
{
printf("BIDDING\n");
}
if(!strcmp(sel, "CLOSE"))
{
printf(">>>>CLOSING \n");
close_flag = 1;
}
if(!close_flag)
{
printf("Enter your choice, VIEW / BID / CLOSE\n");
scanf("%6s", sel);
printf("Entered Choice: %s\n", sel);
}
} while(!close_flag);

我修改了while使用标志来终止循环的条件。此外,另一项建议是限制 sel 的字符数。到 6 个字符,如 scanf("%6s", sel); 所示

关于c - 选择菜单选项后再次打印菜单。 ."do while"选择选项后循环无限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858206/

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