Page 260 - DCAP404 _Object Oriented Programming
P. 260
Unit 12: Console I/O
where variable1, variable2, . . .. are valid C++ variable names that have been declared already. Notes
This statement will cause the computer to halt the execution and look for input data from the
keyboard. The input data for this statement would be:
datal data2……dataN
The input data are separated by white spaces and should match the type of variable in the cin list.
Spaces, new lines and tabs will be skipped.
The operator >> reads the data character by character and assigns it to the indicated location. The
reading for a variable will be terminated at the encounter of a white space or a character that
does not match the destination type. For example, consider the following code:
int code;
cin >> code;
Suppose the following data is given as input:
1267E
The operator will read the characters up to 7 and the value 1267 is assigned to code. The character
E remains in the input stream and will be input to the next cin statement. The general format of
outputting data:
cout << iteml <<item2 << .. ..<< itemN;
!
Caution The items item1 through itemN may be variables or constants of any basic types.
12.1 Concept of Streams
A stream is a source of sequence of bytes. A stream abstracts for input/output devices. It can be
tied up with any I/O device and I/O can be performed in a uniform way. The C++ iostream
library is an object-oriented implementation of this abstraction. It has a source (producer) of
flow of bytes and a sink (consumer) of the bytes. The required classes for the stream I/O are
defined in different library header files.
To use the I/O streams in a C++ program, one must include iostream.h header file in the
program. This file defines the required classes and provides the buffering. Instead of functions,
the library provides operators to carry out the I/O. Two of the Stream Operators are:
<< : Stream insertion for output.
>> : Stream extraction for input.
The following streams are created and opened automatically:
cin : Standard console input (keyboard).
cout : Standard console output (screen).
cprn : Standard printer (LPT1).
cerr : Standard error output (screen).
clog : Standard log (screen).
caux : Standard auxiliary (screen).
LOVELY PROFESSIONAL UNIVERSITY 253