tez
tez

Reputation: 5300

testing a condition with istream objects

I was trying to read a lot of numbers dynamically using the code below followed by EOF at the end:

vector<double> data;
double x;
while (cin >> x)
{
    data.pushback(x);
}

this method is working great but i would like to know what is happening with the expression below

cin >> x

Yeah, I know that it is used to input the data but how come I'm able to test conditions with it.
What happens with the expression below:

while (cin) or if (cin)

What does cin return?

Upvotes: 0

Views: 343

Answers (1)

Eitan T
Eitan T

Reputation: 32930

cin an object of class istream class, which can be cast to a void*, and the returned value is a null pointer if an error occured (this pointer is implicitly convertible to bool).

See here for reference.

Upvotes: 3

Related Questions