0

I've written a function yes(); that is used to confirm if the user wants to exit the program. If 'Y' or 'y' is entered it works fine and the user exits and if 'N' or 'n' is entered it also works fine with the user returning to the menu to select another option however, if anything aside from those characters are entered an error message displays informing the user that only (Y)es or (N)o are acceptable however, the message does not provide the user another opportunity to input a valid option, but instead proceeds to display the menu. Here's my code:

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;
}
Jinto
  • 179
  • 1
  • 3
  • 17
  • 2
    There must be thousands of duplicates to this, but since they are all badly titled it makes them very hard to find. The problem is that when you press the `Enter` key, that key is also put in the input buffer together with the character, as a newline. Now think a little what happens when you next time call `scanf` to read a character, what character do you think it will read? You would have solved this very quickly yourself if you just used a debugger to step through the code line by line. Please do that first in the future. – Some programmer dude Apr 01 '17 at 15:31
  • Looks like the perfect case for a `do ... while` loop. – Siguza Apr 01 '17 at 15:32
  • Probably want to read this: [`scanf()` leaves the new line char in buffer?](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer). There are *thousands* of duplicates of this problem. – WhozCraig Apr 01 '17 at 15:32
  • @WhozCraig Ah, not so hard after all... :) – Some programmer dude Apr 01 '17 at 15:33
  • 2
    @Someprogrammerdude only because it's the third time in the last hour I've seen near-identical posts. – WhozCraig Apr 01 '17 at 15:33
  • 1
    `exit is a reserved name in the standard library. You should not use it, even at block scope. – too honest for this site Apr 01 '17 at 15:43
  • I'm going to try changing it to a do while loop like Siguza mentioned since I added a space after %c, but was still being met with problems. – Jinto Apr 01 '17 at 16:29

0 Answers0