just a Q
Moderator: Board moderators
specify your programming language please
In C you may use feof(stdin)
in C++ cin.eof()
but be careful, whitespaces at the end of input may cause problems.
Usually the best way of EOF detection is when reading the next element fails.
E.g.:
while (scanf("%d",&N) == 1) { printf("read %d\n",N); }
while (cin >> N) { cout << "read " << N << endl; }

In C you may use feof(stdin)
in C++ cin.eof()
but be careful, whitespaces at the end of input may cause problems.
Usually the best way of EOF detection is when reading the next element fails.
E.g.:
while (scanf("%d",&N) == 1) { printf("read %d\n",N); }
while (cin >> N) { cout << "read " << N << endl; }
ywceric wrote:i am using C.
i've tried ""while (scanf("%d",&N) == 1) { printf("read %d\n",N); }"", but seems it doesn't work... even i input nothing and press enter, it doesn't stop.
how to use feof (sdtin)??
i only how to use feof(file)...
Code: Select all
#include <stdio.h>
int N;
int main()
{
while (scanf("%d",&N)==1)
{
printf("read %d\n",N);
}
return 0;
}
Code: Select all
if feof(stdin)
{
break;
}
Learn, learn, learn.
Cahoun: You probably misunderstood his current problem. The problem is: Even if you press Enter, the current input didn't end yet. The scanf function skips the whitespace you enter. Only after an explicit EOF (try typing Ctrl-D) the call of scanf() fails.ywceric wrote:i am using C.
i've tried ""while (scanf("%d",&N) == 1) { printf("read %d\n",N); }"", but seems it doesn't work... even i input nothing and press enter, it doesn't stop.
how to use feof (sdtin)??
i only how to use feof(file)...
Maybe a more clear way is to redirect the input from a file, i.e. run
my_program < file.in
The stdio of your program will be replaced by the contents of file.in
Everything should behave exactly the way you are used to with files (except that you use scanf and printf instead of fscanf and fprintf).