Formatted I/O

Tom Kelliher, CS17

Apr. 12, 1996

Some guidelines for the homework program:

  1. 3--5 separate functions.
  2. At least one function uses formal parameters and is called at least twice with different sets of actual parameters.
  3. At least one function returns a value.

Interactive program: uses standard input, output devices for I/O.

Batch program: uses disk files for I/O.

Standard devices can be changed through ``redirection.''

Output Formatting

Must include additional library file:

#include <iomanip.h>


  1. endl
  2. setw(columns) --- next value will be printed in a field columns wide:
    cout << setw(10) << balance << setw(10) << interest;
  3. setfill(character) --- determine the fill character for subsequent print fields:
    cout << setw(4) << setfill('0') << i;
  4. setprecision(digits) --- how many fractional digits of a floating point value to print (rounding):
    cout << setprecision(2) << balance;
    Why didn't it work?
  5. setiosflags(flag)/ resetiosflags(flag) --- set/reset specific formatting flags:
    1. ios::fixed --- Display floating point values in conventional rather than scientific notation.
      cout << setw(10) << setprecision(2) << setiosflags(ios::fixed)
           << balance;
    2. ios::left/ ios::right --- print justification.
    3. ios::showpoint --- always print a decimal point.
    4. ios::showpos --- print a positive sign.
    5. ios::scientific --- print floating point values in scientific form.

Example Output Function

Write a function which will print a double value in a field of width 15, with a fill character of '#', and always with a decimal point.

void formattedPrint(double value)
   cout << setw(15) << setfill('#') << setiosflags(ios::showpoint)
        << value;

   // Return formatting to original state.
   cout << setfill(' ') << resetiosflags(ios::showpoint);

How could you use the function to print 3 values on each of 3 lines?

How could you modify the function to make the field width and fill character changeable from the calling function?

Example situations:

Input Formatting


Baseball statistics: batting average, age

int age;
double avg;

cin >> avg;
cin >> age;
cin >> avg >> age;

How should the input look?

.333 25
Use whitespace to separate.

More statistics: batting average, position, age

int age;
double avg;
char pos;

cin >> avg >> pos >> age;

How should the input look?

.333 p 28
.333p 28
Character data is a pain.

Suppose we need to be able to read whitespace?

char c;
c = cin.get();

Redirecting Standard Input and Standard Output

Program cal prints a calendar to standard output (monitor).

Can we store the output in a file?

Program sum sums 10 inputs:

#include <iostream.h>

int main()
   int count = 0;
   int sum = 0;
   int current;

   while (count < 10)
      cin >> current;
      sum += current;
      ++ count;

   cout << "The sum is " << sum << endl;
   return 0;
Can we get its input from a file?

Can we get its input from a file and send its output to a file?


Programming problems 1--3 on pp. 337--338.

Thomas P. Kelliher
Thu Apr 11 22:07:35 EDT 1996
Tom Kelliher