#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?
#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);
Just change your code to:
#include <stdio.h>
int main(){
char quit = 'n';
do{
printf("Quit? (Y/N)");
scanf(" %c", &quit);
}while(quit=='n' || quit=='N');
}
For more information read this link