是/否函数无效条目不循环

时间:2017-04-01 15:28:01

标签: c function loops

我写了一个函数yes();用于确认用户是否要退出程序。如果输入'Y'或'y',它工作正常,用户退出,如果输入'N'或'n',它也可以正常工作,用户返回菜单选择另一个选项,但是,除了那些之外的任何其他选项输入字符错误消息显示通知用户仅可接受(Y)es或(N)o但是,该消息不向用户提供另外输入有效选项的机会,而是继续显示菜单。这是我的代码:

void GroceryInventorySystem(void) {
    int menuSelection;
    int exitSelection = 0;

    welcome();
    printf("\n");

    while (exitSelection == 0) {
        menuSelection = menu();
        switch (menuSelection)
    {
        case 1:
        printf("List Items under construction!\n");
        pause();
        break;

        case 2:
            printf("Search Items under construction!\n");
        pause();
        break;

        case 3:
        printf("Checkout Item under construction!\n");
        pause();
        break;

        case 4:
        printf("Stock Item under construction!\n");
        pause();
        break;

        case 5:
        printf("Add/Update Item under construction!\n");
        pause();
       break;

        case 6:
        printf("Delete Item under construction!\n");
        pause();
        break;

        case 7:
        printf("Search by name under construction!\n");
        pause();
        break;

        default:
        printf("Exit the program? (Y)es/(N)o: ");
        exitSelection = yes();
        break;
        }
    }
}

int yes(void) {
            char YN;
        int exit = 0;

        scanf("%c", &YN);

            if (YN == 'Y' || YN == 'y') {
                exit = 1;
            }

            else if (YN == 'N' || YN == 'n') {
                exit = 0;
            }

            else {
                printf("Only (Y)es or (N)o are acceptable: \n");
            }

            return exit;
}

0 个答案:

没有答案