Format output using cout (C++)


Match word(s).

If you have any questions or comments,
please visit us on the Forums.

FAQ > How do I... (Level 1) > Format output using cout (C++)

This item was added on: 2003/03/22

cout has many manipulators that allow specific formatting of output. One can set flags explicitly with std::ios_base member functions:


#include <iostream> 

int main()
{
  std::cout.setf ( std::ios_base::right, std::ios_base::basefield );
  std::cout.width ( 10 );
  std::cout<< "Test" <<std::endl;
}

/*
 * Output
       Test
 *
 */


Some of the flags that can be controlled are:


skipws      // skip whitespace on input
left        // pad after the value
right       // pad before the value
internal    // pad between sign and value
boolalpha   // use the symbolic representation of true and false
dec         // integer base
hex         // hexadecimal base
oct         // octal base
scientific  // floating point notation d.ddddEdd
fixed       // d.dd
showbase    // show the base prefix such as 0 or 0x
showpoint   // print trailing zeros
showpos     // show + for positive values
uppercase   // E, X instead of e, x
adjustfield // flags related to field adjustment
basefield   // flags related to integer base
floatfield  // flags related to floating-point output
unitbuf     // flush output after every operation


These flags can be manipulated with member functions such as:


flags()                              // Read flags
flags ( fmtflags f )                 // Set flags
setf  ( fmtflags f )                 // Add flag
setf  ( fmtflags f, fmtflags mask ); // Add flag
unsetf( fmtflags mask )              // Clear flags

width()                              // Get field width
width ( streamsize wide )            // Set field width
fill()                               // Get filler character
fill  ( Ch ch )                      // Set filler character
precision()                          // Get precision
precision ( streamsize n )           // Set precision


Alternatively, output manipulators can be used.


#include <iostream> 
#include <iomanip> 

int main()
{
  std::cout<< std::setiosflags ( std::ios_base::right )
           << std::setw ( 10 ) << "Test" <<std::endl;
}

/*
 * Output
       Test
 *
 */


Manipulators tend to be cleaner and safer than explicitly setting flags. Some of the standard manipulators declared in <ios>, <iostream> and <iomanip> are:


boolalpha   // cout.setf ( ios_base::boolalpha )
noboolalpha // cout.unsetf ( ios_base::boolalpha )

showbase    // cout.setf ( ios_base::showbase )
noshowbase  // cout.unsetf ( ios_base::showbase )

showpoint   // cout.setf ( ios_base::showpoint )
noshowpoint // cout.unsetf ( ios_base::showpoint )

showpos     // cout.setf ( ios_base::showpos )
noshowpos   // cout.unsetf ( ios_base::showpos )

skipws      // cout.setf ( ios_base::skipws )
noskipws    // cout.unsetf ( ios_base::skipws )

uppercase   // cout.setf ( ios_base::uppercase )
nouppercase // cout.unsetf ( ios_base::uppercase )

left        // cout.setf ( ios_base::left, ios_base::basefield )
right       // cout.setf ( ios_base::right, ios_base::basefield )
internal    // cout.setf ( ios_base::internal, ios_base::basefield )

dec         // cout.setf ( ios_base::dec, ios_base::basefield )
hex         // cout.setf ( ios_base::hex, ios_base::basefield )
oct         // cout.setf ( ios_base::oct, ios_base::basefield )

fixed       // cout.setf ( ios_base::fixed, ios_base::floatfield )
scientific  // cout.setf ( ios_base::scientific, ios_base::floatfield )

endl        // put '\n' and flush
ends        // put '\0'
flush       // flush stream
ws          // eat whitespace

resetiosflags ( fmtflags f ) // Clear flags
setiosflags ( fmtflags f )   // Set flags
setbase ( int base )         // integer base
setfill ( int ch )           // Fill padding with ch
setprecision ( int n )       // n digits of floating point precision
setw ( int n )               // next field width is length n


Those that are not shown to take arguments are to be used as follows:


#include <iostream> 
#include <iomanip> 

int main()
{
  int val = 123;

  std::cout<< val <<" "<< std::oct << val <<" "<< std::hex << val <<std::endl;
}


The user programmer also has the ability to define their own manipulators, this is a more advanced topic, however, so it will be discussed later.

Credit: Prelude

Script provided by SmartCGIs