Professional Documents
Culture Documents
The following statements show how the extraction operator >> works.
Example
int a;
double z;
char ch;
The following statements show how the extraction operator >> works.
cin and the Extraction Operator >>
The following statements show how the extraction operator >> works.
cin and the Extraction Operator >>
Recall that: For int variables, after inputting the digits of the
number, the reading stops at the first whitespace
character or a character other than a digit.
Therefore, the operator >> stores 78 into a.
The next right-side operand of >> is the variable z, which is of
the type double.
Therefore, the operator >> stores the value .49 as 0.49 into z.
cin and the Extraction Operator >>
What happens if the input stream has more data items than
required by the program?
After the program terminates, any values left in the input stream are
discarded.
When you enter data for processing, the data values should
correspond to the data types of the variables in the input statement.
Recall that when entering a number for a double variable, it is not
necessary for the input number to have a decimal part.
If the input number is an integer and has no decimal part, it is
converted to a decimal value.
The computer, however, does not tolerate any other kind of
mismatch. For example, entering a char value into an int or double
variable causes serious errors, called input failure
Using Predefined Functions in a Program
What if you intended to store 'A' in ch1, the blank in ch2, and 25 in
num?
It I clear that you cannot use the extraction operator >> to input
this data.
To overcome this, the variable cin can access the stream function
get, which is used to read character data.
The get function inputs the very next character, including
whitespace characters, from the input stream and stores it in the
memory location indicated by its argument.
The function get comes in many forms
cin and the get Function
To store 'A' in ch1, the blank in ch2, and 25 in num, you can
effectively use the get function as follows:
cin.get(ch1);
cin.get(ch2);
cin >> num;
The Dot Notation between I/O Stream Variables and
I/O Functions: A Precaution
In the statement
cin.get(ch);
cin and get are two separate identifiers separated by a dot
Dot separates the input stream variable name from the member,
or function, name
In C++, dot is the member access operator
Note that:
You must use the input functions together with an input stream
variable. If you try to use any of the input functions alone i.e
without the input stream variable, the compiler might generate an
error message such as ‘‘undeclared identifier - syntax error
Input Failure
Sample Run 1
Line 5: Enter four integers: 34 K 67 28
Line 8: The numbers you entered are:
Line 9: a = 34, b = 20, c = 30, d = 40
The input stream enters the fail state when it tries to input this
character K into the variable b
Sample Run 2
Line 5: Enter four integers: 37 653.89 23 76
Line 8: The numbers you entered are:
Line 9: a = 37, b = 653, c = 30, d = 40
Because c is an int variable, the decimal
point is regarded as a character, so the input stream enters the
fail state
Output and Formatting Output
The sample run shows that when the value of rate and tolerance
are printed without setting the scientific or fixed
manipulators, the trailing zeros are not shown and, in the case
of rate, the decimal point is also not shown.
After setting the manipulators, the values are printed to six
decimal places
setw
An input stream variable cin and the extraction operator >> read
a string into a variable of the data type string e.g
string name; //variable declaration
cin >> name; //input statement
The extraction operator skips any leading whitespace
characters and that reading stops at a whitespace character
The extraction operator cannot be used to read strings that contain
blanks
For example
Input name: John Makenya
then after the statement:
cin >> name;
executes, the value of the variable name is “John".
Input/Output and the string Type