CS 1440 Lab 2
Cover sheetPrevious ActivityNext Activity

Activity 2-2: Input/ Output Examples and the iostream Library

Activity Procedure

It is likely that every program you write this term will have some form of output (otherwise, what good would it be?), and most programs will also require some input. Every C++ program that does input/output must make use of capabilities found in the "iostream" library. The 'io' in that name comes from input/output and the 'stream' comes from the notion of data flowing either to or from the program. The words "cout" and "cin" are the names of the output stream and the input stream, respectively. These words are pronounced "see-out" and "see-in".

In order to use the iostream library, we must tell the C++ compiler to include in our program information found in the iostream header file. This is what the

#include <iostream.h>

line of the program does. Make it a habit to put that line after the comment header of every program you write in this class.

cout represents the standard output device (the screen), and the "<<" operator is used to send values to cout. Likewise, cin is the standard input device (the keyboard) and the ">>" operator gets data from this device and stores it in one or more variables. The single line

        cin >> x >> y >> z;
is equivalent to reading x, y, and z in three separate lines:
        cin >> x; 
        cin >> y; 
        cin >> z;
The program will not proceed until three integers have been read and assigned to the variables x, y and z either way. White space is ignored when reading integers, so the input

88         97       65



97        65

result in the same values being read.

The line

        average = (x + y + z)/3;

is an assignment statement; the value of the expression on the right is stored in the variable on the left.

Now let's give the program a test run.

Compile the program to an executable file like so:

g++ -o ave ave.C

The syntax of this command is very important. If you use the "-o" option, the following name is used for the exececutable which is generated from the source file. In this case, ave.C is the source  file, and ave is the executable. You can omit the -o option and type simply

g++ ave.C

In this case the executable takes the default name of a.out. What you never want to do is to type

g++ -o ave.C

This uses ave.C as the name of the executable, and lacking a source argument, compiles nothing. This results in an empty executable; in other words, you just erased your source file!

So assuming you compile the first way, you now have a new verb in your vocabulary: ave. Just type this at the command line to run the program.

When prompted, enter this data:

81  83  85

(and press <Enter>). You should see:

Enter three grades separated by spaces, then press <Enter>:
81  83  85
The average is 83

Now try it again with new data 81 83 87. Here's the result:

Enter three grades separated by spaces, then press <Enter>:
81 83 87
The average is 83

Oops! We ought to get 83.67, but we get the same average as before. The problem is that when we calculate the average we evaluate (81 + 83 + 87)/3, or 251/3. When an integer is divided by an integer in C++, the result is an integer (obtained by truncation, or throwing away the fractional part). In a mixed division, say integer divided by double, real arithmetic is used and a double answer is obtained. So we can fix the problem simply by changing the denominator from 3 to 3.0. Here's the new program:

// Read and average 3 integers, print the result.
#include <iostream.h>
int main()
        int x,y,z;
        double average;
        // prompt the user:
        cout << "Enter three grades separated by spaces, then press <Enter>:" << endl;
        // read and store three integers:
        cin >> x >> y >> z;
        average = (x + y + z)/3.0;
        cout << "The average is " << average << endl;
        return 0;

Try compiling and running the modified program with the second set of data; you should see:

Enter three grades separated by spaces, then press <Enter>:
81 83 87
The average is 83.6667

It is clear that there are considerations in evaluating C++ expressions that you didn't have to worry about in a math class. We'll go over the rules for evaluating C++ expressions a little bit later.