0

scanf is not skipped. It waits for me to enter input, but I have to enter the value and hit enter twice (the second input is properly saved).

int yes(void)
{
    int yes, rc, isValid;
    char answer, nl = 'a';

    do {
        clearKeyboard();
        rc = scanf(" %c%c", &answer, &nl);
        if (nl == '\n')  { //|| rc != 2
            if ((answer == 'y') || answer == 'Y') {
                yes = 1;
                isValid = 1;
            }
            else if ((answer == 'n') || answer == 'N') {
                yes = 0;
                isValid = 1;
            }
            else {
                printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
                isValid = 0;
                clearKeyboard();
            }
        }
        else {
            printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
            isValid = 0;
            clearKeyboard();
        }
    } while (!isValid);

    return yes;
}
H sub
  • 1
  • 2

1 Answers1

0

I recommend not using scanf, but since you are: what is the purpose of nl? If you remove the second %c in the scanf you would get your answer and only need to enter once.

Burstful
  • 347
  • 1
  • 10