Switch statement using objects that are members of other classes. (HELP VERY MUCH APPRECIATED)

Below I have a switch statement in a class called undergraduate that attempts to use objects of course class (the object is being used as a data member in the undergraduate class ( Data member name: Course c[10]) I have also posted my accessor methods and my input methods of my course class to help contextualize some of the information. I receive the following compiler error:

undergraduate.cpp: In member function ‘int Undergraduate::calculateGPA(double)’:
undergraduate.cpp:108:27: error: switch quantity not an integer
switch (c[i].getGrade()){

Any suggestions on how to fix this?

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
//CALCULATE GPA METHOD IN THE UNDERGRADUATE CLASS THAT ATTEMPTS TO USE AN OBJECT OF THE COURSE CLASS TO CALCULATE GPA
  int Undergraduate::calculateGPA(double G){

        double totalgpa = 0.0;
        int completeCourses = 3;
        int counter = 0;
        bool validate = false;
        do{
                for (int i = 0; i < completeCourses; i++){
                        switch (c[i].getGrade()){
                                case "A":
                                        G = 4.0;
                                        gpa = G;
                                        totalgpa = totalgpa + G;
                                        counter += 1;
                                        validate = true;
                                        break;
                                case "B":
                                        G = 3.0;
                                        gpa = G;
                                        totalgpa = totalgpa + G;
                                        counter += 1;
                                        validate = true;
                                        break;
                                case "C":
                                        G = 2.0;
                                        gpa = G;
                                        totalgpa = totalgpa + G;
                                        counter += 1;
                                        validate = true;
                                        break;
                                case "D":
                                        G = 1.0;
                                        gpa = G;
                                        totalgpa = totalgpa + G;
                                        counter += 1;
                                        validate = true;
                                        break;
                                case "F":
                                        G = 0.0;
                                        counter += 1;
                                        gpa = G;
                                        totalgpa = totalgpa + G;
                                        validate = true;
                                        break;
                                default:
                                        cout << "Invalid grade entered. Please reenter." << endl;
                                        validate = false;
                                        break; 
                      }
                }
        } while (!validate);

        totalgpa = totalgpa/counter;
        cout << "Your GPA is: " << endl;


        return (totalgpa);
}


// ACCESSOR METHOD IN COURSE CLASS
string Course::getGrade(){
        return grade;
}
//INPUT METHOD IN YOUR COURSE COURSE
void Course::input(){

        int sID;
        double scredit;
        string scname, sinstruct, sgrade;

        cout << "Course name:";
        cin >> scname;
        setCourseName(scname);

        cout << "Please input the name of the instructor." << endl;
        cin >> sinstruct;
        setInstructor(sinstruct);

        cout << "Please input the course identification number." << endl;
        cin >> sID;
        setCourseID(sID);

        cout << "Please input the number of credits you received for this course." << endl;
        cin >> scredit;
        setCredit(scredit);

        cout << "Please input the grade you received for this course." << endl;
        cin >> sgrade;
        setGrade(sgrade);
}

You cannot switch on non-numeric types, such as strings. Just use if-elseif-elseif-else...etc.
This has nothing to do with whether or not you're using data from another class.

Switch statements can only work with variables that have an integer type. You're trying to use it with a std::string.

Like the above has said, it doesn't work with strings, but it does work with chars. And that's exactly what a letter is, one character. So make the grade what it should have been in the first place, and make getGradfe return a character.

And change all the

1
2
case "A": 
case "B":


to

1
2
case 'A': // not ' ' not " "
case 'B':
I got my code working guys! Thanks so much for the responses!
Topic archived. No new replies allowed.