Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Format Manipulators

 

Introduction

setiosflags

ios::left

scaling

setw

ios::showpoint

ios::right

Exercises

setprecision

ios::scientific

escape sequences

 
 
Introduction

 

C++ provides tools to format output.  These format manipulators are available by including the file <iomanip.h> as a compiler directive at the top of the source code.
 
#include <iostream.h>
#include <iomanip.h>

 

setw (width) manipulator

 

Causes the next value to be displayed in a field specified by width.  This setw (width) command is placed in the output expression prior to the output of variables or text constants. 

 

 
Code:
 
cout << setiosflags(ios::right);
 
cout << "1234567890" << endl;
cout << setw(10) << "abcde" << endl;
cout << 45 << endl;
 
Run output:
 
    1234567890
             abcde
    45
 
The text constant "abcde" will be right justified in a field-width of 10 columns.  However, the setw (10) does not affect the next output.
 
The setw (width) manipulator only applies to the next value to be printed.  However, the other manipulators will affect the appearance of all values which follow.

 

setprecision

 
controls the number of decimal points displayed by a floating point value.  The default precision display setting is 6 decimal places to the right of the decimal point. 
 
Code:
 
cout << 321.23456789 << endl;
        // default, prints 6 places to right of decimal
cout << setprecision (4);
cout << 321.2345678 << endl;          // displays 4 places to right of decimal
cout << 7.28 << endl;                       // only 7.28 gets printed, not enough places
cout << 25 << endl;                          // setprecision () has no effect on integers
 
Run output:
 
321.234568
321.2346
7.28
25

 

setiosflags manipulator

 

 
Provides for many options of which one or more can be used at a time.  Multiple options are separated by the (|) symbol.  Some of the options available are:
 

Option

Description

ios:showpoint
Displays decimal point and trailing zeros as necessary
ios::fixed
Displays real values in fixed-point form. Values are rounded off.
ios: scienfific
Displays real values in floating-point form
ios::left
Displays values left-justified
ios::right
Displays values right-justified within a field
 

Example 3: ios::showpoint
 
Code:
 
cout << setiosflags (ios::showpoint);       // will cause trailing zeros to print
cout << setprecision (4);
cout << 1.25 << endl;
 
Run output:
 
1.2500

 

Example 4: ios::scientific with setprecision ()

 

 
Code
 
cout << setprecision (2) << setiosflags (ios::scientific);
cout << 1.25 << endl;
cout << 12365.6219 << endl;         // answer is truncated to 2 decimal places
cout << 6.023e23 << endl;
 
Run output:
 
1.25e+00
1.23e+04
6.02e+23

 

Example 5: ios::left

 

 
Code:
 
cout << "12345678901234567890" << endl;
cout << setiosflags (ios::left) << setw (10) << "abcde";
//  left-justify state is still turned on
cout  << 45 << endl;
 
Run output:
 
12345678901234567890
abcde     45

 

Example 6: ios::right, ios::showpoint, ios::fixed, and setprecision()

 

 
Code:
 
cout << setiosflags (ios::right | ios::fixed | ios::showpoint);
cout << setprecision (2);
cout << setw(10) << 20 << endl;
cout << setw(10) << 25.95 << endl;
cout << setw(10) << 123.456 << endl;
 
Run output:
 
    20.00
    25.95
   123.46

 

Exercises

Exercise 1

Develop an 8 x 8 structure (no visible boundaries)

Place the character F around the perimeter.

Place the Character T in all other cells