#include <stdio.h>
int main()
{
char n='Y';
fflush(stdin);
while(n=='Y')
{
printf("Add Next Y/N: ");
n=getc(stdin);
}
printf("n = %c",n);
}
This loop ends after 1st iteration without taking input from keyboard.
On my system, getc() does not seem to return until I hit the return key. Which means that ‘Y’ is always followed by ‘\n’. So in order to keep looping I had to add a condition to the while:
fgets() seems to work better:
Edit for comment below:
scanf() also has issues with carriage return. Better to fgets() and then sscanf(). Since you are doing the extra getchar() I think you can get rid of the check for ‘\n’. Try this:
An alternative way without sscanf():