Skip to main content
Engineering LibreTexts

8.4: Formatting Output

  • Page ID
    29080
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Formatted I/O in C++

    C++ helps you to format the I/O operations like determining the number of digits to be displayed after the decimal point, specifying number base etc.

    Example:

    • If we want to add + sign as the prefix of out output, we can use the formatting to do so:
      stream.setf(ios::showpos)  
      If input=100, output will be +100 
    • If we want to add trailing zeros in out output to be shown when needed using the formatting:
      stream.setf(ios::showpoint) 
      If input=100.0, output will be 100.000

    Note: Here, stream is referred to the streams defined in c++ like cin, cout, cerr, clog.

    There are two ways to do so:

    1. Using the ios class or various ios member functions.
    2. Using manipulators(special functions)
    1. Formatting using the ios members:

      The stream has the format flags that control the way of formatting it means Using this setf function, we can set the flags, which allow us to display a value in a particular format. The ios class declares a bitmask enumeration called fmtflags in which the values(showbase, showpoint, oct, hex etc) are defined. These values are used to set or clear the format flags.

      Few standard ios class functions are:

      1. width(): The width method is used to set the required field width. The output will be displayed in the given width
      2. precision(): The precision method is used to set the number of the decimal point to a float value
      3. fill(): The fill method is used to set a character to fill in the blank space of a field
      4. setf(): The setf method is used to set various flags for formatting output
      5. unsetf(): The unsetf method is used To remove the flag setting

    Additional ios functions can be found at  std::ios - ios - C++ Reference 

    #include <iostream>
    using namespace std; 
    
    int main() 
    { 
    	char c = 'A'; 
    	
    	// Adjusting width will be 5. 
    	cout.width(5); 
    	cout << c <<"\n"; 
    		
    	int temp = 10; 
    	
    	// Width of the next value to be 
    	// displayed in the output will 
    	// not be adjusted to 5 columns. 
    	cout << temp << endl; 
    
        return 0;
    } 

    The output spaces the 'A' character 5 spaces to the right. The spacing is NOT valid in subsequent cout statements

         A
    10
    

    Following is an expanded program that uses the ios class functions. You can see the impact of the various ios functions

    #include<iostream> 
    using namespace std; 
    // The width() function defines width 
    // of the next value to be displayed 
    // in the output at the console. 
    int main() 
    {
    
        cout << "Implementing ios::precision\n\n"; 
        cout << "Implementing ios::width\n"; 
        cout.setf(ios::fixed, ios::floatfield); 
        cout.precision(2); 
        cout<<3.1422; 
        cout << "\n--------------------------\n"; 
    
        // The fill() function fills the unused 
        // white spaces in a value (to be printed 
        // at the console), with a character of choice. 
        cout << "Implementing ios::fill\n\n"; 
        char ch = 'a'; 
        
        // Calling the fill function to fill 
        // the white spaces in a value with a 
        // character our of choice. 
        cout.fill('*'); 
    
        cout.width(10); 
        cout<<ch <<"\n"; 
    
        int i = 1;     
        // Once you call the fill() function, 
        // you don't have to call it again to 
        // fill the white space in a value with 
        // the same character. 
        cout.width(5); 
        cout<<i; 
        cout << "\n--------------------------\n"; 
    
        cout << "Implementing ios::setf\n\n"; 
        int val1 = 100,val2 = 200; 
        cout.setf(ios::showpos); 
        cout<<val1<<" "<<val2; 
        cout << "\n--------------------------\n"; 
    
    
        cout << "Implementing ios::unsetf\n\n"; 
        cout.setf(ios::showpos|ios::showpoint); 
        // Clear the showflag flag without 
        // affecting the showpoint flag 
        cout.unsetf(ios::showpos); 
        cout << 200.0; 
        cout << "\n--------------------------\n"; 
    
        return 0; 
    } 

    The output show the formatting that can be accomplished...

    Implementing ios::precision
    Implementing ios::width
    
    3.14
    --------------------------
    
    Implementing ios::fill
    
    *********a
    ****1
    --------------------------
    
    Implementing ios::setf
    
    +100 +200
    --------------------------
    
    Implementing ios::unsetf 
    200.00
    --------------------------

    Adapted from:
    "Formatted I/O in C++" by Somil SinghGeeks for Geeks is licensed under CC BY-SA 4.0 


    This page titled 8.4: Formatting Output is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.