How do I get this code to where the credit hours for grade of W, I, or any other
ID: 670174 • Letter: H
Question
How do I get this code to where the credit hours for grade of W, I, or any other letter grade is not calculated in the final GPA?
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i=0;
float totalScore=0;
float nclasses, score, coursescore, coursehours;
string coursename;
string coursenumber;
char coursegrade;
float gpa,totalhours;
char grade;
cout << "Enter the total number of classes you want to calculate: ";
cin >> nclasses;
cout << "Please enter the proper information when prompted. Enter course grades as (A, B, C, D, F, W, I), Enter 0 for credit hours for courses with W or I: " << endl; //Had students enter 0 for credit hours for classes with W or I since they received no credit hours for the course
for(i = 0; i < nclasses; i++){
cout << endl << endl;
cin.ignore(); //to skip the new line character
cout << "Enter course " << i+1 <<": ";
getline(cin,coursename); //With this they can enter as many classes as they want and the program will count up as many as needed
cout << "Enter your course number: ";
getline(cin,coursenumber);
cout << "Enter your course grade: ";
cin >> coursegrade;
cout << "Enter course credit hours: ";
cin >> coursehours;
grade = coursegrade;
if(coursehours > 0 && coursegrade > 0){
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 == 'F') score = 0;
else if(grade == 'W'); //not used in calculation
else if(grade == 'I'); //not used in calculation
else{ //if student enters any letter grade other than what is listed above the following message will occur
cout << "This class will not be used in GPA calculation.";
}
}
coursescore = score * coursehours;
totalScore += coursescore;
totalhours += coursehours;
}
gpa = (float)totalScore / totalhours;
cout << "Your GPA is: " << gpa << endl;
return 0;
}
Explanation / Answer
You need to simply make coursehours=0 for grades W and I so that they are not taken into consideration.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i=0;
float totalScore=0;
float nclasses, score, coursescore, coursehours;
string coursename;
string coursenumber;
char coursegrade;
float gpa,totalhours;
char grade;
cout << "Enter the total number of classes you want to calculate: ";
cin >> nclasses;
cout << "Please enter the proper information when prompted. Enter course grades as (A, B, C, D, F, W, I), Enter 0 for credit hours for courses with W or I: " << endl; //Had students enter 0 for credit hours for classes with W or I since they received no credit hours for the course
for(i = 0; i < nclasses; i++){
cout << endl << endl;
cin.ignore(); //to skip the new line character
cout << "Enter course " << i+1 <<": ";
getline(cin,coursename); //With this they can enter as many classes as they want and the program will count up as many as needed
cout << "Enter your course number: ";
getline(cin,coursenumber);
cout << "Enter your course grade: ";
cin >> coursegrade;
cout << "Enter course credit hours: ";
cin >> coursehours;
grade = coursegrade;
if(coursehours > 0 && coursegrade > 0){
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 == 'F') score = 0;
else if(grade == 'W') coursehours=0; //make the credit hours to 0 so that they are not used in calculation
else if(grade == 'I') coursehours=0; //make the credit hours to 0 so that they are not used in calculation
else{ //if student enters any letter grade other than what is listed above the following message will occur
cout << "This class will not be used in GPA calculation.";
}
}
coursescore = score * coursehours;
totalScore += coursescore;
totalhours += coursehours;
}
gpa = (float)totalScore / totalhours;
cout << "Your GPA is: " << gpa << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.