Returning a string from a function

Hey yall, had another question on a school project. So the goal is to use this menu to choose a subject, then enter the scores, then put the scores into a function then have it put out a grade. The problem I'm having with it is the final part of it, then returning the grade. I have the grades set as strings because they need to have the + and - but whenever I actually input the scores I get a 1 in return. After a few hours of coding this stuff I think this is my last shot for the day, the code is below. I know it isn't perfect but it also isn't done yet haha.

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
string getScores(int, int);
int menuOfSubjects();
bool validateInput(int, int, int, string);

void
moreFunk()
{
    int pointsEarned = 0; // total points
    int totalPoints = 0;
    bool menuFlag = true;

       while (menuFlag)
       {
         int choice = menuOfSubjects();
         switch (choice)
         {
            case PHYSICS_CHOICE:
                cout << "Please enter points earned" << endl;
                cin >> pointsEarned;
                cout << "Please enter total points" << endl;
                cin >> totalPoints;
                    string getScores(int pointsEarned, int totalPoints);
                    cout <<"Your grade was: " << getScores << endl;
                break;

            case BIOLOGY_CHOICE:
                cout << "Please enter points earned" << endl;
                cin >> pointsEarned;
                cout << "Please enter total points" << endl;
                cin >> totalPoints;
                    string getScores(int pointsEarned, int totalPoints);
                    cout <<"Your grade was: " << getScores << endl;

                break;

            case CALCULUS_CHOICE:
                cout << "Please enter points earned" << endl;
                cin >> pointsEarned;
                cout << "Please enter total points" << endl;
                cin >> totalPoints;
                    string getScores(int pointsEarned, int totalPoints);
                    cout <<"Your grade was: " << getScores << endl;

                break;

            case GEOGRAPHY_CHOICE:
                cout << "Please enter points earned" << endl;
                cin >> pointsEarned;
                cout << "Please enter total points" << endl;
                cin >> totalPoints;
                    string getScores(int pointsEarned, int totalPoints);
                    cout <<"Your grade was: " << getScores << endl;

                break;

            case SOCIOLOGY_CHOICE:
                 cout << "Please enter points earned" << endl;
                cin >> pointsEarned;
                cout << "Please enter total points" << endl;
                cin >> totalPoints;
                    string getScores(int pointsEarned, int totalPoints);
                    cout <<"Your grade was: " << getScores << endl;

                break;

            case COMPUTERSCIENCE_CHOICE:
                cout << "Please enter points earned" << endl;
                cin >> pointsEarned;
                cout << "Please enter total points" << endl;
                cin >> totalPoints;
                    string getScores(int pointsEarned, int totalPoints);
                    cout <<"Your grade was: " << getScores << endl;

                break;

            case POLYMERS_CHOICE:
               cout << "Please enter points earned" << endl;
                cin >> pointsEarned;
                cout << "Please enter total points" << endl;
                cin >> totalPoints;
                    string getScores(int pointsEarned, int totalPoints);
                    cout <<"Your grade was: " << getScores << endl;

                break;

            case QUIT_CHOICE:
                cout << "Program ending.\n";
         }
       }

      cout << "end of more functions" << endl;
      cin.get();
    }

//*******************************************
// TASK 5 CODE - PLACE CODE HERE FOR TASK
//*******************************************

int menuOfSubjects()
{
  int choice;
  bool loopFlag = true;
  string msg("Please choose a subject from the menu ");
    cout << "Please choose a subject from the menu \n\n";
	cout << "1. Physics\n";
	cout << "2. Biology\n";
	cout << "3. Calculus\n";
	cout << "4. Geography\n";
	cout << "5. Sociology\n";
	cout << "6. Computer Science I\n";
	cout << "7. Properties of Solid Polymers\n";
	cout << "8. Quit\n";

	while (loopFlag){
		cout << "Enter your choice (1-8): ";
		cin >> choice;
		loopFlag = validateInput(choice, 1, 8, msg);
	}

	return choice;
}

string getScores(int pointsEarned, int totalPoints){

    string grade = " ";

         int gradePercentage = ((pointsEarned) / static_cast<float>(totalPoints) * 100);

              if ((gradePercentage) > 92.99)
            {
                grade = "A";
            }
            else if ((gradePercentage) > 89.99)
            {
                 grade = "A-";
            }
            else if ((gradePercentage) > 86.99)
            {
                 grade = "B+";
            }
            else if ((gradePercentage) > 82.99)
            {
                 grade = "B";
            }
            else if ((gradePercentage) > 79.99)
            {
                 grade = "B-";
            }
            else if ((gradePercentage) > 76.99)
            {
                grade = "C+";
            }
            else if ((gradePercentage) > 72.99)
            {
                 grade = "C";
            }
            else if ((gradePercentage) > 69.99)
            {
                 grade = "C-";
            }
            else if ((gradePercentage) > 66.99)
            {
                 grade = "D+";
            }
            else if ((gradePercentage) > 62.99)
            {
                 grade = "D";
            }
            else if ((gradePercentage) > 59.99)
            {
                 grade = "D-";
            }
            else if ((gradePercentage) < 59.99)
            {
                 grade = "F";
            }
            
        return grade;

}



Last edited on
Regarding every place you have:
string getScores(int pointsEarned, int totalPoints);
You are not calling the function here. This is just a function declaration (exactly the same as line 1).

To call the function, you do something like
1
2
string score = getScores(pointsEarned, totalPoints);
cout << "Your grade was: " << score << endl;


PS: IMO, 'getScores' is a misleading name since it's plural, but it returns a single grade.
Last edited on
FYI, your code snippet you provided is incomplete, it is not SSCCE. [A Short, Self Contained, Correct (Compilable), Example]
http://www.sscce.org/

Making some wild guesses since I can't compile or test the code:

comment out/delete lines 22, 31, 41, 51, 61, 71, and 81.
string getScores(int pointsEarned, int totalPoints);

Change lines 23, 32, 43, 52, 62, 72 and 82:
cout <<"Your grade was: " << getScores(int pointsEarned, int totalPoints) << endl;

Thank you for using code tags!

Line 22 (and in each pf the other cases): By putting string in front of getScores(), you've made it into a function declaration rather than a function call.

What you need to do is:
1
2
3
4
 
    string grade;
    grade = getScores(int pointsEarned, int totalPoints);
    cout <<"Your grade was: " << grade << endl;


edit: missed the ints in the function call.
Last edited on
AbstractionAnon wrote:
What you need to do is:

1
2
3
4
    string grade;
    grade = getScores(int pointsEarned, int totalPoints);
    cout <<"Your grade was: " << grade << endl;


No, that's still not the right syntax for calling a function. By leaving in the declarations of the argument types, you have some weird hybrid of a declaration and a call.

That code does, at least, have the virue of causing the compiler to issue an error, which is an improvement over the OP's code :)
Last edited on
Ah great! thank you guys so much! I am new to c++ and still getting the hang of functions. those solutions all worked though, thanks again!
Topic archived. No new replies allowed.