Output Manipulators

You can use output manipulators to do nifty things like printing pretty tables. I don't think there are really any abstract concepts involved, so on with the reference material.

To use these, you need to have the following statements at the start of your program:

#include <iostream> //needed for any in/out streams
#include <iomanip> //includes the output manipulators

using std::manipulator //where manipulator is the manipulator you want to use

The following table provides a list of the most useful output manipulators and their descriptions.

Manipulator Description
setw(n) Displays the next item in a field at least n units wide.
right Displays output right-justified in the output field
left Displays output left-justified in the output field
fixed Changes to fixed (rather than floating) point notation
showpoint Makes sure the decimal point will be shown (even when there is only an integer portion)
setprecision(n) Sets the number of digits to show
(if used alone, this means a total of n digits; if used with fixed, there will be n digits after the decimal point)
setfill(x) Pads output that's smaller than the field with the character x

Here's a sample function that uses almost all of the above!

//This function accepts a number and displays it in the form $***12.34
//Requires the <iostream> library //Uses: fixed, showpoint, setprecision, setfill, setw, right

void DisplayMoney(double amount)
{
    cout << '$' << fixed << showpoint << setprecision(2) << setfill('*') << setw(8) << right << amount;
}


I hope you found this reference useful. Please contact me with any questions, corrections, or suggestions.