C++ File pointers and Manipulators
The header file iomanip provides a set of functions called manipulators which can be used to the manipulate the output formats. They provide the same features as that of the ios(Input output system) member functions and flags. Some manipulators are more convenient to use than their counterparts in the class(Input output system) ios. such that, two or more manipulators can be used as a chain in one statement as shown below:
cout << manip1 << manip2 << manip3 << item; cout << manip1 << item1 << manip2 << item2;
|
This kind of concatenation is useful when we want to display several columns of output.The most commonly used manipulators are shown in Table. The table also gives their meaning and equivalents. To access these manipulators, we must include the file iomanip in the program.
Manipulators and their meanings |
Manipulator |
Meaning |
Equivalent |
setw(int w)setprecision(int d) |
Set the field width ton w. Set the following point precision to d. |
width()precision() |
setfill(intc) |
Set the fill character to c. |
fill() |
setiosflags(long f) |
Set he format flag f. |
setf() |
restiosflags(long f) |
Clear the flag specified by f. |
unsetf() |
Endif |
Insert new line and flush stream. |
“\n” |
Some examples of manipulators are given below:
cout << setw(10) << 12345;
|
This statements prints the value 12345 right-justified in a field width of 10 characters. The output can be made left-justified by modifying the statement as follows:
cout << setw(10) << setiosflags(ios : : left) << 12345;
|
One statement can be used to format output for two or more values. For example, the statement
cout << setw(5) << setprecision(2) << 1.2345
<< setw(10) << setprecision(4) << sqrt(2)
<< setw(15) << setiosflags(ios : : scientific) << sqrt(3);
<< end1;
|
Will print all the three values in one line with the field sizes of 5,10, and 15 respectively. Note that each output is controlled by different sets of format specifications. We can jointly use the manipulation and the ios functions in a program. The following segment of code is valid:
cout.setf(ios : :showpoint);
cout.setf(ios : :showpos);
cout << setprecsion(4);
cout << setiosflags(ios : : scientific);
cout << setw(10) << 123.45678;
|
Note! There is a major difference in the way the manipulators are implemented as compared in the ios member functions. The ios member function return the previous format state which can be used later, if necessary. But the manipulator does not return the previous format state. In case, we need to save the old format states, we must use the ios member functions rather than the manipulators. Example:
cout.precsion(2);
int p = cout.prcision(4); ;
|
when these statements are executed, p will hold the value of 2(previous state and the new format state will be 4. We can restore the previous format state as follows:
cout.precision(p);
Program illustrates the following of the output values using both manipulators and ios functions.
FORMATTING WITH MANIPULATORS
< iostream >
< iomanip >
using namespace std;
main()
{
cout.setf(ios : : showpoint);
cout << setw(5) << “n”
<< setw(15) << “Inverse_of_ n”
<< setw(15) << “Sum_ of_terms\n\n”;
term, sum=0;
(int n=1; n<=10; n++)
{
term = 1.0 / float(n);
sum = sum + term;
cout << setw(5) << n
<< setw(14) << setprecison(4)
<< setiosflags(ios : : scientific) << term
<< setw(13) << resetiosflags(ios : : scientific)
<< sum << end1;
}
0;
}
|
The output of Program 10.8 would be:
n Inverse_of_n Sum_of_terms
1 1.0000e+000 1.0000
2 5.0000e-001 1.5000
3 3.3333e-001 1.8333
4 2.5000e-001 2.0833
5 2.0000e-001 2.2833
6 1.6667e-001 2.4500
7 1.4286e-001 2.5929
8 1.2500e-001 2.7179
9 1.1111e-001 2.8290
10 1.0000e-001 2.9290
|