Formatting the Output
Video Summary: https://youtu.be/omCW2Ou8ghE
To format a program's output means to control how it appears visually on the screen or on a printer. We use the special ostream variable cout together with the insertion operator << to accomplish this. Here is what an output statement looks like.
cout << ExpressionOrManipulator << ExpressionOrManipulator << ... ;
The C++ standard library supplies many manipulators, here are a few of them:
Category | Manipulator | Description |
---|---|---|
flow | endl |
|
Field Width | setw(n) |
|
left |
|
|
right |
|
|
Floating Point | setprecision(n) |
|
fixed |
|
|
scientific |
|
|
showpoint |
|
In this table, the manipulators without arguments (such as endl, fixed,
showpoint) are available through the header file iostream.
Those with argument (such as setw(n), setprecision(n)) require the
header file iomanip.
Now look at an example and see how those manipulators are used: