C - Programming - C - Program - Chapter 03.pdf

(837 KB) Pobierz
CHAPTER
3
I NPUT /O UTPUT
IN THIS CHAPTER, YOU WILL:
n Learn what a stream is and examine input and output streams
n Explore how to read data from the standard input device
n Learn how to use predefined functions in a program
n Explore how to use the input stream functions get , ignore ,
putback , and peek
n Become familiar with input failure
n Learn how to write data to the standard output device
n Discover how to use manipulators in a program to format output
n Learn how to perform input and output operations with the
string data type
n Become familiar with file input and output
1247350666.049.png 1247350666.057.png 1247350666.058.png 1247350666.059.png 1247350666.001.png 1247350666.002.png 1247350666.003.png 1247350666.004.png 1247350666.005.png 1247350666.006.png 1247350666.007.png 1247350666.008.png 1247350666.009.png 1247350666.010.png 1247350666.011.png 1247350666.012.png 1247350666.013.png 1247350666.014.png 1247350666.015.png 1247350666.016.png 1247350666.017.png 1247350666.018.png 1247350666.019.png 1247350666.020.png 1247350666.021.png 1247350666.022.png 1247350666.023.png 1247350666.024.png 1247350666.025.png 1247350666.026.png 1247350666.027.png 1247350666.028.png 1247350666.029.png
116
|
Chapter 3: Input/Output
In Chapter 2, you were introduced to some of C++’s input/output (I/O) instructions,
which get data into a program and print the results on the screen. You used cin and
the extraction operator >> to get data from the keyboard, and cout and the insertion
operator << to send output to the screen. Because I/O operations are fundamental to
any programming language, in this chapter, you will learn about C++’s I/O operations
in more detail. First, you will learn about statements that extract input from the
standard input device and send output to the standard output device. You will then
learn how to format output using manipulators. In addition, you will learn about the
limitations of the I/O operations associated with the standard input/output devices and
learn how to extend these operations to other devices.
I/O Streams and Standard I/O Devices
A program performs three basic operations: it gets data, it manipulates the data, and it
outputs the results. In Chapter 2, you learned how to manipulate numeric data using
arithmetic operations. In later chapters, you will learn how to manipulate non numeric
data. Because writing programs for I/O is quite complex, C++ offers extensive support
for I/O operations by providing substantial prewritten I/O operations, some of which
you encountered in Chapter 2. In this chapter, you will learn about various I/O
operations that can greatly enhance the flexibility of your programs.
In C++, I/O is a sequence of bytes, called a stream, from the source to the
destination. The bytes are usually characters, unless the program requires other
types of information, such as a graphic image or digital speech. Therefore, a
stream is a sequence of characters from the source to the destination. There are
two types of streams:
Input stream: A sequence of characters from an input device to the computer.
Output stream: A sequence of characters from the computer to an output device.
Recall that the standard input device is usually the keyboard, and the standard
output device is usually the screen. To receive data from the keyboard and send
output to the screen, every C++ program must use the header file iostream .This
header file contains, among other things, the definitions of two data types,
istream (input stream) and ostream (output stream). The header file also con-
tains two variable declarations, one for cin (pronounced ‘‘see-in’’), which stands
for common input, and one for cout (pronounced ‘‘see-out’’), which stands for
common output.
These variable declarations are similar to the following C++ statements:
istream cin;
ostream cout;
To use cin and cout , every C++ program must use the preprocessor directive:
#include <iostream>
I/O Streams and Standard I/O Devices
|
117
From Chapter 2, recall that you have been using the statement using namespace
std ; in addition to including the header file iostream to use cin and cout . Without
the statement using namespace std; , you refer to these identifiers as std::cin
and std::cout . In Chapter 8, you will learn about the meaning of the statement
using namespace std; in detail.
3
Variables of type istream are called input stream variables; variables of type ostream
are called output stream variables.Astream variable is either an input stream
variable or an output stream variable.
Because cin and cout are already defined and have specific meanings, to avoid confu-
sion you should never redefine them in programs.
The variable cin has access to operators and functions that can be used to extract
data from the standard input device. You have briefly used the extraction operator
>> to input data from the standard input device. The next section describes in detail
how the extraction operator >> works. In the following sections, you will learn how
to use the functions get , ignore , peek ,and putback to input data in a specific
manner.
cin and the Extraction Operator >>
In Chapter 2, you saw how to input data from the standard input device by using cin
and the extraction operator >> . Suppose payRate is a double variable. Consider the
following C++ statement:
cin >> payRate;
When the computer executes this statement, it inputs the next number typed on
the keyboard and stores this number in payRate . Therefore, if the user types 15.50 ,
the value stored in payRate is 15.50 .
The extraction operator >> is binary and thus takes two operands. The left-side operand
must be an input stream variable, such as cin . Because the purpose of an input statement
is to read and store values in a memory location, and because only variables refer to
memory locations, the right-side operand is a variable.
The extraction operator >> is defined only for putting data into variables of simple
data types. Therefore, the right-side operand of the extraction operator >> is a variable
of the simple data type. However, C++ allows the programmer to extend the definition
of the extraction operator >> so that data can also be put into other types of variables
by using an input statement. You will learn this mechanism in the chapter entitled
Overloading and Templates, later in this book.
1247350666.030.png 1247350666.031.png 1247350666.032.png 1247350666.033.png 1247350666.034.png 1247350666.035.png 1247350666.036.png 1247350666.037.png 1247350666.038.png 1247350666.039.png
 
