#include <stdio.h>
int main(){
char quit = 'n';
do{
printf("Quit? (Y/N)");
scanf("%c", &quit);
}while(quit=='n' || quit=='N');
}
Why does my program quit after inputting anything?
The %c format specifier accepts any character, including newlines. So if you press N, then scanf reads that character first but the newline from pressing ENTER is still in the input buffer. On the next loop iteration the newline character is read. And because a newline is neither n or N the loop exits.
You need to add a space at the start of your format string. That will absorb any leading whitespace, including newlines.
scanf(" %c", &quit);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With