Sams Teach Yourself C++ in 21 Days

(singke) #1
int varOne, varTwo, varThree;
cout << “Enter three numbers: “
cin >> varOne >> varTwo >> varThree;
When you write cin >> varOne >> varTwo >> varThree;, the first extraction is evalu-
ated (cin >> varOne). The return value from this is another istreamobject, and that
object’s extraction operator gets the variable varTwo. It is as if you had written this:
((cin >> varOne) >> varTwo) >> varThree;
You’ll see this technique repeated later when coutis discussed.

Other Member Functions of cin..........................................................................


In addition to overloading operator>>,cinhas a number of other member functions.
These are used when finer control over the input is required. These functions allow you
to do the following:


  • Get a single character

  • Get strings

  • Ignore input

  • Look at the next character in the buffer

  • Put data back into the buffer


Single Character Input....................................................................................


operator>>taking a character reference can be used to get a single character from the
standard input. The member function get()can also be used to obtain a single character,
and can do so in two ways:get()can be used with no parameters, in which case the
return value is used, or it can be used with a reference to a character.

Using get()with No Parameters
The first form of get()is without parameters. This returns the value of the character
found and returns EOF (end of file) if the end of the file is reached. get()with no para-
meters is not often used.
Unlike using cinto get multiple values, it is not possible to concatenate this use of
get()for multiple input because the return value is not an iostreamobject. Thus, the
following doesn’t work:
cin.get() >>myVarOne >> myVarTwo; // illegal
The return value of cin.get() >> myVarOneis actually an integer, not an iostream
object.

604 Day 17

Free download pdf