+1 (315) 557-6473 

Grade Calculator Program using C++ homework Solution


Streams, Multiple Files and Linear Search

Create a class, called Grader, based upon the following information:

Complete the c++ assignment program should prompt the user to enter file names of course files to process until the user enters stop. After reading each file it should print the entire content of the input file to the output file. There should be a new column added called Grade and each student should have a letter grade assigned to them. After each file has been processed you should then read through the output file in order to calculate the grade distribution, number of each letter for all files, and print it out at the bottom of the output file. Then, you should ask the user if they wish to find a specific student’s information and print it to the screen. If they select yes, you should then ask the user to enter a 4-digit id number which you will use to search through the output file and print the entire line for the student to screen. You should stop prompting the user for student ids when the user enters stop.

A breakdown of the class Grader:

Two Private Fields:

An ifstream is called input and an ofstream is called output. You will use the streams in this class to accomplish the file I/O.

Public Methods:

setOutputFile which returns nothing and takes as input a string for the file name of the output file, and an integer parameter to allow you to optionally open the file in a different model than the default. Hint: Since you are opening and closing the output file multiple times you could overwrite it. The mode should be set to 0 by default which will indicate a “normal/default” opening. Otherwise, you could open it with the app flag to append the data to the file.

closeOutputFile which returns nothing and takes no parameters. It simply closes the output file.

searchID which returns nothing and takes a string id of length 3 (e.g. “1234”) as a parameter. This method should open the closed output file as an input file and search line by line for a matching id. If the id does not exist then it should display a message indicating that no such student exists, otherwise it should print the entire student’s data line to screen.

readFile which returns nothing and takes a string file name as input. The file name should be a user-entered name of a file that contains formatted student grade data. This function should call the protected functions in order to calculate the various parts of the student grade as specified in each of the protected methods. This function should print the student information to the output file along with their final grade in the course.

calculateDistribution which returns nothing and takes no parameters. This function will read through the output file after all student grades have been calculated for every input file. It should count the grades per letter and display the information on a single line. See the sample output below. An A would be all A+, A, and A- grades.

Protected Methods:

calculateMidterms which returns a double and takes as input two doubles; one for each midterm exam (ex1 and ex2). Each exam is scored out of 100 pts. and the two exams together are worth 30% of the overall grade. You should return the amount out of the 30% the student has earned.

calculateFinal which returns a double and takes as input one double for the final exam (ex3). The final exam is scored out of 100 pts. and is worth 25% of the overall grade. You should return the amount out of the 25% the student has earned.

calculateProgs which returns a double and takes as input four doubles; one for each program. The programs are each scored out of 100 pts. and are worth 30% collectively. You should return the amount out of the 30% the student has earned.

calculateLabs which returns a double and takes as input 10 doubles; one for each lab. The labs are all scored out of 10 pts. each and are worth 15% collectively. You should return the amount out of the 15% the student has earned.

calculateGrade which returns a string and takes a string parameter as input. The method is called the readFile method in order to calculate a single student’s grade. The input string is the entire line of student data which it then parses, converts the various grades to doubles, and calls the other various calculate methods to get the student’s overall percent grade in the course. This method then returns a string letter grade based on the following criteria:

Grader 1

Note: If a student scores below a 60% then they receive an F.

FILE FORMATS:

An input file may have between 0 and infinite students

Grader 2

You may only have a single output file which should have the following format:

Grader 3

SAMPLE SCREEN OUTPUT:

Grader 4

Solution:

Grader:

