我希望在用户选择“VIEW”或“BID”之后再次打印菜单..如果没有无限循环,我该如何做?我将菜单设置为自己的功能,然后在我的“主”功能中调用它。
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;
}
答案 0 :(得分:0)
从您的代码中,有两点需要考虑。一个menu
函数不需要返回int
,但可以是void
,即void 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);