There's a small error in this C code. After the user's input causes the program to enter the while
loop (error handling) - that the while loop iterates twice?!
Something to do with the scanf()
or printf()
?
The output:
Would you like to interact with this program today? (y/n
Please answer the question by typing 'y' or 'n' and hitting enter
Would you like to interact with this program today? (y/n)
Please answer the question by typing 'y' or 'n' and hitting enter
Would you like to interact with this program today? (y/n)
The code:
printf("\nWould you like to interact with this program today? (y/n)\n");
char answer; // recording the user's answer in a character
scanf("%c", &answer);
while ((answer != 'y') && (answer != 'n')) { // keeps looping twice???
printf("\nPlease answer the question by typing 'y' or 'n' and hitting enter\n");
printf("Would you like to interact with this program today? (y/n)\n");
scanf("%c", &answer);
}