C++ Formatted console Input output operations
C++ language also supports a number of features that could be used for formatting the output. These features include:
1 ios class functions and flags.
2 Manipultors.
3 User-defined output functions.
The ios class contain a large number of member functions that would help us to format the output in a number of ways. The most important ones among them are listed in below Table
Table: ios format functions |
Width () |
To specify the required fields size for displaying an output value or contents
|
precesion () |
To specify the number of digits to be displayed after the decimal point of a float value |
fill () |
To specify a characters that are used to fill the unused portion of a field |
self() |
To specify format flags that can control the form of output display (such as left-justification and right-justification) |
unself() |
To clear flags specified |
Manipulators are special functions that can be included in the I/O(Input output) statements to alter the format parameters of a stream. Table10.3 shows some important manipulator functions that are frequently used. To access these manipulator, the file iomanip should be included in the program.
Manipulators |
Equivalent ios function |
set w() |
witdh() |
setprecision() |
precision() |
setfill() |
fill() |
setiosflags() |
self() |
resetiosflags() |
unself() |
In addition to these functions supported by the C++ library, we can create our own manipulator functions to provide any special output formats. The following sections will provide details of how to use the pre-defined formatting functions and how to create new ones.