I ran into this problem when I want to check what I entered, a number. The scanf function will return 1 if I successfully cite a number. So here is what I wrote:
int argu;
while(scanf("%d",&argu)!=1){
printf("Please input a number!\n");
}
But when I introduced things like abcd into it, the loop would go on forever and not stop for a hint.
I looked it up on the Internet and found that it has something to do with the cache, and I need to clear it, so scanf can get new data. So I tried fflush, but that didn't work.
Then I saw this:
int argu,j;
while(scanf("%d",&argu)!=1){
printf("Please input a number!\n");
while((j=getchar())!='\n' && j != '\n');
}
Then, when I introduced things like "abcd", it worked well and it requested my data. But when I introduced things like "12ab", this would not work again.
, scanf ( "% d", & arg) , ?
, while(*eptr != '\n').
, fgets '\n' , fget - . .