iostream obj in the if statement

Hi,
I am pretty curious how the if statement find out true or false from a iostream object as the following:

if (cin)
{
...
}

How could C++ achieve this?
Thanks.
cin is an istream object.

http://cplusplus.com/reference/iostream/istream/

What do you want to achieve with this condition? You may want to use the .good(), .bad(), .fail(), or .eof() functions.

1
2
3
4
if (cin.good()) 
{
   ...
}
sure, I just want to know how it can be re-evaluate to true or false

1
2
3
4
5
6

char a;
if (cin.get(a))
{
    ....
}
In what condition would you expect it to return true or false?

For the get function:
http://cplusplus.com/reference/iostream/istream/get/

This use of the function which you have shown will return the character which was read. This means that if you type ANYTHING, it will return non-zero which means it will always return true.
Last edited on
In c++, any non-zero value is interpreted as true.
The class istream has a conversion member function that converts an istream object to const void *

So then you use cin such a way as

if ( cin )

cin is converted to const void * and then it is compared with zero.
Last edited on
Topic archived. No new replies allowed.