Write a program that reads from a file a list of course names, letter grades for
ID: 3643604 • Letter: W
Question
Write a program that reads from a file a list of course names, letter grades for each course and number of credits each course is worth. The program will read each course name, grade earned and number of credits from the file and print out the course name, grade earned and number of credits for each course to the screen. After the program has read in all of the courses, grades earned and credits, the program should calculate the grade point average based on all of the courses read in by the program.Notes: Students who receive an
Explanation / Answer
Solution:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void calcGPA(ifstream& in_file, ofstream& out_file, float& avg);
char calc_grade(float avg);
int main() {
ifstream inp;
ofstream outp;
inp.open("Grades_session07.txt" );
outp.open("Grades_session07.txt" );
outp << "Student Test1 Test2 Test3 Test4 Test5 Average Grade";
string name;
float classAvg = 0;
char grade;
int k;
float avg;
for (k=1; k<=10; k++)
{
inp >> name;
outp << setw(10) << name;
calcGPA(inp, outp, avg);
outp << setw(6) << avg;
classAvg = classAvg + avg;
grade = calc_grade(avg);
outp << setw(5) << " " << grade << endl;
}
classAvg = classAvg / 10;
outp << endl << "Class Average = " << classAvg;
return 0;
}
void calcGPA(ifstream& inp, ofstream& outp, float& avg)
{
int score, k;
float sum = 0.0;
for (k=1; k<=5; k++)
{
inp >> score;
outp << setw(4) << score << " ";
sum = sum + score;
}
avg = sum / 5;
}
char calc_grade(total)
{
if (total >= 0)
if (total >= 60)
if (total >= 70)
if (total >= 80)
if (total >= 90)
if (total > 100)
cout << "Grade can not exceed 100" << endl;
else
cout << "A" << endl;
else
cout << "B" << endl;
else
cout << "C" << endl;
else
cout << "D" << endl;
else
cout << "F" << endl;
else
cout << "Grade can not be a negative number" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.