Output Formatting in C++


The term output formatting refers to the manner in which visual output is displayed. It includes such issues as spacing, alignment, precision, presence of decimal points, etc. C++ controls output formatting through the use of the stream object cout and a collection of optional stream manipulators as described in section 3.7 of your textbook.

The cout object

The cout object is used to output most types of data to the screen and to other devices such as disks. The C++ statement to display the phrase "Hello world!" would be

    cout << "Hello world!";

The cout object is not a standard part of the C++ Language, but can be found in the iostream header file. Before you can use the object in a C++ program, you must type the following compiler preprocessing directive on a line by itself at the beginning of the source code:

    #include <iostream>

Displaying Prompts

The cout object can be used to display prompts (messages urging a user to enter some data) as in:

    cout << "How old are you? ";

When executed, this statement would leave the cursor on the same line as the prompt and allow the user to enter a response following it on the same line. A space was included at the end of the prompt (before the closing quote mark) to separate the upcoming response from the prompt and make the prompt easier to read during data entry.

Carriage Returns

If you want a carriage return to occur after displaying a message, simply include the special stream manipulator endl to specify that a line of output should end. It peforms the same action as inserting the special escape sequence \n into a string being output. For example, the statement

   cout << "My Program" << endl;

and the statement

   cout << "My Program\n";

will both display the words "My Program" one a single line and then carriage return to the next line.

Be aware that each occurance of either the endl manipulator or the \n escape sequence will cause the cursor to advance to a new line. For example, the single C++ statement

   cout << "Line 1\n" << endl << "Line 3" << endl;

will produce three lines of output:

Line 1

Line 3

For information about how to output other special characters, read the document about Displaying Special Characters in C++.

Output Field Width and Rounding

When displaying the contents of a variable, we seldom know what the value will be. And yet, we can still control the format of the output field (area), including the:

Output formatting is used to define specific alignment and rounding of output, and can be performed in while using the cout object by including various stream manipulators as described below. Before you can use these stream manipulators, you must type the following preprocessing directive on a line by itself at the beginning of the source code:

    #include <iomanip>

setw()

Example: To display the floating point number 123.456 right-aligned in an eight character wide output field and rounded to 2 decimal places, you would expand basic statement of simply

    cout << 123.456 << endl;

to be

    cout << setw(8) << setprecision(2) << fixed << 123.456 << endl;

The setw(8) would define the width of the output field, and the setprecision(2) would indicate the desired rounding. The value 123.456 in the statement above could be replaced by a variable, a named constant or a symbolic constant. Keep in mind that the addition of these stream manipulators has no effect on the value itself, only on the appearance of the output characters. If the output item is larger than the width specified, the setw manipulator is overriden and the entire item is displayed. If the output item is smaller than the width specified, the setw manipulator will align item to the right side of the output field (padding the unused positions with blank spaces) unless overriden by the use of the left (alignment) stream manipulator. For example, the statement

   cout << setw(8) << 123.45 << endl;

will display the value 123.45 in a width of 8 characters with (2 spaces padded in front of the digit 1).

setprecision() and fixed

The term "significant digits" (or precision) describes the quantity of digits in a written number that appear before and after the decimal point (combined). In C++ cout statements, output precision is controlled by using the setprecision() stream manipulator ahead of the value to be displayed in the output stream. For example, the statement

   cout << setprecision(4) << 246.7912 << endl;

will display the value 246.8 (rounded at the 4th digit position). When used with the setw() manipulator, a low value specified for the setprecision() manipulator can cause the computer to use scientific notation in the output of the value. When used with the fixed manipulator, the setprecision() manipulator changes to specify the number of digits to the right of the decimal point, rather than the total on both sides of it. The effects of the setprecision() manipulator are persistent, affecting all items in the output stream following its use. For example, the statement:

   cout << setprecision(2) << fixed << 123.9182 << endl;

will display the value 123.92 (rounded at the 2nd digit position right of the decimal point). The effects of the fixed manipulator are persistent, affecting all items in the output stream following its use.

left and right

Programmers can specify the horizontal alignment for output within a field using the stream manipulators left or right. The manipulator left will horizontally align all following output to the left side of the output field by padding the unused positions with blank spaces. The right stream manipulator will horizontally align all following output to the right side of the output field by padding the unused positions with blank spaces. Note that the default horizontal alignment for the C++ cout stream object is "right", regardless of data type.

showpoint

showpoint is a stream manipulator used with the cout object to pad numeric output with trailing zeros until all digits specified by the setprecision() manipulator have been output (even for number with fewer digits than specified). For example, the statement

   cout << setprecision(6) << showpoint << 123.4 << endl;

will display the value 123.400 (padding the last 2 digit positions of the 6 specified with spaces). The effects of the showpoint manipulator are persistent, affecting all items in the output stream following its use.

For more information about output formatting in C++, read chapter 3 of your textbook or search the Web.

PATH: Instructional Server> COP 2000> Examples>