118
|
Chapter 3: Input/Output
The syntax of an input statement using cin and the extraction operator >> is:
cin >> variable >> variable...;
As you can see in the preceding syntax, a single input statement can read more than one
data item by using the operator >> several times. Every occurrence of >> extracts the
next data item from the input stream. For example, you can read both payRate and
hoursWorked via a single input statement by using the following code:
cin >> payRate >> hoursWorked;
There is no difference between the preceding input statement and the following two
input statements. Which form you use is a matter of convenience and style:
cin >> payRate;
cin >> hoursWorked;
How does the extraction operator >> work? When scanning for the next input, >>
skips all whitespace characters. Recall that whitespace characters consist of blanks and
certain nonprintable characters, such as tabs and the newline character. Thus,
whether you separate the input data by lines or blanks, the extraction operator >>
simply finds the next input data in the input stream. For example, suppose that
payRate and hoursWorked are double variables. Consider the following input
statement:
cin >> payRate >> hoursWorked;
Whether the input is:
15.50 48.30
or:
15.50
48.30
or:
15.50
48.30
the preceding input statement would store 15.50 in payRate and 48.30 in
hoursWorked . Note that the first input is separated by a blank, the second input is
separated by a tab, and the third input is separated by a line.
Now suppose that the input is 2 . How does the extraction operator >> distinguish
between the character 2 and the number 2 ? The right-side operand of the extraction
operator >> makes this distinction. If the right-side operand is a variable of the data type
char ,theinput 2 is treated as the character 2 and, in this case, the ASCII value of 2 is
stored. If the right-side operand is a variable of the data type int or double ,theinput
2 is treated as the number 2 .
1247350666.040.png 1247350666.041.png 1247350666.042.png
 
I/O Streams and Standard I/O Devices
|
119
Next, consider the input 25 and the statement:
cin >> a;
where a is a variable of some simple data type. If a is of the data type char ,onlythesingle
character 2 is stored in a .If a is of the data type int , 25 is stored in a .If a is of the data type
double , the input 25 is converted to the decimal number 25.0 . Table 3-1 summarizes this
discussion by showing the valid input for a variable of the simple data type.
3
Valid Input for a Variable of the Simple Data Type
TABLE 3-1
Data Type of a
Valid Input for a
char
One printable character except the blank
int
An integer, possibly preceded by a + or - sign
A decimal number, possibly preceded by a + or - sign. If the actual
data input is an integer, the input is converted to a decimal number
with the zero decimal part.
double
When reading data into a char variable, after skipping any leading whitespace characters,
the extraction operator >> finds and stores only the next character; reading stops after a
single character. To read data into an int or double variable, after skipping all leading
whitespace characters and reading the plus or minus sign (if any), the extraction operator
>> reads the digits of the number, including the decimal point for floating-point variables,
and stops when it finds a whitespace character or a character other than a digit.
EXAMPLE 3-1
Suppose you have the following variable declarations:
int a, b;
double z;
char ch, ch1, ch2;
The following statements show how the extraction operator >> works.
Statement
Input
Value Stored in Memory
1 cin >> ch;
A
ch = 'A'
2 cin >> ch;
AB
ch = 'A', 'B' is held for
later input
3 cin >> a;
48
a = 48
1247350666.043.png 1247350666.044.png 1247350666.045.png 1247350666.046.png 1247350666.047.png 1247350666.048.png 1247350666.050.png 1247350666.051.png 1247350666.052.png 1247350666.053.png 1247350666.054.png 1247350666.055.png 1247350666.056.png
 
Zgłoś jeśli naruszono regulamin