CPS 149s home

Input and Output

 

The standard C++ header iostream.h provides the objects

Common input forms are

#include <iostream.h>

void main()
{
    int num;
    char word[256];

    if (!( cin >> num >> word ))
        return;          // returns on failure

    char ch1, ch2;
    char line[1024];

    cin >> ch1;          // ch1 != whitespace
    cin.get( ch2 );      // ch2 may be whitespace
    cin >> ws;           // skips over whitespace
    if (!cin.getline( line, 1024 ))
        return;          // ret if no more chs in input
}

The standard C++ header iomanip.h adds output format control.

Output padding may be controlled using

Integer output is modified by

Floating point output can be changed with

#include <iostream.h>
#include <iomanip.h>

void main()
{
    double S = 0.000000123;
    double A = 456.7;
    double B = 8910000000000.0;

    cout << "default precision is "
         << cout.precision() << endl;	// 6

    cout << "    S = " << S << endl;	// 1.23e-07
    cout << "    A = " << A << endl;	// 456.7
    cout << "    B = " << B << endl;	// 8.91e+12

    cout << setiosflags(ios::showpoint)
         << "ios::showpoint ON" << endl;

    cout << "    S = " << S << endl;	// 1.23000e-07
    cout << "    A = " << A << endl;	// 456.700
    cout << "    B = " << B << endl;	// 8.91000e+12

    cout << resetiosflags(ios::showpoint)
         << "ios::showpoint OFF" << endl;
    cout << setiosflags(ios::fixed)
         << "ios::fixed ON" << endl;

    cout << "    S = " << S << endl;	// 0.000000
    cout << "    A = " << A << endl;	// 456.700000
    cout << "    B = " << B << endl;	// 8910000000000.000000

    cout << resetiosflags(ios::fixed)
         << setiosflags(ios::scientific)
         << "ios::scientific ON" << endl;

    cout << "    S = " << S << endl;	// 1.230000e-07
    cout << "    A = " << A << endl;	// 4.567000e+02
    cout << "    B = " << B << endl;	// 8.910000e+12
}
#include <iostream.h>
#include <iomanip.h>

void main()
{
    char * S = "z";
    char * L = "abcdef";
    int I = 123;

    cout << "    S = [" << S << ']' << endl;        // [z]
    cout << "    L = [" << L << ']' << endl;        // [abcdef]
    cout << "    I = [" << I << ']' << endl;        // [123]

    cout << "using width 5" << endl;
    cout << "    S = [" << setw(5) << S << ']' << endl;
                                                    // [    z]
    cout << "    L = [" << setw(5) << L << ']' << endl;
                                                    // [abcdef]
    cout << "    I = [" << setw(5) << I << ']' << endl;
                                                    // [  123]

    cout << setiosflags(ios::left)
         << "ios::left ON, using width 5" << endl;
    cout << "    S = [" << setw(5) << S << ']' << endl;
                                                    // [z    ]
    cout << "    L = [" << setw(5) << L << ']' << endl;
                                                    // [abcdef]
    cout << "    I = [" << setw(5) << I << ']' << endl;
                                                    // [123  ]

    cout << setfill( '#' )
         << "fill char '#', using width 5" << endl;
    cout << "    S = [" << setw(5) << S << ']' << endl;
                                                    // [z####]

    cout << "using default width" << endl;

    cout << "    octal I = " << oct << I << endl;   // 173
    cout << "    hex I = " << hex << I << endl;     // 7b
    cout << setiosflags( ios::uppercase )
         << "ios::uppercase ON" << endl;
    cout << "    hex I = " << hex << I << endl;     // 7B
    cout << "    decimal I = " << dec << I << endl; // 123
}

The standard C++ header strstream.h lets us read from and write to an in-memory buffer.

  • istrstream
  • Creation

    Extraction

  • while (IN >> enginesNeeded) ...
  • ostrstream
  • Creation

    Insertion

  • for (...) OUT << engineName[i] << ’ ’;
  • Termination

  • OUT << ends;
  • Access

  • process( OUT.str() );
  • Be careful not to accidentally use cin or cout!

    #include <iostream.h>
    #include <iomanip.h>
    #include <strstream.h>
    
    long defaultFlags;
    char defaultFill;
    int defaultPrecision;
    
    void restoreDefaults()
    {
        cout.flags( defaultFlags );
        cout.fill( defaultFill );
        cout.precision( defaultPrecision );
    }
    
    void main()
    {
        defaultFlags = cout.flags();
        defaultFill = cout.fill();
        defaultPrecision = cout.precision();
    
        int posInt = 455;
        int negInt = -711;
        double aDouble = 3.18;
        char * shortStr = "foo";
        char * longStr = "The quick brown fox jumped ...";
    
     
        //-------------------------------------------------
        // Output an integer with either a leading
        // '+' or a leading '-', as in
        // 
        //    The gross profit was $ +23     or
        //    The gross profit was $ -1000
    
     
        cout << setiosflags(ios::showpos);
        cout << "... was $ " << posInt << endl
             << "... was $ " << negInt << endl;
    
     
        // output:  ... was $ +455
        //          ... was $ -711
    
        restoreDefaults();
    
     
        //-------------------------------------------------
        // Output a natural number padded with leading
        // zeros to a width of 10 chars, as in
        //
        //    0000000023 + 0000151111 = 0000151134
    
     
        cout << setfill('0');
        cout << setw(10) << posInt << " + "
             << setw(10) << -negInt << " = "
             << setw(10) << posInt + -negInt << endl;
    
     
        // output:  0000000455 + 0000000711 = 0000001166
    
        restoreDefaults();
    
     
        //-------------------------------------------------
        // Output the results of a floating point
        // computation with 1 digit following the
        // decimal point, as in
        //
        //    0.1   or   -45678.9
    
     
        cout << setprecision(1)
             << setiosflags(ios::fixed)
             << aDouble << endl;
    
     
        // output:  3.2
    
        restoreDefaults();
    
     
        //-------------------------------------------------
        // Output the results of a floating point
        // computation with 11 significant digits:
        //
        //   56.700000000
    
     
        cout << setprecision(11)
             << setiosflags(ios::showpoint)
             << aDouble << endl;
    
     
        // output:  3.1800000000
    
        restoreDefaults();
    
     
        //-------------------------------------------------
        // Output the concatenation of a record's
        // fields, padding on the right with spaces to
        // a total width of 15 or truncating if the
        // resulting string is too long.
        
    
        ostrstream o1;
        o1 << posInt << ',' << shortStr << ','
           << longStr << setw(15) << ' ' << ends;
    
        cout.write( o1.str(), 15 );
        cout << endl;
    
     
        // output:  455,foo,The qui
    
     
     
        //-------------------------------------------------
        // Input a line with an integer followed
        // by a (possibly empty) line containing a
        // string.  No lines will be longer than
        // 500 chars.
    
     
        cin >> posInt;
        char buffer[1024];
        cin.getline( buffer, 1024 );
        cin.getline( buffer, 1024 );
    
        // Notice that the '\n' following the integer
        // is sucked up by the first getline, while
        // the actual line is read by the second call
        // to getline.
        }
    

    Nathan Bronson