GPA Calculator: Write a C++ program that reads data from the input file (gradeData.txt) and then calculate GPA and
total credit hours. Print them out along with other information into an output file (transcript.txt)
in the required format given in the sample output file.
Use the following formula to calculate GPA:
\(
GPA=\dfrac{\sum_{k=1}^n G_k H_k} {\sum_{k=1}^n H_k } \\
\)
\(G_k\) is grade points earned for course k;
\(H_k\) is the number of credit hours for course k
For example:
Courses
|
Grades
|
Points
|
Hours
|
Points x Hours
|
CSC1321
|
B
|
3.0
|
3
|
9
|
ASE1311
|
A
|
4.0
|
3
|
12
|
EXS1220
|
D
|
1.0
|
2
|
2
|
PHY1401 |
C
|
2.0
|
4
|
8
|
ENG1301 |
F
|
0.0
|
3
|
0
|
\( GPA = \dfrac{3 \times 3.0 + 3 \times 4.0 + 2 \times 1.0 + 4 \times 2.0 + 3 \times 0.0 }{ 3 + 3 + 2 + 4+3} = \dfrac{31}{15} = 2.07 \)
//GPA Calculator
//GPA is a credit hour weighted average.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
double getPoints(char);
int main()
{
//Open input file
ifstream inData;
inData.open("gradeData.txt");
//Open output file
ofstream outData;
outData.open("transcript.txt");
//Declare variables for student name, semester, classes, grades, points, and credit hours
//More ...
//Read data from the input file
//More ...
//Hint: Use getline(inData, name) and getline(inData, semester) to read the first two lines containing student name and semester year.
//Get credit hours for each class. e.g. if class1 <= CSC1321, then hours1 <= 1
hours1 = class1.substr(4, 1).at(0) - '0';
//More ...
//Get points for each letter grade. e.g. if grade1 <= B, then points1 <= 3.0
points1 = getPoints(grade1);
//More ...
//Calculate total credit hours. Add all credit hours.
//More ...
//Calculate credit hour weighted average using GPA calculation formula
//More ...
//Print transcript into the output file named "transcript.txt" The format should be the same as the sample.
//Keep two fractional places for GPA value.
//More ...
return 0;
}
//Given a letter grade, it returns points earned.
//For example, it returns 4.0 points for a grade of A
double getPoints(char grade)
{
double pts = 0.0;
switch(grade)
{
case 'A':
pts = 4.0;
break;
case 'B':
pts = 3.0;
break;
case 'C':
pts = 2.0;
break;
case 'D':
pts = 1.0;
break;
case 'F':
pts = 0.0;
break;
default:
pts = 0.0;
}
return pts;
}
//Data file "gradeData.txt" contains the following:
Olivia Miyazawa
Fall 2023
CSC1321 B
ASE1311 F
EXS1220 D
PHY1401 C
ENG1302 A
//Output in transcript.txt:
//*********** Transcript of Texas Welseyan University ***********
//
// Student Name: Olivia Miyazawa
// Semester: Fall 2023
// Total Hours: 15
// Semester GPA: 2.07
//
//Classes taken and grades in Fall 2023:
//CSC1321 B
//ASE1311 F
//EXS1220 D
//PHY1401 C
//ENG1302 A
//***************************************************************