-1

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.

Jinto
  • 179
  • 1
  • 3
  • 17
  • 2
    You are not providing a meaningful **return** from `yes()`. E.g. in the `if (YN == 'Y' || YN == 'y') return 0;` and then in the no block `return menu();` – David C. Rankin Apr 02 '17 at 03:08

2 Answers2

1

Looking through what your code does, this is what occurs when the program is run and someone selects menu option '0':

  • menu() returns 0 and is assigned to menuselection
  • the exit case executes and yes() is run
  • here the user selects 'N' and menu() is called again
  • menu() returns whatever option is selected and nothing is done with it
  • run() reaches end of execution for which behaviour is undefined
  • while loop ends and executes again with menuselection still 0

When you execute your case for exit, you need to signal to your while loop that the user has changed their mind.

Community
  • 1
  • 1
Bash
  • 628
  • 6
  • 19
0
int yes(void) 
{
            char YN;
            begin:;

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

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

            else if (YN == 'Y' || YN == 'y') {
                    printf("Goodbye!\n");
            }

            else {
                    printf("Only (Y)es or (N)o are acceptable: ");
                    goto begin;
            }
}
Jinto
  • 179
  • 1
  • 3
  • 17