Two basic types of I/O manipulators: parameterless and parameterized. - C++ File Stream

C++ examples for File Stream:cout

Introduction

The parameterless manipulators that operate on output streams are shown here:

Manipulator Purpose
boolalpha Turns on boolalpha flag.
endl Outputs a newline.
ends Outputs a null.
dec Turns on dec flag. Turns off the hex and oct flags.
fixed Turns on fixed flag. Turns off the scientific flag.
flush Flushes the stream.
hex Turns on hex flag. Turns off the dec and oct flags.
internal Turns on internal flag. Turns off the left and right flags.
left Turns on left flag. Turns off the right and internal flags.
noboolalpha Turns off boolalpha flag.
noshowbaseTurns off showbase flag.
noshowpoint Turns off showpoint flag.
noshowpos Turns off showpos flag.
nounitbuf Turns off unitbuf flag.
nouppercase Turns off uppercase flag.
oct Turns on oct flag. Turns off the dec and hex flags.
right Turns on right flag. Turns off the left and internal flags.
scientificTurns on scientific flag. Turns off the fixed flag.
showbase Turns on showbase flag.
showpoint Turns on showpoint flag.
showpos Turns on showpos flag.
unitbuf Turns on unitbuf flag.
uppercase Turns on uppercase flag.

To use a parameterized manipulator, you must include <iomanip>.

It defines the following manipulators:

ItemValue
resetiosflags (ios_base::fmtflags f) Turn off the flags specified in f.
setbase(int base) Set the number base to base.
setfill(int ch) Set the fill character to ch.
setiosflags(ios_base::fmtflags f) Turn on the flags specified in f.
setprecision (int p) Set the number of digits of precision.
setw(int w) Set the field width to w.
cout << setprecision(8) << left << 123.23;
cout << setw(12) << fixed << showpos << 98.6 << setw(10) << avg;

The same result can be obtained by use of stream member functions, but in a less compact form:

cout.width(12);
cout.setf(ios::fixed, ios::floatfield);
cout.setf(showpos);
cout << 98.6;
cout.width(10);
cout << avg;

Example,

Demo Code

#include <iostream>
#include <iomanip>
using namespace std;
int main()//from   w  w w.j  av a 2s .  co  m
{
   cout << "Default format: " << 123.123456789 << endl;
   cout << "Fixed format with precision of 7: ";
   cout << setprecision(7) << fixed << 123.123456789 << endl;
   cout << "Scientific format with precision of 7: ";
   cout << scientific << 123.123456789 << endl;
   cout << "Return to default format: ";
   cout << resetiosflags(ios::floatfield) << setprecision(6)
   << 123.123456789 << "\n\n";
   cout << "Use a field width of 20:\n";
   cout << "|" << setw(20) << "Testing" << "|\n\n";
   cout << "Use a field width of 20 with left justification:\n";
   cout << "|" << setw(20) << left << "Testing" << "|\n\n";
   cout << "Returning to right justification.\n\n" << right;
   cout << "Booleans in both formats: ";
   cout << true << " " << false << " " << boolalpha
   << true << " " << false << "\n\n";
   cout << "Default: " << 10.0 << endl;
   cout << "After setting the showpos and showpoint flags: ";
   cout << showpos << showpoint  <<  10.0 << "\n\n";
   cout << "The setw manipulator is very useful when repeated field\n"
   << "widths must be specified. For example:\n";
   cout << setw(8) << "this" << endl <<  setw(8) << "is" << endl
   << setw(8) << "a" << endl << setw(8) << "column" << endl
   << setw(8) << "of" << endl << setw(8) << "words";
   return 0;
}

Result


Related Tutorials