Using cin with while

Sep 30, 2012 at 4:31am
Can anyone please explain the following code ?

1
2
3
4
char buffer[10]; //declares a char array of length 10
while(cin >> buffer){ //?
 cout << buffer ; //prints the buffer on standard output
}
Sep 30, 2012 at 7:15am
Edit; heh
Last edited on Sep 30, 2012 at 7:52am
Sep 30, 2012 at 7:28am


while(cin >> buffer){

}

cin >> buffer is a statement. While loops can take either statements or conditionals. A statement always evaluates to true. So what is happening is the while loop keeps running the statement, which evaluates to true, and you end up with an infinite loop.

If you want a useful answer some more context would be helpful. Where did you get the code form?
Sep 30, 2012 at 5:49pm
Initially ,I thought some kind of comparison is taking place.
But here, it means that the statement cin >> buffer will always evaluate to true .
That means for compiler code above is equivalent to
1
2
3
4
while(true){
  cin >> buffer ;
  cout << buffer ;
}

Thanks for replying , the question was too basic
Topic archived. No new replies allowed.