Exercise #5
CO 110 Fall '99

Below and on the back page is a program that processes a file containing the scores students have made on five quizzes. Each line of the file looks like this sample for John Doe - five scores and a name

38  44  33  41  46 John Doe

The program asks the person at the keyboard to indicate which score is to be evaluated (by entering a number 1 thru 5). The program also asks for a "range" value from the person at the keyboard. It then reads the designated score for each student and computes the average for that score. Finally, the program rereads the file and displays the names and scores of all students within "range" points of average for the selected score. The comments in the program explain more.

Your task is to rewrite the program so that the major tasks are done by functions. This should result in a main function that contains only basic control actions and invokes other functions to do most of the work. You should NOT radically alter the statements you are given - this program works. Simply rearrange the statements into functions and create function prototypes, headers, and invocations.

#include <fstream.h>
const int HOW_MANY = 5;         // Number of scores per student
int main ()
{
    ifstream       theData;        // File stream object
    int            scoreSum;        // For sum of scores
    float          scoreAverage;    // For average
    int            aScore;        // One score from file
    int            junk;        // A score being ignored
    int            selection;    // Choice of which score to use
    int            counter;        // Loop counter
    int            studentCount;    // Number of students
    float          range;        // Entered range value
    char           letter;        // A letter of a name
                            // Get the score selection
    cout << "Which score should be used (1-" << HOW_MANY << ")? ";
    cin >> selection;            // Must be reasonable
    while (selection < 1 || selection > HOW_MANY)
    {
        cout << "Which score should be used (1-"
            << HOW_MANY << ")? ";
        cin >> selection;
    }
    cout << "What range should be used? ";    // Get the range
    cin >> range;
    while (range <= 0.0)                // Must be positive
    {
        cout << "What range should be used? ";
        cin >> range;
    }
    theData.open("datafile.txt");        // Open the file and
    scoreSum = 0;                    // initialize values
    studentCount = 0;
    while (theData)                // Loop to end of file
    {
        counter = 0;
        while (counter < selection - 1)    // Skip unused scores
        {
            theData >> junk;
            counter++;
        }
        theData >> aScore;            // Get a student's score
        scoreSum = scoreSum + aScore;    // Add to the sum
        studentCount++;            // Count the student
        theData.ignore(100,'\n');    // Discard rest of line
    }
    theData.close();                // Compute average
    scoreAverage = float (scoreSum) / float (studentCount);
    cout << "Students within " << range << " points of "
        << "the average: " << scoreAverage
        << " for score " << selection << endl;
    theData.open("datafile.txt");        // Start the file over again
    while (theData)                // Loop to end of file
    {
        counter = 0;
        while (counter < selection - 1)    // Skip unused scores
        {
            theData >> junk;
            counter++;
        }
        theData >> aScore;                // Get student's score
                            // Check if score is within range
        if (aScore <= scoreAverage + range &&
                aScore >= scoreAverage - range)
        {
            counter++;
            while (counter < HOW_MANY)    // Skip other scores
            {
                theData >> junk;
                counter++;
            }
            theData.get(letter);        // Get space before name
            theData.get(letter);
            while (letter != '\n' && theData)    // Loop to read and
            {                            // display the name
                cout << letter;
                theData.get(letter);
            }
            cout << "   " << aScore << endl;    // Display the score
        }
        else
            theData.ignore(100, '\n');    // If not in range, ignore
    }
    theData.close();
    return 0;
}