#include <iostream>
#include <iomanip>
using namespace std;
/*******************************************************************************
*
* Project #2 – Repetition & Switch Statement
*
* This program will obtain from the user, a list of Letter Grades then display the
* Total Points and GPA then, and then loop 3 times.
*
*
*
*
* Computations are based on
* TotalPoints = Total Points + Grade Value
* GPA = Total Points / Grade Count
*
* _____________________________________________________________________________
* INPUT
* LetterGrade : Letter grades from A,B,C,D,F, or X to end list,
* entered by the User
*
*
*
* OUTPUT
* TotalPoints : Total accumulated points converted through the LetterGrades
* GPA : Grade Point Average for the list of Grades
*
*
******************************************************************************/
int main() {
char LetterGrade; // Letter Grades to be entered by the user
// Outer loop runs 3 times for the 3 test runs
for (int LoopCount = 0; LoopCount <= 2; LoopCount++) {
double TotalPoints = 0.0; // Total value of all letter grades in list
double GPA = 0.0; // Average of points in list
int GradeCount = 0; // Amount of grades entered by user
bool KeepGoing = true; // Flag to control grades entry loop
// Test header
cout << "TEST #" << (LoopCount + 1) << ":" << endl << endl;
// Continuously collects grades until 'X' or 'x' is entered
while (KeepGoing) {
cout << "Enter Letter Grade (enter 'X' to exit): " << endl;
cin >> LetterGrade; // Gathers Letter Grade inputs
switch (LetterGrade) { // Converting Letter Grades to numeric values
case 'A':
case 'a': // Included lowercase just in case users type lowercase
TotalPoints += 4.0;
GradeCount += 1;
break;
case 'B':
case 'b':
TotalPoints += 3.0;
GradeCount += 1;
break;
case 'C':
case 'c':
TotalPoints += 2.0;
GradeCount += 1;
break;
case 'D':
case 'd':
TotalPoints += 1.0;
GradeCount += 1;
break;
case 'F':
case 'f':
TotalPoints += 0.0;
GradeCount += 1;
break;
case 'X':
case 'x':
KeepGoing = false;
break;
default:
cout << endl << "Invalid letter grade, please try again" << endl << endl;
}
}
// Prevent division by zero if they enter X immediately
if (GradeCount > 0) {
GPA = TotalPoints / GradeCount;
} else {
GPA = 0.0;
}
// Displaying Total Points and GPA
cout << endl;
cout << "Total Grade Points: " << TotalPoints << endl;
cout << fixed << setprecision(2);
cout << "GPA: " << GPA << endl;
// Triple space between loops
if (LoopCount < 2) {
cout << endl << endl << endl;
}
}
return 0;
}