Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ how does While( cin >> x) work?

My question is how does,

while(cin>>x)
{
     //code
}

work. Or to be more specific, what about that code halts the loop?

From the documentation here, it looks like the >> operator returns a &istream. Does that mean if the read fails or hits the end of file it not only sets the eofbit, failbit or badbit but also returns a null? That doesn't really make sense so I doubt that's it.

Is there some kind of implicit check of the eofbit?

I ask because I'm hoping to implement something similar with 2 classes like this,

class B
{
   //variables and methods
}

class A
{
   //Variables and methods
   //Container of B objects. ex. B[] or vector<B> or Map<key,B>
   &A >> (B b);
}

int main()
{
   A a;
   B b;

   while( a >> b)
   {
      //Code
   }

}

Note: I'm not looking to inherit from istream unless it's where the magic that makes this work come from. The reason for this is I'm hoping to keep the classes and their dependencies as small as possible. If I inherit from istream I will receive all its public and protected stuff and I'm not trying to create an istream like object. I just want to copy one REALLY nice piece of it.

Edit: I'm using Visual Studio 2010, (which is becoming a real pain) and I'm going to need something compatible with it's implementation of C++03 + some C++11.

like image 748
Dan Avatar asked Oct 16 '12 21:10

Dan


People also ask

What does while CIN X mean?

cin >> number) is a test of the stream returned by cin >> number , and thanks to iostream 's operator ! the stream state is tested for failure. If the stream is in a fail state, this results in true. while (!( cin >> number)) will enter the body of the loop if the read fails or has already failed.

Can you put CIN in a while loop?

Well, you use cin inside a while loop when you need the user to enter a value that will be used after that, in the same loop. Everytime the loop runs, the user can chose something else. For example, calculating x*y.

What does cin >> n mean?

cin is the standard input stream. Usually the stuff someone types in with a keyboard. We can extract values of this stream, using the >> operator. So cin >> n; reads an integer. However, the result of (cin >> variable) is a reference to cin .

What does while CIN mean in C++?

The statement while (cin) means "while all previous operations on cin have succeeded, continue to loop." Once we enter the loop, we'll try to read a value into y .


2 Answers

From the documentation here, it looks like the >> operator returns a &istream. Does that mean if the read fails or hits the end of file it not only sets the eofbit, failbit or badbit but also returns a null?

No, a reference to the stream object is returned, but the internal flags will be set. Subsequent calls to operator>> will fail after just testing those flags.

Is there some kind of implicit check of the eofbit?

Not sure what the question is. The stream will not try to find upfront whether the next request will fail, but only set the flag if one operation failed because it hit EOF. Each call to operator>> will first test the state of the stream, and will only proceed if the stream is in a good condition.

How does while(cin>>x) work?

There is an implicit conversion from the stream object to a boolean. The conversion yields true if the stream is in a good condition or false otherwise.

like image 66
David Rodríguez - dribeas Avatar answered Sep 25 '22 13:09

David Rodríguez - dribeas


Now could you write up an example of how I can do that with the above stuff?

Like this:

// UNTESTED
class B
{
   //variables and methods
}

class A
{
   bool still_good;

   //Variables and methods
   //Container of B objects. ex. B[] or vector<B> or Map<key,B>
   A& operator>>(B& b) {
     try_to_fill_b();
     if(fail) still_good = false;
     return *this;
   }
   explicit operator bool() { return still_good; }
}

int main()
{
   A a;
   B b;

   while( a >> b)
   {
      //Code
   }

}
like image 27
Robᵩ Avatar answered Sep 25 '22 13:09

Robᵩ