Calculating GPA from User Input (with constraints)

Hello,

I am having some trouble creating a simple program to calculate a GPA from user input due to some constraints placed on the assignment.

Namely:
1. No use of 'string'.
2. No use of arrays.
3. Use single characters 'char'.
** Use of 'int/double/bool' and other basic classes and other operators are allowed.

Those are the primary instructions, so I assume that getting into enum, struct, or more complex functions would also not be allowed.

The program is meant to process letter-grade input and assign numeric values. Everything is working fine, however I cannot figure out how to deal with grades like "A+" or "A-" (multiple character inputs, stored in single character variables).

I am hoping to get some assistance, or advice.
I feel like I'm missing something obvious and I'm embarrassed to ask my peers.
I am hoping to get some assistance, or advice.

Post here what code you've written would be a good start. It is your assignment, you have to do it.

We can offer advice/suggestions after you've tried to do the work.
Here is what I have, that was working fine.
However with the constraints (listed above) that I've overlooked, I essentially have to remove the arrays and work out a way to dumb it down to just single character (char) inputs.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int main()
{
    int i;
    int numClasses;
    double gradePoints = 0;
    double gradeAvg = 0;
    char choice, opt;

    while (opt != '0')
    {
    cout << "-------Welcome to the Grade Point Average Calculator-------" << endl;

    cout << "What can I help you with today?\n" << endl;
    cout << "1. Compute grades." << endl;
    cout << "0. Exit\n" << endl;
    cout << "***********************************************************" << endl;

    cin >> opt;

    if (opt == '1')
    {

    do {
    cout << "\n\nHow many classes would you like me to compute for you: ";
    cin >> numClasses;
    cout << "\nOk. I will be calculating grades for " << numClasses << " class(es)." << endl;

    string gradeArray[numClasses];
    string classNames[numClasses];
    int courseHours[numClasses];
    int courseHoursSum = 0;

    for (i=0; i<numClasses;++i)
    {
        cout << "\nWhat is the name of class number " << i+1 << "?" << endl;
        cin >> classNames[i];
        cout << "\nWhat grade did you receive in " << classNames[i] << "?" << endl;
        cin >> gradeArray[i];
        cout << "\nHow many credit hours was " << classNames[i] << "?" << endl;
        cin >> courseHours[i];
    }

    cout << "You entered the following classes: " << endl;
    for (i=0; i<numClasses;++i)
    {
        cout << classNames[i] << "\t\t" << " ";
        cout << courseHours[i] << " hrs\t ";
        cout << "Grade: " << gradeArray[i];
        cout << endl;
    }

    cout << "\n\nComputing grade points..." << endl;

    for (i=0; i<numClasses;++i)
    {
        if(gradeArray[i] == "A")
            gradePoints += 4.00 * courseHours[i];
        else if (gradeArray[i] == "A-")
            gradePoints += 3.67 * courseHours[i];
        else if (gradeArray[i] == "B+")
            gradePoints += 3.33 * courseHours[i];
        else if (gradeArray[i] == "B")
            gradePoints += 3.00 * courseHours[i];
        else if (gradeArray[i] == "B-")
            gradePoints += 2.67 * courseHours[i];
        else if (gradeArray[i] == "C+")
            gradePoints += 2.33 * courseHours[i];
        else if (gradeArray[i] == "C")
            gradePoints += 2.00 * courseHours[i];
        else if (gradeArray[i] == "C-")
            gradePoints += 1.67 * courseHours[i];
        else if (gradeArray[i] == "D+")
            gradePoints += 1.33 * courseHours[i];
        else if (gradeArray[i] == "D")
            gradePoints += 1.00 * courseHours[i];
        else if (gradeArray[i] == "D-")
            gradePoints += 0.67 * courseHours[i];
        else if (gradeArray[i] == "F")
            gradePoints += 0.00 * courseHours[i];
    }

    for (i=0; i<numClasses; ++i)
    {
        courseHoursSum += courseHours[i];
    }

    gradeAvg = gradePoints/courseHoursSum;

    cout << "Total semester hours taken: " <<  courseHoursSum << endl;
    cout << "Total quality (grade) points earned: " << gradePoints << endl;
    cout << "Grade point average (GPA) = " << gradeAvg << endl;

    cout << "\n\nWould you like me to calculate another term? ";
    cin >> choice; choice = toupper(choice);

    } while (choice == 'Y');

    }
    }

    cout << "Goodbye...\n\n\n" << endl;

    return 0;
}
I think I solved it using 'cin.get', but if anyone has any other solutions or advice... this is the working code:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    int i;
    int courseNumber = 0;
    double creditHours = 0, creditHoursTotal = 0;
    double gradePoints = 0, gpa = 0, cgpa = 0;
    char choice, grade, gradePlus;

    do {

    cout << "\nHow many courses would you like to factor into your GPA? : ";
    cin >> courseNumber;
    cout << "I will begin calculating grades for " << courseNumber << " courses." << endl;

    for (i = 0; i < courseNumber; ++i)
    {

        cout << "\n***** Course number " << i+1 << " *****" << endl;
        cout << "Total number of credit hours: ";
        cin >> creditHours;

        cout << "Grade: ";
        cin >> grade;
        cin.get(gradePlus);
        cout << endl;

        cout << "\nFor course number " << i+1 << " you entered the following: " << endl;
        cout << "  Credit hours: " << creditHours << "\tGrade: " << grade << gradePlus << endl;

        creditHoursTotal += creditHours;

        if (grade == 'A' && gradePlus != '-' && gradePlus != '+')
            gradePoints += 4.00 * creditHours;
        else if (grade == 'A' || grade == 'a' && gradePlus == '-')
            gradePoints += 3.67 * creditHours;
        else if (grade == 'B' || grade == 'b' && gradePlus == '+')
            gradePoints += 3.33 * creditHours;
        else if (grade == 'B' || grade == 'b' && gradePlus != '-' && gradePlus != '+')
            gradePoints += 3.00 * creditHours;
        else if (grade == 'B' || grade == 'b' && gradePlus == '-')
            gradePoints += 2.67 * creditHours;
        else if (grade == 'C' || grade == 'c' && gradePlus == '+')
            gradePoints += 2.33 * creditHours;
        else if (grade == 'C' || grade == 'c' && gradePlus != '-' && gradePlus != '+')
            gradePoints += 2.00 * creditHours;
        else if (grade == 'C' || grade == 'c' && gradePlus == '-')
            gradePoints += 1.67 * creditHours;
        else if (grade == 'D' || grade == 'd' && gradePlus == '+')
            gradePoints += 1.33 * creditHours;
        else if (grade == 'D' || grade == 'd' && gradePlus != '-' && gradePlus != '+')
            gradePoints += 1.00 * creditHours;
        else if (grade == 'D' || grade == 'd' && gradePlus == '-')
            gradePoints += 0.67 * creditHours;
        else if (grade == 'F' || grade == 'f' && gradePlus != '-' && gradePlus != '+')
            gradePoints += 0.00 * creditHours;
    }

    gpa = gradePoints / creditHoursTotal;
    cgpa = gradePoints / creditHoursTotal;

    cout << "\nTotal number of hours: --------- " << creditHoursTotal << endl;
    cout << "Total number of grade points --- " << gradePoints << endl;
    cout << "GPA: --------------------------- " << gpa << endl;
    cout << "CGPA: -------------------------- " << cgpa << endl;

    cout << "\n\nWould you like to run the program again? (Y/N) ";
    cin >> choice; choice = toupper(choice);

    creditHours = 0, creditHoursTotal = 0;
    gradePoints = 0, gpa = 0, cgpa = 0;

    } while (choice == 'Y');

    cout << "\n\nGoodbye...\n" << endl;

    return 0;
}
Topic archived. No new replies allowed.