samedi 1 avril 2017

Else if statement ending program

I've attempted to write a function yes that confirms if the user wants to exit the program. If the user enters 'Y' or 'y' it exits successfully and if the user enters an invalid input it also exits successfully however, if the user enters 'N' or 'n' it will return them to the menu, but any further input will simply end the program. I've tried a do while loop as well, but was met with similar issues. Here's my code:

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

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

    while (menuSelection != 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;
        begin:;

        scanf("%c", &YN);
        flushKeyboard();

        if (YN == 'Y' || YN == 'y') {
        }

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

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

int menu(void) {
    int option;
    int firstSelection = 0;
    int lastSelection = 7;
    printf("\n1- List all items\n");
    printf("2- Search by SKU\n");
    printf("3- Checkout an item\n");
    printf("4- Stock an item\n");
    printf("5- Add new item or update item\n");
    printf("6- Delete item\n");
    printf("7- Search by name\n");
    printf("0- Exit program\n");
    printf("> ");
    option = getIntLimited(firstSelection, lastSelection);
    return option;
}

I can provide the rest of the code if necessary.

Aucun commentaire:

Enregistrer un commentaire