C++ supports a number of features that could be used for formatting the output. These features include:
- ios class functions and flags.
- Manipulators.
- User-defined output functions.
1. ios Class functions and flags
The ios class contains a large number of member functions that could be used to format the output in a number of ways. The most important ones among them are listed below.
Function | Syntax | Use |
width() | cout.width(size); | To specify the required field size for displaying an output value. |
precision() | cout.precision(2); | To specify the number of digits to be displayed after the decimal point of a float value. |
fill() | cout.fill(‘character’); | To specify a character that is used to fill the unused portion of a field. |
setf() | cout.setf(arg1, arg2); | To specify format flags that can control the form of output display such as left or right justification. |
unsetf() | cout.resetiosflags() | To clear the flags specified. |
In setf() function we can provide one or two argument.
cout.setf(arg1, arg2);
The arg1 is formatting flags defined in the ios class, and arg2 is known as bit field specifies the group to which the formatting flags belong. The flags and bit field are shown below:
Format required | Flag (arg1) | Bit-field (arg2) |
Left justified output Right justified output Padding after sign or base indicator (like +##20) | ios::left ios::right ios::internal | ios::adjustfield ios::adjustfield ios::adjustfield |
Scientific notation Fixed point notation | ios::scientific ios::fixed | ios::floatfield ios::floatfield |
Decimal base | ios::doc | ios::basefield |
Octal base Hexadecimal base | ios::oct ios::hex | ios::basefield ios::basefield |
There some of the flags without bit fields are mentioned below:
Flag | Meaning |
ios::showbase ios::showpos ios::showpoint ios::uppercase | Use base indicator on output. Print + before positive numbers. Show trailing decimal point and zeros. Use uppercase letters for hex output. |
ios::skipus | Skip white space on input. |
ios::unitbuf ios::stdio | Flush all streams after insertion. Flush stdout and stderr after insertion. |
Example of ios functions:
#include <iostream>
#include <math>
using namespace std;
int main()
{
cout.fill('*');
cout.setf(ios::left,ios::adjustfield);
cout.width(10);
cout<<"value";
cout.setf(ios::right,ios::adjustfield);
cout.width(15);
cout<<"SQRT OF VALUE"<<"\n";
cout.fill('.');
cout.precision(4);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.setf(ios::fixed,ios::floatfield);
for(int i=1;i<=10;i++)
{
cout.setf(ios::internal, ios::adjustfield);
cout.width(5);
cout<<i;
cout.setf(ios::right, ios::adjustfield);
cout.width(20);
cout<<sqrt(i)<<"\n";
}
cout.setf(ios::scientific, ios::floatfield);
cout<<"\nSQRT(100)="<<sqrt(100)<<"\n";
return 0;
}
Output:
value*******SQRT OF VALUE
+...1.............+1.0000
+...2.............+1.4142
+...3.............+1.7321
+...4.............+2.0000
+...5.............+2.2361
+...6.............+2.4495
+...7.............+2.6458
+...8.............+2.8284
+...9.............+3.0000
+..10.............+3.1623
SQRT(100)=+1.0000e+01
2. Manipulators
Manipulators are used to manipulate the output in specific format. Below is the list of manipulators in c++.
Manipulators | Use |
setw() | To specify the required field size for displaying an output value. |
setprecision() | To specify the number of digits to be displayed after the decimal point of a float value. |
setfill() | To specify a character that is used to fill the unused portion of a field. |
setiosflags() | To specify format flags that can control the form of output display such as left or right justification. |
resetiosflags() | To clear the flags specified. |
You must log in to post a comment.