tags:

views:

95

answers:

4
+2  Q: 

cin object in cpp

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?

+1  A: 

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.

Jay
+1 because this explains how cin "knows" how to separate words from an input stream.
rturrado
Actually, it uses any whitespace character as delimiter, which includes space, newline, tab, and more. But it's correct that it stops the current extraction when a delimiter is encountered.
Lajnold
A: 

http://www.cplusplus.com/reference/iostream/cin/

Visage
an abstract at least...
Tom
+3  A: 

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.

Jose Basilio
Your description of `if(cin)` is not quite the whole truth. One cannot put anything as the condition; is has to be a primitive type, or otherwise "convertible" to one. In the case of std::istream, of which std::cin is an instance, this is done via `operator void*()`. While the stream is "okay", the function returns non-NULL, otherwise it returns NULL.
Lajnold
+2  A: 

cin usage

"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>>

DumbCoder