I have to write a program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s last name, then one space, then the student’s first name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is not greater than 100. Your program will read data from this file and write its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number at the end of each line: the average of the student’s ten quiz scores.
The output file must be formatted such that first and last names appear together in a left justified column that is 20 characters wide. Each quiz score should be listed in a right justified column that is 4 characters wide, and the average should appear in its own right justified column that is 10 characters wide.
Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10; these students are assumed to have missed one or more of the quizzes. The output file should contain a line (or lines) at the beginning of the file explaining the output in addition to appropriate column headings. Use formatting statements to make the layout clean and easy to read.
After writing the required data to an output file, your program will close all file streams and then copy the contents of the “output” file to the “input” file. Thus, the net effect of the program is to change the contents of the input file. Do not attempt to copy the output file to the input file until you have thoroughly tested and debugged the rest of your program to ensure that it operates correctly.
You should use at least two functions that include file streams in their parameter lists. (These functions may have other parameters as well.) The input file should be called quiz.txt and the output file should be called average.txt.
Some test cases you may want to consider are the following:
• What if the input file is empty?
• What if a student does not have any quiz grades at all?
• What if there are extra new lines at the end of the file?
• What if the last record in the file does not have a new line after it but rather ends with the end-of-file character?
*****heres the code I have....do I need to add anything to it or am I good with what I have?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
void take_avg(ifstream& fin, ofstream& fout);
//pre: fin and fout must be passed an open file.
//post: will print formatted scores and an average in average.txt.
void reprint(ifstream& fin, ofstream& fout);
//pre: fin and fout must be passed an open file.
//post: will copy what is in average.txt into quiz.txt.
int main() {
ifstream fin;
ofstream fout;
fin.open ("quiz.txt");
if (fin.fail()) {
cout << "Input file failed to open.";
exit(1);
}
fout.open ("average.txt");
if (fout.fail()) {
cout << "output file failed to open.";
exit(1);
}
take_avg(fin, fout);
fin.close();
fout.close();
fin.open ("average.txt");
if (fin.fail()) {
cout << "Input file failed to open.";
exit(1);
}
fout.open ("quiz.txt");
if (fout.fail()) {
cout << "output file failed to open.";
exit(1);
}
reprint(fin, fout);
fin.close();
fout.close();
}
void take_avg(ifstream& fin, ofstream& fout) {
string last;
while (fin >> last) {
string first, both;
int quiz, average(0);
fin >> first;
both = last + " " + first;
fout.setf(ios::left);
fout << setw(20) << both;
for (int n(0); n < 10; n++ ) {
fin >> quiz;
if (fin.fail()) {
fin.clear();
fout.setf(ios::right);
fout << setw(4) << "0";
}
else {
average = average + quiz;
fout.setf(ios::right);
fout << setw(4) << quiz;
}
}
if (fin.fail()) fin.clear();
average = average / 10;
fout.setf(ios::right);
fout << setw(10) << average << "\n";
fout.unsetf(ios::right);
}
}
void reprint(ifstream& fin, ofstream& fout) {
char ch;
fin.get(ch);
while (!fin.eof()) {
fout.put(ch);
fin.get(ch);
}
}
|