0

I don't quite understand the working and the use of this code.

I have tried giving 'null' as an input to terminate this while loop, but still not sure how it functions in a program.

while (cin >> x) {
  code
}
Mat
  • 202,337
  • 40
  • 393
  • 406

1 Answers1

-3

The definition of the std::cin's "operator >>" is (roughly):

template<class T>
istream& operator>> (const T& val);

After calling '>>' an istream& will always be returned which will than evaluate to 'true' in the while loop. Therefor this program will not terminate until some type of exception is thrown.

  • or a break statement e.g. `if (x == 5) break;` – CurtisJC Jun 11 '19 at 19:37
  • 2
    This is not correct. See the linked answer. The conversion to `bool` yields the state of the stream, so the program will certainly terminate under normal conditions ... like running out of input. – Barry Jun 11 '19 at 19:44