Mail Archives: djgpp/1994/09/01/12:13:29
----- Begin Included Message -----
>From pjones AT acs DOT bu DOT edu Thu Sep 1 11:24 EDT 1994
Thanks for the quick reply. However I don't agree. You said:
This is the correct behavior of scanf(); it will work that way on all
C compilers.
This is not the case with SUN's C compiler (at least the one that came
with SunOS 4.3.2), nor is it the case with Microsoft C 6.00A.
The problem with your test program is that when scanf() encounters an
illegal character according to its format specification, it will not
remove it from the input stream, so you are continually trying to scan
the same invalid string. One way of fixing this would be to call
"fflush(stdin)" after when you scanf() has failed, or before every
scanf() call, whichever is more appropriate...
This is a good suggestion, especially since it won't break my code on
machines where scanf() behaves in the other way. Unfortunately, it
doesn't work. Modifying my example as you suggested (see below) makes
no difference.
Thanks again
David Ronis
#include <stdio.h>
main()
{
while(1)
{
int i,j;
j=scanf("%d",&i);
printf("i=%d; j=%d\n",i,j);
if(j!=1)fflush(stdin); /* YOUR SUGGESTION */
}
}
- Raw text -