ADVANCED PLACEMENT Computer Science - C++
LESSON 3: stream I/O
INTRODUCTION: This lesson will introduce two new topics, stream input and the formatting of stream output. High level languages provide programming with standardized ways to access data. One of the primary sources of data input for your programs is the keyboard, which will be accessed in C++ programs by using the input operator (>>) and the object cin. The key topics for this lesson are:
A.
Stream Input Operator (>>)
B. Multiple Line
Stream Output Expressions
C. Format
Manipulators
D. Use of apstring
Objects
VOCABULARY:
| FORMAT MANIPULATORS | STRING |
| STREAM INPUT OPERATOR |
DISCUSSION: A. Stream Input Operator (>>)
1. Streams fall into two categories. An istream allows for input from the keyboard, a disk drive, etc. An ostream allows for output to the monitor, a printer, a disk drive, etc.
2. In addition to the general definition for streams in C++, the iostream.h library defines three specific stream objects:
a. An
istream object named cin, associated with the keyboard.
b.
An ostream object named cout, associated with the monitor.
c.
An ostream object named cerr, which is associated with the
monitor and is used to print error messages.
3. The input operator has the following general syntax and behavior: istream >> variable
a.
istream will be the source of input.
b.
variable will represent one or more variables.
c.
The input operator returns the value of istream.
This will allow for chaining of the input operator.
4.
Here are some example statements:
|
cin >> num1; |
a.
When the program statement
cin >> num1
is encountered,
execution of the program is suspended until an appropriate value is
entered on the keyboard.
b.
The input operator is left-associative, which means that the
multiple input example in the second line,
cin >> c1 >> c2 >> c3
will be solved from left to right. The expression is solved in this order:
( (cin >> c1) >> c2) >> c3
The expression (cin >> c1) is solved by typing in one keystroke from the keyboard and the expression returns cin. This prepares the expression to deal with the second input to c2. We can think of chaining like this:
(cin >> c1) >> c2
cin >> c2
The input expression inside of the parentheses returns cin. The stream object cin is ready for more input action. This second example using cin will assign the character variables c1, c2, and c3 with the 3 characters typed at the keyboard by the user of the program.
5. Any white space (spaces, tabs, newline) will separate input values. When filling character variables, white space keystrokes are ignored as the character variables are filled. If it is desirable to fill character variables with both white space and non-white space characters, a different C++ tool (cin.get()) is required. This tool will be covered in Lesson 14.
6. When requesting data from the user via the keyboard, it is good programming practice to provide a prompt. An unintroduced input statement leaves the user hanging without a clue of what the program wants. Prompt the user.
cout << "Enter an integer ---> ";
cin
>> number;
B. Multiple Line Stream Output Expressions
1. We have already used examples of multiple output statements such as:
cout << "The value of sum = " << sum << endl;
2.
There will be occasions when the length of an output statement
exceeds one line of code.
This can be broken up several
different ways.
| cout << "The sum of " << number1 << " and " << number 2 << " = " << number1 + number2; or... cout << "The
sum of " << number1 << " and
" << number2; |
3. You cannot break up a string constant and wrap it around a line. This is not valid:
cout << "A long string constant must be broken up into
two
separate quotes. This
will not work.";
However, this will work:
cout << "A long string constant must be broken up into
two"
<< "separate quotes. This
will work.";
C. Format Manipulators
1. C++ provides tools to format output. These format manipulators are available by including the file <iomanip.h> as a compiler directive at the top of the source code.
#include <iostream.h>
#include <iomanip.h>
2. The setw (width) manipulator causes the next value to be displayed in a field specified by width. This setw (width) command is placed in the output expression prior to the output of variables or text constants. Example:
|
|
Run output:
|
abcde 45 |
The text constant "abcde" will be right justified in a field-width of 10 columns. However, the setw (10) does not affect the next output. The setw (width) manipulator only applies to the next value to be printed. However, the other manipulators will affect the appearance of all values which follow.
3. The setprecision (precision) controls the number of decimal points displayed by a floating point value. The default precision display setting is 6 decimal places to the right of the decimal point. Example:
|
cout << setprecision (4); |
Run output:
|
|
4. The setiosflags (FlagList) manipulator provides for many options of which one or more can be used at a time. Multiple options are separated by the (|) symbol. Some of the options available are:
ios::showpoint Displays decimal point and trailing zeros as necessary.
ios::fixed Displays real values in fixed-point form. Values are rounded off.
ios::scientific Displays real values in floating-point form (scientific notation, 1.25e10). Extra place values are truncated.
ios::left Displays values left-justified.
ios::right Displays values right-justified within a field.
Example 1, use of ios::showpoint:
cout << setiosflags (ios::showpoint); // will cause trailing zeros to print cout << setprecision (4); cout << 1.25 << endl; |
Run output:
| 1.2500 |
Example 2, use of ios::scientific with setprecision ():
|
|
Run output:
|
|
Example 3, use of ios::left:
|
|
Run
output:
|
abcde
45 |
Example 4, use of ios::right, ios::showpoint,
ios::fixed, and setprecision():
|
|
Run output:
|
25.95
123.46 |
D. Use of apstring Objects
1. A string is a collection of characters. The apstring class supports the declaration and use of strings.
2. A program which uses apstrings will need to include the appropriate file, much like the include statement for the iostream.h file. To use the apstring class, add this line near the top of your source code:
#include <apstring.h>
3. The following program illustrates the use of apstrings with the input/output operators.
|
#include
<iostream.h> main() { |
Run
output:
|
word
= hello |
4. Notice that using apstrings in the context of input and output statements is identical to using other data types.
5. The delimiter for entering apstrings from an input file is any white space character: return key, space bar, or the tab key. If you had tried to type in the string "hello world", the apstring would have only accepted "hello". For example:
Run output:
|
word
= hello |
Notice that the blank space terminated the input of the string.
6. The apstring class will be covered in depth in Lesson 23. For now you will use strings for simple input and output in programs.
SUMMARY/REVIEW: These two libraries, iostream.h and iomanip.h, will be used in almost every program. The labs in this lesson will provide an opportunity to practice using the format manipulators.
ASSIGNMENT: ICT 3: lab 3.1 -- Lab Exercise, L.A.3.1, Change
ICT 3: lab 3.2 -- Lab
Exercise, L.A.3.2, CarRental