< previous page page_133 next page >

Page 133
said to read outside data into the variables. The data for the program can come from an input device or from a file on an auxiliary storage device. We look at file input later in this chapter; here we consider the standard input device, the keyboard.
Input Streams and the Extraction Operator (>>)
The concept of a stream is fundamental to input and output in C++. As we stated in Chapter 3, you can think of an output stream as an endless sequence of characters going from your program to an output device. Likewise, think of an input stream as an endless sequence of characters coming into your program from an input device.
To use stream I/O, you must use the preprocessor directive
#include <iostream.h>
The header file iostream.h contains, among other things, the definitions of two data types: istream and ostream. These are data types representing input streams and output streams, respectively. The header file also contains declarations that look approximately like this:
istream cin;
ostream cout;
(We say approximately because the actual declarations are slightly different in a way that does not concern us right now.) The first declaration says that cin (pronounced see-in) is a variable of type istream. The second says that cout (pronounced see-out) is a variable of type ostream. Furthermore, cin is associated with the standard input device (the keyboard), and cout is associated with the standard output device (usually the display screen).
As you have already seen, you can output values to cout by using the insertion operator (<), which is sometimes pronounced put to:
cout << 3 * price;
In a similar fashion, you can input data from cin by using the extraction operator (>>), sometimes pronounced get from:
cin >> cost;

 
< previous page page_133 next page >