Output

The built in IOStream library is used for output in C++. Two objects are predefined for output in C++:

cout and cerr are used for output. The operator used with these streams is <<, which is called the insertion operator. The insertion operator takes a value from the program and inserts it into the output stream.

A cout statement can contain multiple insertion operators to print multiple values. Printing the special value endl causes subsequent output to appear on the next line.

The output generated depends on the type of data being printed.

Example

#include <iostream.h>

int main()
{
   int num = 10;
   double val = 3.141592653;
   float money = 5.50;
   float bigval = 987654321000.0;
   float smallval = .00000028917;
   char yes = 'y';

   cout << "the value of num is" << num << endl;
   cout << "val = " << val << endl;
   cout << "money = " << money << endl;
   cout << yes << endl << endl;
   cout << "bigval = " << bigval << " and smallval = " << smallval << endl;
   cout << "bigval = " << bigval;
   cout << " and smallval = " << smallval;
   return 0;
}

The output from this program is:

the value of num is10
val = 3.14159
money = 5.5
y

bigval = 9.87654e+11 and smallval = 2.8917e-07
bigval = 9.87654e+11 and smallval = 2.8917e-07

Formatting Real Numbers

To format real numbers, you will use manipulators. These are special I/O commands. To use manipulators you must include another header file, iomanip.h. There are three steps to format real numbers. The first is to specify that you want the numbers printed in fixed format, not exponential format. For this you will use the setiosflags manipulator. The second is to specify that you always want the decimal point to print. Again for this step you will use the setiosflags manipulator. The third is to specify how many decimal places you want to print. For this you will use the setprecision manipulator. The value of the real number will be rounded to the number of decimal places you specify.

Example

#include <iostream.h>
#include <iomanip.h>

int main()
{
    int num = 10;
    double val = 3.141592653;
    float money = 5.5;
    float bigval = 9876543210.0;
    float smallval = .0028917;
    float e = 2.71828;

    cout << setiosflags(ios::fixed);
    cout << setiosflags(ios::showpoint);
    cout << setprecision(2);

    cout << "num = " << num << endl;
    cout << "val = " << val << endl;
    cout << "money = " << money << endl;
    cout << "bigval = " << bigval << " and smallval = " << smallval << endl;
    cout << "e = " << e << endl;
    cout << setprecision(4);
    cout << "bigval = " << bigval << " and smallval = " << smallval << endl;
    return 0;
}

The output from this program is:

num = 10
val = 3.14
money = 5.50
bigval = 9876543488.00 and smallval = 0.00
e = 2.72
bigval = 9876543488.0000 and smallval = 0.0029

Aligning Output

By default, each item printed will use as many columns as necessary to print the value. To align output, you can specify a field width for each value you print. If the field width is larger than the value, the value will be right aligned in the field. If the field width is smaller than the value, the field width will be ignored, and the value will take up as many columns as it needs. To specify field width, you will use another manipulator. The manipulator used to set the field width is setw. The setw manipulator only applies to the next value printed. If you want to set the field width for multiple items, you must specify setw for each one. Also remember that a decimal point and a minus sign, if present, each take up one column.

Example

#include <iostream.h>
#include <iomanip.h>

int main()
{
    int num = 10;
    float money = 5.5;
    float bigval = 9876543210.0;

    cout << setiosflags(ios::fixed);
    cout << setiosflags(ios::showpoint);
    cout << setprecision(2);

    cout << setw(10) << "first" << setw(10) << "second" << endl;
    cout << setw(10) << num << setw(10) << (num + 1) << endl;
    cout << setw(10) << num * 2 << setw(10) << (num * 2 + 1) << endl;
    cout << setw(6) << num << setw(6) << money << endl;
    cout << setw(6) << num << money << endl;
    cout << setw(6) << "long label" << setw(6) << bigval << endl;
    return 0;
}

The output from this program is:

     first    second
        10        11
        20        21
    10  5.50
    105.50
long label9876543488.00

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002