Iomanip
Iomanip
html
Sets minimum field width on output. This sets the minimum size of the field - a larger number
will use more columns. Applies only to the next element inserted in the output. Use left and
setw(n) next right to justify the data appropriately in the field. Output is right justified by default. Equivalent
to cout.width(n); To print a column of right justified numbers in a seven column field:
cout << setw(7) << n << endl;
left next Left justifies output in field width. Only useful after setw(n).
Right justifies output in field width. Since this is the default, it is only used to override the effects
right next
of left. Only useful after setw(n).
Only useful after setw. If a value does not entirely fill a field, the character ch will be used to fill in
setfill(ch) all the other characters. Default value is blank. Same effects as cout.fill(ch) For example, to print a
number in a 4 character field with leading zeros (eg, 0007):
cout << setw(4) << setfill('0') << n << endl;
Sets the number of digits printed to the right of the decimal point. This applies to all subsequent
floating point numbers written to that output stream. However, this won't make floating-point
setprecision(n) all
"integers" print with a decimal point. It's necessary to use fixed for that effect. Equivalent to
cout.precision(n);
Used fixed point notation for floating-point numbers. Opposite of scientific. If no precision has
fixed all
already been specified, it will set the precision to 6.
bool output
boolalpha
all Uses alphabetic representation (true and false) for bool values. Turned off with noboolalpha .
noboolalpha
Input
For most input values (eg, integers and floating-point numbers), skipping initial whitespace (eg,
blanks) is very useful. However, when reading characters, it is often desired to read the
skipws whitespace characters as well as the non-spacing characters. The these I/O manipulators can be
all
noskipws used to turn whitespace skipping off and on. Eg,
cin >> noskipws;
turns whitespace skipping off for all subseqent cin input.
1 of 2 5/2/2006 12:07 PM
C++ Notes: I/O Manipulators http://www.fredosaurus.com/notes-cpp/io/omanipulators.html
Other
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const float tenth = 0.1;
const float one = 1.0;
const float big = 1234567890.0;
cout << "A. " << tenth << ", " << one << ", " << big << endl;
cout << "B. " << fixed << tenth << ", " << one << ", " << big << endl;
cout << "C. " << scientific << tenth << ", " << one << ", " << big << endl;
cout << "D. " << fixed << setprecision(3) << tenth << ", " << one << ", " << big << endl;
cout << "E. " << setprecision(20) << tenth << endl;
cout << "F. " << setw(8) << setfill('*') << 34 << 45 << endl;
cout << "G. " << setw(8) << 34 << setw(8) << 45 << endl;
return 0;
}
produces this output (using DevC++ 4.9.8.0):
A. 0.1, 1, 1.23457e+009
B. 0.100000, 1.000000, 1234567936.000000
C. 1.000000e-001, 1.000000e+000, 1.234568e+009
D. 0.100, 1.000, 1234567936.000
E. 0.1000000014901161
F. ******3445
G. ******34******45
Lines F and G show the scope of setw() and setfill().
Copyleft 2000-2003 Fred Swartz Last update 2003-08-24, URL=undefined
2 of 2 5/2/2006 12:07 PM