< previous page page_172 next page >

Page 172
    cout.setf(ios::fixed, ios::floatfield);   // Set up floating pt.
    cout.setf(ios::showpoint);                //   output format

    // Prepare file for reading

    scoreFile.open(scores.dat);

    // Get data

    scoreFile >> test1 >> weight1;
    scoreFile >> test2 >> weight2;
    scoreFile >> test3 >> weight3;

    // Print heading

    cout << Test Score  Weight << endl << endl;

    // Print data

    cout << setw(7) << test1
         << setw(11) << setprecision(2) << weight1 << endl;
    cout << setw(7) << test2 << setw(11) << weight2 << endl;
    cout << setw(7) << test3 << setw(11) << weight3 << endl;

    // Find weighted average

    ave = test1 * weight1 + test2 * weight2 + test3 * weight3;

    // Print weighted average

    cout << endl;
    cout << Weighted average =  << ave << endl;
    return 0;

}
If the file scoreFile (that is, the physical disk file scores.dat) contains this data:
90   0.30
85   0.25
78   0.45
the output from the program looks like this:

 
< previous page page_172 next page >