GPA (Grade point average)

Hi, I want to make a program that calculates your GPA for the classes I made up. My program runs but it gives me a way off number. Not sure what is wrong with it, please help and thanks! UPDATE: I just saw It doesn't work on the shell, but does run on c9 for some reason...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
#include <cmath>
using namespace std;
int main()
{
    
    char math, english, science, major_related, history;
    int a = 4;
    int b = 3;
    int c = 2;
    int d = 1;
    int f = 0;

    
    cout << "Hello, Today I'm going to determine your unweighted GPA!  " << endl;
    cout << "What was your grade in math:  " << endl;
    cin >> math;
    cout << "What was your grade in english:  " << endl;
    cin >> english;
    cout << "What was your grade in science:  " << endl;
    cin >> science;
    cout << "What was your grade in history:  " << endl;
    cin >> history;
    cout << "What was your grade in major related:  " << endl;
    cin >> major_related;
    
    cout << "" << endl;
    cout << "Your total GPA is:  " << (math + english + history + science + major_related) / 5 << endl; 
    
    return 0;
}
Last edited on
Change your grades from int to use float instead.
When you divide an int you are doing integer division and you will see rounding issues.
Why are the subjects of char type? Is there some conversion function that converts the subjects into integer value that you have not included? If you do not a function that does this, line 28 is not going to work. chars (unless I'm mistaken) can't be added up. Hence failure.

PS
If the code above is not the full code, please show us the full code.

-VX
Last edited on
If you do not a function that does this, line 28 is not going to work.

That is incorrect. char can be implicitly converted/promoted to int.
signed char or signed short can be converted to int;

http://en.cppreference.com/w/cpp/language/implicit_conversion

Also, I'd recommend you define a function that calculates the GPA, just to keep main() clean.
1
2
3
4
double calc_gpa( double grades[], size_t size )
{
    
}

Also, do some basic error/validity checks, such as range checking of grades.
Last edited on
@integralfx, that was not what I meant. I was asking if @bardo99 had a function to convert the grade inputs (A, B, C, D, or F) into 4, 3, 2, or 1.

Cheers,
VX

PS
I apologize if my poorly worded sentences was what confused you. English is my second language and I am still learning every day.
@integralfx, that was not what I meant. I was asking if @bardo99 had a function to convert the grade inputs (A, B, C, D, or F) into 4, 3, 2, or 1.

I misunderstood OP's code and misread your statement. That was a mistake on my part. Your English is fine though, it was just me not understanding it correctly. Sorry about that.
Last edited on
That's OK. We all make mistakes, and it's my fault that I didn't word my reply well.

-VX
No I do not have a function that converts the grade inputs, that is all I have. I changed the ints to floats but nothing has changed. I'm novice can I get some more help please.
And I'm using char so the user can input a letter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cctype>
#include <string>
using namespace std;


int gradeToNum( char c )
{
   switch( tolower( c ) )
   {
      case 'a':   return 4;
      case 'b':   return 3;
      case 'c':   return 2;
      case 'd':   return 1;
      default :   return 0;
   }
}


int main()
{
    const int NUMGRADES = 5;
    string subject[NUMGRADES] = { "math", "english", "science", "major_related", "history" };
    int total = 0;
    char grade;
    
    cout << "Hello, Today I'm going to determine your unweighted GPA!\n" << endl;

    for ( int i = 0; i < NUMGRADES; i++ )
    {
       cout << "What was your grade in " << subject[i] << "? ";
       cin >> grade;
       total += gradeToNum( grade );
    }
    cout << "\nYour total GPA is: " << (double)total / NUMGRADES << endl;
}
Last edited on
Thanks this is great, how would you do this assignment though without using the case statement. You don't have to recode but just explain it would be great, thanks!
The function gradeToNum simply converts a single char to a numerical value. @VX0726 was suggesting this earlier.

case is really just a part of the switch block - see the last items in
http://www.cplusplus.com/doc/tutorial/control/
It's a very routine part of C or C++ (or, in some form or other, most programming languages) - it's simply neater than a whole sequence of if ... else if statements. You would usually include break statements, but they aren't needed here because you return directly from the function at each case. tolower is simply taking care when the user has accidentally left the caps-lock key on and forcing the grade to be tested in lower case.

Alternatives?
I suppose you could
return 4 - ( c - 'a' );
for letter grades c = 'a', 'b', 'c' or 'd', but it's hardly obvious coding. Using some associative container like a map would be over-complicating it too.


In the main() routine, whenever you find yourself doing almost exactly the same thing repeatedly, consider using arrays and/or loops. Here, it would also make it much easier to change the number or name of courses as well.
Last edited on
Thank you so much, you helped me a lot!
You should look at making it calculate A- etc
You should look at making it calculate A- etc 


Letter grades will then have to become strings, number grades will have to be floating point ... and a map probably becomes worth it then to do the association.

Sorry, @muffsez, but I don't know much about STL maps, so I will have to ask for help from better programmers on this site to advise on this.

For what it's worth, and at the risk of getting a severe knocking for poor use of maps, here's one attempt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <cctype>
#include <map>
using namespace std;

map<string,double> gradeMap;                // to contain numbers associated with letter grades

// function prototypes
void setGradeMap();
double gradeToNum( string grade );
string stringToLower( string s );


int main()
{
    const int NUMGRADES = 5;
    string subject[NUMGRADES] = { "math", "english", "science", "major_related", "history" };
    double total = 0.0;
    string grade;

    setGradeMap();                           // set up mapping of letter grades to numbers (doubles)

    for ( int i = 0; i < NUMGRADES; i++ )
    {
       cout << "What was your grade in " << subject[i] << "? ";
       cin >> grade;
       grade = stringToLower( grade);        // make sure it is lower case
       total += gradeToNum( grade );
    }
    cout << "\nYour total GPA is: " << total / NUMGRADES << endl;
}


void setGradeMap()
{
   gradeMap["a+"] = 4.25;
   gradeMap["a" ] = 4.00;
   gradeMap["a-"] = 3.75;
   gradeMap["b+"] = 3.25;
   gradeMap["b" ] = 3.00;
   gradeMap["b-"] = 2.75;
   gradeMap["c+"] = 2.25;
   gradeMap["c" ] = 2.00;
   gradeMap["c-"] = 1.75;
   gradeMap["d+"] = 1.25;
   gradeMap["d" ] = 1.00;
   gradeMap["d-"] = 0.75;
}


double gradeToNum( string grade )      // converts letter(+-) grade to a number
{
   if ( gradeMap.count( grade ) == 0 ) return 0.0;               // if not found
   else                                return gradeMap[grade];   // if found, return corresponding number
}


string stringToLower( string s )       // converts string to lowercase
{
   string value = s;
   for ( int i = 0; i < value.size(); i++ ) value[i] = tolower( value[i] );
   return value;
}


What was your grade in math? A+
What was your grade in english? B-
What was your grade in science? c
What was your grade in major_related? d+
What was your grade in history? awful
Your total GPA is: 2.05
Last edited on
It seems to work, however I would do some input validation.
Thanks @Thomas1965.

You are right. Indeed, I only have to accidentally put a space in A + to make a student very unhappy. A robust program would probably have a function call instead of line 27 that would do the individual grade input and validation (and avoid, or be very careful with, cin). Left as another exercise for now.
Topic archived. No new replies allowed.