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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    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.