Home > Software design >  Why does my loop request usable character twice?
Why does my loop request usable character twice?

Time:07-26

So I have a very simple code to ask the user if they agree or not (i'm just starting out). Currently I have the loop set up to allow the user to answer with 'y' or 'n', and to tell them "only y/n please" if they enter a different character. So far, everything works almost perfectly, however, upon entering a character other than 'y' or 'n', the correction message displays twice. any advice?

code:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    char answer = get_char("Do You Agree? y/n \n"); // prompt user to answer question

    do{
        scanf("%c", &answer);

        if (answer == 'y')
        {
            printf("Great!\n");
            break;
        }
        else if(answer == 'n')
        {
            printf("That's unfortunate\n");
            break;
        }
        else
        {
            printf("only y/n please.\n"); // prompt user for acceptable character
        }
    }while (answer != 'y' || answer != 'n');
}

And here is what it returns:

Do You Agree? y/n 
a
a
only y/n please.
only y/n please.
a
only y/n please.
only y/n please.
a
only y/n please.
only y/n please.
s
only y/n please.
only y/n please.
y
Great!

CodePudding user response:

You don't need to use scanf(), you already asked for the input with get_char(). Just move that inside the loop in place of the scanf().

You don't need to check answer in the while() condition, since you break out of the loop when it's y or n.

int main(void)
{
    char answer;

    while (1) {
        answer = get_char("Do You Agree? y/n \n"); // prompt user to answer question

        if (answer == 'y')
        {
            printf("Great!\n");
            break;
        }
        else if(answer == 'n')
        {
            printf("That's unfortunate\n");
            break;
        }
        else
        {
            printf("only y/n please.\n"); // prompt user for acceptable character
        }
    }
}

CodePudding user response:

Ask the question with printf(), then loop until user types 'y' or 'n'...

printf( "Do You Agree? y/n \n" );
for( ;; ) {
    char answer[32];
    scanf("%s", &answer); // %s will swallow '\n'...

    if (answer[0] == 'y')
    {
        printf("Great!\n");
        break;
    }

    if(answer[0] == 'n')
    {
        printf("That's unfortunate\n");
        break;
    }

    printf("only y/n please.\n");
}

CodePudding user response:

The CS50 function get_char will extract a whole line of input from the input stream and then return the first character of that input., assuming that there was only a single character on the line. Otherwise, it will reprompt the user for input.

The function call

scanf("%c", &answer);

on the other hand will only extract a single character from the input stream, not a whole line, without performing any input validation. The character that scanf extracts can also be a '\n' newline character, which marks the end of the line.

The sequence of events that are occurring is the following:

  1. You call get_char, which extracts 'a' and '\n' from the input stream, and returns 'a'.
  2. You call scanf, which extracts 'a' from the input stream, but leaves '\n' on the input stream.
  3. Your program detects that the user did not enter 'y' or 'n', so it prints "only y/n please.\n".
  4. You call scanf again, which simply extracts the '\n' that was left over by the previous call to scanf, so scanf does not obtain any new input from the user.
  5. Your program incorrectly thinks that the user entered '\n' instead of 'y' or 'n', so it prints another error message.

In order to solve the problem, I recommend that you only use get_char and don't mix it with scanf. In contrast to scanf, the function get_char will never return a '\n' character. Therefore, the function get_char is much easier to use.

Since you want to call get_char more than once if necessary, you should move that function call inside the loop, so that it replaces the call to scanf.

  • Related