Tom Kelliher, CS17
Apr. 12, 1996
Some guidelines for the homework program:
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.''
Must include additional library file:
#include <iomanip.h>
Manipulators:
cout << setw(10) << balance << setw(10) << interest;
cout << setw(4) << setfill('0') << i;
cout << setprecision(2) << balance;Why didn't it work?
cout << setw(10) << setprecision(2) << setiosflags(ios::fixed) << balance;
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:
Baseball statistics: batting average, age
int age; double avg; cin >> avg; cin >> age;or:
cin >> avg >> age;
How should the input look?
.333 25 .33325Use 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 28Character data is a pain.
Suppose we need to be able to read whitespace?
char c; cin.get(c); c = cin.get();
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.