Program outputting wrong result

Dec 5, 2012 at 12:57am
Ok, so I just started with c++ a few days ago, and I tried to make a GPA Calculator. I see nothing wrong with the code, but when I tested it out, I always receive an incorrect answer. I just put 'A' for every letter grade, and 4 for every credit hour so that I knew the GPA should be 4.0, but for some reason it gives me 65 for the GPA. Here is the code:


//GPA Calculator

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int qualitypoints(char letter){
int qPoints;
if (letter == 'A')
qPoints = 4;
else if (letter == 'B')
qPoints = 3;
else if (letter == 'C')
qPoints = 2;
else if (letter == 'D')
qPoints = 1;
else
qPoints = 0;
return qPoints;
}

struct grade
{
string course;
char letter;
int hours;


};

int main()
{

float GPA;

float totalqp;

grade grade1, grade2, grade3, grade4, grade5;

cout << "Enter the letter grade of your first course " << endl;
cin >> grade1.letter;

cout << "Enter the name of your first course " << endl;
cin >> grade1.course;

cout << "Enter the number of hours your course was " << endl;
cin >> grade1.hours;

cout << "Enter the letter grade of your second course " << endl;
cin >> grade2.letter;

cout << "Enter the name of your second course " << endl;
cin >> grade2.course;

cout << "Enter the number of hours your course was " << endl;
cin >> grade2.hours;

cout << "Enter the letter grade of your third course " << endl;
cin >> grade3.letter;

cout << "Enter the name of your third course " << endl;
cin >> grade3.course;

cout << "Enter the number of hours your course was " << endl;
cin >> grade3.hours;

cout << "Enter the letter grade of your fourth course " << endl;
cin >> grade4.letter;

cout << "Enter the name of your fourth course " << endl;
cin >> grade4.course;

cout << "Enter the number of hours your course was " << endl;
cin >> grade4.hours;

cout << "Enter the letter grade of your fifth course " << endl;
cin >> grade5.letter;

cout << "Enter the name of your fifth course " << endl;
cin >> grade5.course;

cout << "Enter the number of hours your course was " << endl;
cin >> grade5.hours;

totalqp = (grade1.hours * grade1.letter) + (grade2.hours * grade2.letter) + (grade3.hours * grade3.letter) + (grade4.hours * grade4.letter) + (grade5.hours * grade5.letter);

GPA = totalqp / (grade1.hours + grade2.hours + grade3.hours + grade4.hours + grade5.hours);

cout << "Your GPA is: " << GPA << endl;

system("PAUSE");
return 0;
}
Dec 5, 2012 at 1:05am
Please use code tages

Maybe use your qualitypoints function to give you a numerical value for the grade? Also make sure you are inputting 'A' and not 'a'.
Dec 5, 2012 at 1:16am
First, I'd like to thank you for your response. Sorry, I'm very new to the forum, and don't know what code tags even are. I put capital A each time, so that's not the issue. And, what do you mean by give a numerical value for the grade? It already does. If it didn't give a numerical value, I wouldn't get a number (65).
Dec 5, 2012 at 1:16am
Also I see the function qualitypoint() is unused.
Dec 5, 2012 at 1:24am
You will see an Edit button, then click it. You'll see a format menu on the right, then click <>, then wrap your code between two tabs you've just received.
Dec 5, 2012 at 1:26am
Can you explain what you mean by "unused"? Sorry, I'm very nooby.
Dec 5, 2012 at 1:30am
You defined a function (E.g qualitypoint()) but please, check the function main(), you haven't used this yet.
Dec 5, 2012 at 1:30am
You are not calling the function qualitypoints() - C++ will use the ASCII value of a letter ('A' = 65, 'B' = 66 and so on) Google ASCII values

1
2
3

totalqp = (grade1.hours * qualitypoints(grade1.letter)) + (grade2.hours * qualitypoints(grade2.letter)) + (grade3.hours * qualitypoints(grade3.letter)) + (grade4.hours * qualitypoints(grade4.letter)) + (grade5.hours * qualitypoints(grade5.letter));


You have to call the function. Does that help?
Dec 5, 2012 at 1:33am
Ahh, I see. Thanks a lot, now I am getting the right answers.
Topic archived. No new replies allowed.