#include #include #include #include using namespace std; struct Student { int id; //4 digit student id double score; //overall score as a percentage char grade; //course letter grade double hw1; //homework scores double hw2; double hw3; int qz1; //quiz scores int qz2; int qz3; int mt; //midterm score int fn; //final score }; const double HW_WEIGHT = 0.08; const double QZ_WEIGHT = 0.15; const double MT_WEIGHT = 0.31; const double FN_WEIGHT = 0.46; const double HW_TOT = 15.0; const double QZ_TOT = 30.0; const double MT_TOT = 100.0; const double FN_TOT = 100.0; void get_scores(Student& stud, ifstream& fin); //preconditions: Passed Student object has been declared and the input file // has been connected to fin. //postconditions: Scores for all assignments have been entered into // appropriate member variable and id has been entered. void calc_grade(Student& stud); //preconditions: Passed Student object has scores for all assignments. //postconditions: score and grade has been calculated using global constants // for assignment weights. void output_student(Student stud, ofstream& fout); //preconditions: Student's scores and grades have been entered and calculated. //postconditions: Student's scores and grades have been output in nice // format to the file connected to fout. void output_heading(ofstream& fout); //preconditions: fout has been succesfully opened for writing and no scores // have been written yet. //postconditions: Properly formatted heading has been output to file. //You can put another function declaration here for a function that would calculate //and output the class avg and the number of each grade. This function would need to be passed //the entire array along with the ofstream object for output. int main() { ifstream infile; ofstream outfile; infile.open("scores.txt"); // will need to change the name of this file for as8 if (infile.fail()) { cout << "Error opening input file\n"; exit(1); } outfile.open("grades.txt"); // will need to change the name of this file for as8 if (outfile.fail()) { cout << "Error opening output file\n"; exit(1); } Student student1; // need to declare an array of Students here output_heading(outfile); // outputs heading for output file get_scores(student1, infile); // These function calls get scores, calculate the grade, calc_grade(student1); // and output the scores and grade for one student. output_student(student1, outfile); // To do all this for a class, you could put these inside // a for loop and then you wouldn't have to make any changes // to the functions. Each iteration of the for loop would // take care of one student. Don't forget to change the // student1 variable to an element of the Student array. // after the for loop, you would then need to call the function that calculates the class // average and number of each grade. infile.close(); outfile.close(); return 0; } void get_scores(Student& stud, ifstream& fin) { fin >> stud.id >> stud.hw1 >> stud.hw2 >> stud.hw3 >> stud.qz1 >> stud.qz2 >> stud.qz3 >> stud.mt >> stud.fn; } void calc_grade(Student& stud) { double hw_avg = (stud.hw1 + stud.hw2 + stud.hw3)/HW_TOT; double qz_avg = (stud.qz1 + stud.qz2 + stud.qz3)/QZ_TOT; stud.score = ( (qz_avg * QZ_WEIGHT) + (hw_avg * HW_WEIGHT) + ((stud.mt / MT_TOT) * MT_WEIGHT) + ((stud.fn / FN_TOT) * FN_WEIGHT) ) * 100; if (stud.score >= 90) { stud.grade = 'A'; } else if(stud.score >= 80) { stud.grade = 'B'; } else if(stud.score >= 70) { stud.grade = 'C'; } else if(stud.score >= 60) { stud.grade = 'D'; } else { stud.grade = 'F'; } } void output_student(Student stud, ofstream& fout) { fout << setw(4) << stud.id << setw(4) << stud.hw1 << setw(5) << stud.hw2 << setw(5) << stud.hw3 << setw(5) << stud.qz1 << setw(5) << stud.qz2 << setw(5) << stud.qz3 << setw(5) << stud.mt << setw(5) << stud.fn << setw(7) << static_cast(stud.score) << setw(6) << stud.grade << endl; } void output_heading(ofstream& fout) { fout << setw(4) << "id " << setw(5) << "HW 1" << setw(5) << "HW 2" << setw(5) << "HW 3" << setw(5) << "Qz 1" << setw(5) << "Qz 2" << setw(5) << "Qz 3" << setw(5) << "MT " << setw(5) << "FN " << setw(8) << "Course" << setw(6) << "Grade" << endl; fout << setw(4) << "----" << setw(5) << "----" << setw(5) << "----" << setw(5) << "----" << setw(5) << "----" << setw(5) << "----" << setw(5) << "----" << setw(5) << "----" << setw(5) << "----" << setw(8) << "------" << setw(6) << "-----" << endl << endl; }