I am working on a simple program in C that asks the user to give an input in a loop until they enter the letter Q when prompted. When I run the program however, it immediately skips the prompt to enter a character and asks the user to enter another input. Any idea why this happens and how I can fix it?
Here is my code:
while (exit != 'q' && exit != 'Q') {
printf("Please enter the grade for assignment #%d: ", assignmentNumber);
scanf("%lf", &gradeInput);
printf("Press q to quit, press any other character to continue: ");
exit = getchar();
}
I tried changing the getchar to scanf(%c%*c) like I saw someone suggest under a post with a similar problem. It did make it so the prompt to enter a character actually worked, but the loop no longer ended when Q is entered.
scanf()andgetchar(). Preferably use exclusivelyfgets()for ALL user input (for the grade, for the quit character, ...).scanf()was not designed for user input;getchar()alone can be a PITA;fgets()works likegetchar()on steroids.