ADVANCED PLACEMENT Computer Science - C++ LESSON 3: stream I/O The key topics for this lesson are: A.
Stream Input Operator (>>) VOCABULARY:
DISCUSSION: 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. 3.
The input operator has the following general syntax and behavior: a.
istream will be the source of input. 4.
Here are some example statements:
a.
When the program statement
cin >> num1
is encountered, 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 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 ---> "; 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.
3.
You cannot break up a string constant and wrap it around a line.
cout << "A long string constant must be broken up into
two However, this will work:
cout << "A long string constant must be broken up into
two" 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> 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:
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:
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: Example 1, use of ios::showpoint:
Run output:
Example 2, use of ios::scientific with setprecision ():
Run output:
Example 3, use of ios::left:
Run
output:
Example 4, use of ios::right, ios::showpoint,
ios::fixed, and setprecision():
Run output:
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.
Run
output:
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:
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 |