To use these, you need to have the following statements at the start of your program:
#include <iostream> //needed for any in/out streamsThe following table provides a list of the most useful output manipulators and their descriptions.
#include <iomanip> //includes the output manipulatorsusing std::manipulator //where manipulator is the manipulator you want to use
| 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, rightvoid 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.