Reputation: 25
I have no programming experience and I'm taking a class called introduction to C++ for engineers. We are currently learning how to use if else statements and I've been stuck for a few days on why my program isn't executing correctly and where I made a mistake.
I need to create a program that calculates and displays the gpa for two classes taken in one term. I did not learn looping yet so I need to hardcode the calculation and input for the two classes. The problem is, when I input any grade letter for both classes, it always takes the first if statement as being true in both cases.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double gpa, qualityPoints, qualityPoints1, qualitypointsTotal, totalCredits, credits, credits1;
double cqPoints, cqPoints1;
char grade, grade1;
cout << "Grade Point Average Calculator" << endl << endl;
cout << "Enter the grade letter received for the first class: " << endl;
cin >> grade;
if (grade = 'A')
{
qualityPoints = 4.0;
}
else if (grade = 'B')
{
qualityPoints = 3.0;
}
else if (grade = 'C')
{
qualityPoints = 2.0;
}
else if (grade = 'D')
{
qualityPoints = 1.0;
}
else if (grade = 'F')
{
qualityPoints = 0.0;
}
else
{
cout << "Invalid Letter Grade" << endl;
}
cout << "How many credit hours was the class worth?" << endl;
cin >> credits;
cqPoints = qualityPoints * credits;
cout << "Enter the grade letter received for the second class: " << endl;
cin >> grade1;
if (grade1 = 'A')
{
qualityPoints1 = 4.0;
}
else if (grade1 = 'B')
{
qualityPoints1 = 3.0;
}
else if (grade1 = 'C')
{
qualityPoints1 = 2.0;
}
else if (grade1 = 'D')
{
qualityPoints1 = 1.0;
}
else if (grade1 = 'F')
{
qualityPoints1 = 0.0;
}
else
{
cout << "Invalid Letter Grade" << endl;
}
cout << "How many credit hours was the class worth?" << endl;
cin >> credits1;
cqPoints1 = qualityPoints1 * credits1;
totalCredits = credits + credits1;
cout << "Total credit hours earned this term: " << totalCredits << endl;
qualitypointsTotal = cqPoints + cqPoints1;
gpa = qualitypointsTotal / totalCredits;
cout << "Your GPA for this term is: " << gpa << endl;
cout << "Credits: " << credits << endl;
cout << "Credits1: " << credits1 << endl;
cout << "qualityPoints: " << qualityPoints << endl;
cout << "qualityPoints1: " << qualityPoints1 << endl;
cout << "cqPoints: " << cqPoints << endl;
cout << "cqPoints1: " << cqPoints1 << endl;
cout << "qualitypointsTotal: " << qualitypointsTotal << endl;
cout << "totalCredits: " << totalCredits << endl;
return 0;
}
I am very aware that there are much more efficient ways to do this but right now I am just learning the basics so I would appreciate keeping the answers related to the scope of the lesson.
Thanks!
EDIT: I think it might be necessary to mention that I understand double is not needed for the variables. I also don't need io manipulation or any of the couts near the end of the program. I am just trying to figure out what went wrong. :-)
Upvotes: 1
Views: 3177
Reputation: 3365
That's because you are using the assignment operator, '=', instead of the comparison operator, '==', in your if-statements. So you are not comparing to Grade, you are assigning to it.
There are several ways to reduce this type of problem. One is to declare variables as 'const' as much as possible; this will flag unwanted assignments as an error. Another is to invert the variable and literal value in the if-statement (i.e. if ('A' == Grade) ), which has the same effect.
Also, if you turn on all compiler warnings, you might just get a warning on this as well. That depends on your compiler, of course.
Upvotes: 1