what does while(std::cin) mean?

I read in my book find this, and I can't understand it:
1
2
3
4
5
6
while(std::cin) {
....
// a input operation
}

...


there'no input operation before this code, I'm wondering what does while(std::cin) do? exam the input in the block?
Just to be sure: 'cin' is a variable declared in the 'std' namespace and usually (or always, I don't really know but I don't think it's important) found in the iostream header file.

If I recall correctly, the specific use being given here is to check whether the input stream associated with this variable is good to go or not. In other words, that's the same as while (std::cin.good()). How is this possible? By means of an operator override. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
namespace std
{
    class istream
    {
        ...
    public:
        operator bool()
        {
            return good();
        }
    };
}


Hopefully I got that syntax right. It's been a year since I don't touch C++.
Last edited on
I also think the condition is to exam the input is correct, but the while(std::cin) is in a main() funciton, there's no input operation except in the while, what's input does the while exam? the inside? how a a inside input stream go to while() ?


It allows the user to enter as many variable as they want as long as the cin is the type of the variable.
The loop will return false or break if the user enter a different type.

e.g.
1
2
3
4
5
6
7
8
9
int a;
string b;
cout<<" enter a\n";
while(cin>>a)
{
//keep doing this stuff
//if they enter b- stop this stuff
//i.e user's input cannot be converted to the type of the right hand operand of >>. 
}

but in my case, the condition is not
cin >> a
, it just
while(std::cin)
, no variable, what's does this mean?
It just checks the input stream. The loop continues while the stream is in a good state. That is, while EOF has not been encountered and it's still open, ...

I would expect something to read from std::cin in the loop. But it's not common practice to write code like that.
thanks, bkw!
Topic archived. No new replies allowed.