Homework 11 Solution

Write a program that reads in a file like input.txt that contains the names of several students along with their hw, quiz and exam averages and prints out the names of the students followed by their overall average given the weighting 20%, 20% and 60%, respectively. For example, on input.txt the program should produce:
Brown   71.8
Green   93.8
Jones   82.4
Smith   86.2
Worth   91.2

Your program should work for any file in the same format, though you may hard-code the filename, rather than read it in from the user if you prefer. Turn In: a printout of your source code and a screen capture of the output of your program on this input file: test.txt. (Note: producing your output as an HTML-table might not be bad practice for you!)

Solution

//KGS
// Hmwk11
// This calculates and average and prints it to the screen and to an html file.

#include <iostream>
#include <fstream>
#include <string>
using namespace
std;

int main(){
    string junk,name;
    int hw,quiz,exam;

    string filename;
    cout << "Enter file name: ";
    cin >> filename;

    ifstream fin (filename.c_str());
    //Check to ensure file was opened.
    if (!fin) {
        cout << "File did not open." << endl;
        exit (1);
    }

    // Set up the html file
    ofstream fout ("output.htm");
    fout << "<html>" << endl;
    fout << "<body>" << endl;
    fout << "<table border=2>" << endl;

    double avg;
    //Read the labels
    fin >> junk >> junk >> junk >> junk;
    while (fin >> name){
        fin >> hw >> quiz >> exam;
        avg = 0.2 * hw + 0.2 * quiz + 0.6 * exam;
        // Print to the screen
        cout << name << '\t' << avg << endl;
        // Print to the html file
        fout << "<tr>" << endl;
        fout << "<td>" << name << "<td>" <<avg;
    }

    fout << "</table>" << endl;
    fout << "</body>" << endl;
    fout << "</html>" << endl;
    return 0;
}