Hi everyone, I started to learn cpp and encountered cin as a way to receive input from the keyboard. If I understood, cin is an object and >> is an operator defined for it. In the way it is defined, how does it "knows" to separate words from each other? and another thing, what is the meaning of: while(cin) is cin a bool type? what does it mean if it returns true or false?
When you use the input stream there are specific character(s) defined to separate items in the input. By default I believe it's the space character. So you can enter things separated by spaces.
Calling:
cin >> var1 >> var2 >> var3;
is equivalent to:
cin >> var1;
cin >> var2;
cin >> var3;
As far as your other question goes, in C/C++ anything that returns a NULL
or zero is treated as false in an if
statement, otherwise it is treated as true.
That's why the line: if(cin)
works to check whether there's more data to be read in the stream.
"Where strm is the identifier of a istream object and variable is an object of any type supported as right parameter. It is also possible to call a succession of extraction operations as:
strm >> variable1 >> variable2 >> variable3; //...
which is the same as performing successive extractions from the same object strm" -> from operator>>