hi, everyone i want to ask about EOF in C++

when i learn C to use EOF in my code is like this :

while (scanf("%d",&a) != EOF){
   statement
   ...
}

but, when i use in C++ {

while ((cin >> a) != EOF){
   statement
   ...
}

it's having an error. So my question is how can i use EOF in C++ if the code's like that?

I'm sorry it's a newbie question, because i can't find the answer >.<

Take out the comparison completely. If cin returns EOF, it should automatically terminate.

while (cin >> a){
   statement
   ...
}

is a completely valid construct.

The only thing you have to watch for is the data type of "a" (which appears to be numeric, per your C example). If the value entered for "a" is zero (0), it would terminate.

Take out the comparison completely. If cin picks up EOF, it will automatically terminate.

while (cin >> a){
   statement
   ...
}

is a completely valid construct.

The only thing you have to watch for is the data type of "a". If the value entered for "a" is zero (0), it would terminate.

ooo..

so in C++ there's no need to put EOF anymore like in C..

Ok.. Thank You.

Comparison to the macro EOF still has its uses in other contexts, but it really isn't used much because of how it's defined (generally as the value -1 which can lead to fake/invalid EOF detections).

You'll probably encounter EOF (End of File) when using file streams though. It is then as simple as a bool stream.eof() to use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.