Jose Luis Landivar Jose Luis Landivar - 2 months ago 20
C++ Question

GPA Calculator. Problems with OR and dividing by 0

I am writing a GPA calculator. I am having issues with the OR (||) function for checking if the input for credit is correct or no (If incorrect, it should ask again). Also, when the input for grades is "I" or "W" the program should not use that information (it should do nothing about it).
Thank you before hand for the help.
Here is the code:

// Include Statements
#include <iostream>
using namespace std;

//Program to calculate GPA
int main()
{
// Program made by Jose Luis Landivar
// Fall 2016
cout << ""<< endl;
cout << "The following program will calculate your GPA "<< endl<<'\n';

char grade;
int g = 1;
//variables for the algebraic operations needed
int credit = 0;
int totalcredit = 0;
int ptotal = 0;
double gpa, p = 1;
double count = 1;
double nclass;
string coursename, coursenumber;


cout << "Please enter the number of classes for the GPA calculator"<< endl;
cin >> nclass;
cout << ""<< endl;
while (count <= nclass ) // While
{
//Information for the class
cout << "Please enter the course name for the class # "<< count << endl;
cin >> coursename;
cout << "Please enter the course number for the class # "<< count << endl;
cin >> coursenumber;

//Getting grades and credits from user
cout << "Enter the grade for the class # "<< count <<" (A,B,C,D,F,I,W)"<< endl;
cin >> grade;
if (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D' || grade == 'F' || grade == 'I' || grade == 'W')

{

// If for grades
if (grade == 'A')
{
g = 4;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}


if (grade == 'B')
{
g = 3;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}

if (grade == 'C')
{
g = 2;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}

if (grade == 'D')
{
g = 1;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}

if (grade == 'F')
{
g = 0;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}

count = count + 1;
}

else {cout << "Input is incorrect\n";
}
}
//algebraic formulas
gpa = ptotal/totalcredit;

//output results
cout << "Your total number of credits is: " << totalcredit << endl;
cout << "Your GPA is: " << gpa << endl;

if (grade == 'I')
{
}
if (grade == 'W')
{
}
return 0;
}

Answer

Be careful when comparing int to char. For example:

int credit = 3;

// THIS WILL FAIL
if (credit == '3') {
}

an int of 3 and a char of '3' will NOT be equal. Although you can compare a char and an int they dont have the same value. For instance

cout << "Int : " << int(3) << std::endl;
cout << "Char: " << int('3') << std::endl;

gives the output:

Int : 3
Char: 51

You should change every occurance of

if (credit == '5' || credit == '4' || credit == '3' || credit == '2' || grade == '1'){
                                                                     // ^^^ is this suppose to be "credit"? 

to

if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){

UPDATE
Note that your code is never actually doing anything when "I" or "W" is provided (other than incrementing the course count variable as it should). However, when EVERY course is either "I" or "W" (i.e. all courses are (I)ncomplete or (W)ithdrawl) then your totalcredit variable will be equal to 0 giving you the 'divide by 0' error. Change

//algebraic formulas
gpa = ptotal/totalcredit;

to

if (totalcredit > 0) {
   gpa = ptotal/totalcredit;
} else {
   gpa = 0;
}

The else statement probably isnt necessary (gpa will already be zero), but it makes it explicit exactly what you want the GPA to be when there are no valid credits to compute the GPA from.

Comments