Next: Programming Style
Up: A simple C++ program
Previous: General form of a
Input and output use the input stream cin
and the
output stream cout
. The input stream cin
is usually associated with the keyboard and the output stream
cout
is usually associated with the monitor.
The following statement waits for a number to be entered from the
keyboard and assigns it to the variable number
:
cin >> number;
The general form of a statement to perform input using the input
stream cin
is:
where input-list is a list of identifiers, each identifier preceded by the input operatorcin
input-list;
>>
. Thus
cin >> n1 >> n2;
would take the next two values entered by the user and assign the
value of the first one to the variable n1
and the second to the
variable n2
.
The program must read a value for each variable in the input-list
before it executes any more statements.
The order in which the values are entered must correspond to the order
of the variables in the input-list and they must be of the same type
as the corresponding variable. They should be separated by spaces.
Normally, the C++ system will not pass any values to the variables in
the input-list until a complete line of input has been read, i.e. until the return or enter key has been pressed. If
more values are supplied than are required to give each variable in the
input-list a value, the unused values will be used for any subsequent
input statements using cin
.
For example given the following declarations and input statement:
int count, n; float value; cin >> count >> value >> n;the user could enter
23 -65.1 3to assign 23 to
count
, -65.1 to value
and 3 to
n
. There is no indication in the data of which value is to be
associated with which variable; the order of the data items
must correspond to the order of the variables in the input list. The
data items on input should be separated by spaces or new lines. Any
number of these will be skipped over before or between data items.
Thus the input above could equally well have been entered as:23 -65.1 3
The following statement outputs the current value of the variable
count
to the output stream cout
, which is usually
associated with the monitor. The value will be
printed on the current line of output starting immediately after any
previous output.
cout << count;
The general form of a statement to perform output using the output
stream cout
is:
where output-list is a list of variables, constants, or character strings in quotation marks, each preceded by the output operatorcout
output-list;
<<
. The output
operator displays the value of the item that follows it. The values
are displayed in the order in which they appear in the output-list. A
new line is taken if the special end-of-line character endl
is
output. If an endl
is not output, the output line will either
be chopped off at the right hand edge of the screen or it may wrap
round on to the next line. Do not rely on either behaviour as
different computer systems may do things differently. Thus
cout << "Hello there" << endl;
will print Hello there
on the current output line and then take
a new line for the next output. The statements:float length, breadth; cout << "Enter the length and breadth: "; cin >> length >> breadth; cout << endl << "The length is " << length; cout << endl << "The breadth is " << breadth << endl;will display, if the user enters 6.51 and 3.24 at the prompt, the following output:
The length is 6.51 The breadth is 3.24
Note that a value written to cout
will be printed immediately
after any previous value with no space between. In the above program
the character strings written to cout
each end with a space
character. The statement
cout << length << breadth;
would print out the results as
6.513.24
which is obviously impossible to interpret correctly. If printing
several values on the same line remember to separate them with spaces
by printing a string in between them as follows:
cout << length << " " << breadth;