-1

我想在用户选择“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;
    }
4

1 回答 1

0

从您的代码中,有两点需要考虑。一,menu函数不必返回 an int,但可以是voidie 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条件以使用一个标志来终止循环。此外,另一项建议是将字符数限制sel6 个字符,如scanf("%6s", sel);

于 2013-04-07T02:31:04.980 回答