J
Jack
Why fflush in the following code does not work?
#include <stdio.h>
int main()
{
int n;
char string[80];
for ( n=0 ; n<12 ; n++ )
{
printf( "Enter some words: " );
scanf( "%s", string );
printf( "The first word you entered is : %s\n", string );
fflush ( stdin );
}
return 0;
}
The execution result is as follows:
Enter some words: this is a test.
The first word you entered is : this
Enter some words: The first word you entered is : is
Enter some words: The first word you entered is : a
Enter some words: The first word you entered is : test.
Enter some words:
It seems to me that the stdin is not flushed. Otherwise, the for loop
should stop after 'this' is printed. Am I right?
Thanks.
#include <stdio.h>
int main()
{
int n;
char string[80];
for ( n=0 ; n<12 ; n++ )
{
printf( "Enter some words: " );
scanf( "%s", string );
printf( "The first word you entered is : %s\n", string );
fflush ( stdin );
}
return 0;
}
The execution result is as follows:
Enter some words: this is a test.
The first word you entered is : this
Enter some words: The first word you entered is : is
Enter some words: The first word you entered is : a
Enter some words: The first word you entered is : test.
Enter some words:
It seems to me that the stdin is not flushed. Otherwise, the for loop
should stop after 'this' is printed. Am I right?
Thanks.