Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this GPA calculator that I am working on and it compiles with no errors.

ID: 668587 • Letter: I

Question

I have this GPA calculator that I am working on and it compiles with no errors. Say I enter 9 hours for the semester it is wanting me to enter 9 classes, how can I fix it so that when I reach 9 hours worth of classes it stops and calculates your GPA (it needs to work the same no matter how many hours you get) So if you have 12 hours it needs to stop and calculate after you have entered 12 hours. Also how can I get it to calculate GPA to two decimal places?

#include<iostream>
#include<string>
using namespace std;

int main()
{
int i;
int totalScore = 0;
int nhours, score, credits, courseScore;
   float gpa;
char grade;

cout << "Welcome to your GPA calculator: " << endl;

cout << "Enter the total number of hours you took: ";
cin >> nhours;

for(i = 0; i < nhours; i++)
{
cout << "Enter Hours of your " << (i+1) << " course:";
cin >> credits;
cout << "Please enter your grade: ";
cin >> grade;

if(grade == 'A') score = 4;
else if(grade == 'B') score = 3;
else if(grade == 'C') score = 2;
else if(grade == 'D') score = 1;
else if(grade == 'E') score = 0;
else score = 0;

courseScore = score * credits;
cout << "Your score is: " << (courseScore) << endl;

totalScore += courseScore;
}
gpa = totalScore / nhours;
cout << "Your GPA is: " << gpa << endl;
cin >> grade;

Explanation / Answer

The problem with your code is, you are trying to run the loop based on nhours, for nhours number of times, (nhours is the total number of hours you took). But you are reading the number of hours for each class into the variable credits and you are not considering this to run the loop.

One more thing is that, you didn't specified, whether credits are based on the number of hours you took the course. If so, the inputs you read is enough. If not, you have to read one more input, that is the number of hours per course(different from that of the credits.)

To set the precision only upto 2 digits of a floating point number by using the function setprecision()

The modified code is:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i=0,n=0;
int totalScore = 0;
int nhours, score, credits, courseScore,courseHours;
float gpa;
char grade;
cout << "Welcome to your GPA calculator: " << endl;
cout << "Enter the total number of hours you took: ";
cin >> nhours;
//for(i = 0; i < nhours; i++)
while(i < nhours)
{
cout << "Enter Hours of your " << "course "<< (n+1)<<": " ;
cin >> courseHours;
i += courseHours;
cout << "Enter the credits of your " << "course:"<< (n+1)<<": " ;
n++;
cin >> credits;
cout << "Please enter your grade: ";
cin >> grade;
if(grade == 'A') score = 4;
else if(grade == 'B') score = 3;
else if(grade == 'C') score = 2;
else if(grade == 'D') score = 1;
else if(grade == 'E') score = 0;
else score = 0;
courseScore = score * credits;
cout << "Your score is: " << (courseScore) << endl;
totalScore += courseScore;
}
gpa = (float)totalScore / nhours;
cout << "Your GPA is: " << setprecision(2) << fixed <<gpa << endl;
}