#include "Grader.h" #include < sstream> Grader::Grader() { outputFilename = "cis200finalgrades.txt"; } void Grader::setOutputFile(string filename, ios_base::openmode mode) { outputFilename = filename; output.open(outputFilename, mode); } void Grader::closeOutputFile() { output.close(); } void Grader::searchID(string id) { ifstream inFile(outputFilename); string line; int counter = 0; while(getline(inFile, line)) { if (counter < 2) { counter++; continue; } stringstream s(line); string name; string lineId; s >> name; s >> name; s >> lineId; if (lineId == id) { cout << line << endl; inFile.close(); return; } } inFile.close(); cout << "No user with ID: " << id << " was found" << endl; } void Grader::readFile(string filename) { setOutputFile(outputFilename); input.open(filename); string line; int counter = 0; int len; while(getline(input, line)) { if (counter < 2) { if (counter == 1) { len = line.length(); string extLine = line + " Grade"; output << extLine << endl; } else { output << line << endl; } } else { string grade = calculateGrade(line); line.resize(len, ' '); string extLine = line + " " + grade; output << extLine << endl; } counter++; } input.close(); closeOutputFile(); calculateDistribution(); cout << "File: " << filename << " successfully processed!" << endl; } void Grader::calculateDistribution() { ifstream inFile(outputFilename); string line; int counter = 0; int a = 0, b = 0, c = 0, d = 0, f = 0; while(getline(inFile, line)) { if (counter < 2) { counter++; continue; } stringstream s(line); string name; string id; string grade; double ex1, ex2, ex3, p01, p02, p03, p04, l01, l02, l03, l04, l05, l06, l07, l08, l09, l10; s >> name; s >> name; s >> id; s >> ex1; s >> ex2; s >> ex3; s >> p01; s >> p02; s >> p03; s >> p04; s >> l01; s >> l02; s >> l03; s >> l04; s >> l05; s >> l06; s >> l07; s >> l08; s >> l09; s >> l10; s >> grade; switch (grade[0]) { case 'A': a++; break; case 'B': b++; break; case 'C': c++; break; case 'D': d++; break; default: f++; } } inFile.close(); setOutputFile(outputFilename, ofstream::app); output << "CIS 200 Grade Distribution A: " << a << " B: " << b << " C: " << c << " D: " << d << " F: " << f << endl; closeOutputFile(); } double Grader::calculateMidterms(double ex1, double ex2) { return 0.3 * (ex1 + ex2) / 2.0; } double Grader::calculateFinal(double ex3) { return 0.25 * (ex3) / 1.5; } double Grader::calculateProgs(double p01, double p02, double p03, double p04) { return 0.3 * (p01 + p02 + p03 + p04) / 4.0; } double Grader::calculateLabs(double l01, double l02, double l03, double l04, double l05, double l06, double l07, double l08, double l09, double l10) { return 0.15 * (l01 + l02 + l03 + l04 + l05 + l06 + l07 + l08 + l09 + l10) / 1.0; } string Grader::calculateGrade(string input) { stringstream s(input); string name; string id; double ex1, ex2, ex3, p01, p02, p03, p04, l01, l02, l03, l04, l05, l06, l07, l08, l09, l10; s >> name; s >> name; s >> id; s >> ex1; s >> ex2; s >> ex3; s >> p01; s >> p02; s >> p03; s >> p04; s >> l01; s >> l02; s >> l03; s >> l04; s >> l05; s >> l06; s >> l07; s >> l08; s >> l09; s >> l10; double sum = 0.0; sum += calculateMidterms(ex1, ex2); sum += calculateFinal(ex3); sum += calculateProgs(p01, p02, p03, p04); sum += calculateLabs(l01, l02, l03, l04, l05, l06, l07, l08, l09, l10); if (sum >= 97.0) { return "A+"; } else if (sum >= 93.0) { return "A"; } else if (sum >= 90.0) { return "A-"; } else if (sum >= 87.0) { return "B+"; } else if (sum >= 80.0) { return "B-"; } else if (sum >= 80.0) { return "B-"; } else if (sum >= 77.0) { return "C+"; } else if (sum >= 73.0) { return "C"; } else if (sum >= 70.0) { return "C-"; } else if (sum >= 67.0) { return "D+"; } else if (sum >= 63.0) { return "D"; } else if (sum >= 60.0) { return "D-"; } return "F"; }

Main:

#include < iostream> #include "Grader.h" using namespace std; int main() { Grader grader; string filename; string id; while(true) { cout << "Enter a name of a class file to read or stop to end: "; cin >> filename; if (filename == "stop") { break; } grader.readFile(filename); } while(true) { cout << "Enter ID to find and print record or stop to end: "; cin >> id; if (id == "stop") { break; } grader.searchID(id); } }