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!