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
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.
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
.
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
Plik z chomika:
Januszek66
Inne pliki z tego folderu:
Back Seat_ A Mumbai Tale - Aditya Kripalani.mobi
(755 KB)
Brief Wondrous Life of Oscar Wao, The - Junot Diaz.opf
(3 KB)
Don't Make Me Think, Revisited_ - Steve Krug.mobi
(9256 KB)
M. T. Anderson - Norumbegan 03 - The Empire of Gut and Bone # (v5.0).epub
(2209 KB)
M. T. Anderson - Norumbegan 02 - The Suburb Beyond the Stars # (v5.0).epub
(2105 KB)
Inne foldery tego chomika:
Dokumenty
Galeria
LUDLUM ROBERT
Midi - Kar
Mszał Rzymski PL
Zgłoś jeśli
naruszono regulamin