PDA

View Full Version : fflush and fgetc question



spank
05-24-2007, 04:49 AM
int i;

while(1)
{
fflush(stdin);
i=fgetc(stdin);
printf("%d\n",i);
}

i want to read the first character of the string i enter:
for example if I enter "1\n" it reads both the chars (2 cycles of the while) why doesn't the fflush resolve this?

Salem
05-24-2007, 05:12 AM
> why doesn't the fflush resolve this?
Because it's undefined?
Because you haven't read the FAQ?

spank
05-24-2007, 05:19 AM
ok... i understand now that. but how can I fix it without fflush(stdin) ?

Salem
05-24-2007, 05:39 AM
What do you want to fix?

Perhaps
if ( i != '\n' ) printf("%d\n",i);

Or read a line using fgets(), then parse that line to decide whether you're interested in each character (or not).