trying to figure out why my function isnt working

So ive been working on this for a couple of hours and i hit a couple speed bumps but nothing major. when i got to the " string calculateLetterGrade" i thought i was doing it right following what looked like simple instructions but when i go to call it like i did with the previous function it doesn't work and i cant seem to figure out why.

any help is appreciated

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
#include <iostream>
#include <string>

using namespace std;

//make a function that will be called later 
float CalculateAverage(float a, float b, float c, float d, float e)
{
	float division;
	float addition;

	addition = (a + b + c + d + e);
	division = addition / 5;
	return division;
}

string calculateLetterGrade(float grade)
{
	float gradeAverage;
	float letter;

	{
		if (gradeAverage >= 90 && gradeAverage == 100)
			grade = 'A+';
		else if (gradeAverage >= 85 && gradeAverage == 89)
			grade = 'A';
		else if (gradeAverage >= 80 && gradeAverage == 84)
			grade = 'A-';
		else if (gradeAverage >= 77 && gradeAverage == 79)
			grade = 'B+';
		else if (gradeAverage >= 74 && gradeAverage == 76)
			grade = 'B';
		else if (gradeAverage >= 70 && gradeAverage == 73)
			grade = 'B-';
		else if (gradeAverage >= 67 && gradeAverage == 69)
			grade = 'C+';
		else if (gradeAverage >= 64 && gradeAverage == 66)
			grade = 'C';
		else if (gradeAverage >= 60 && gradeAverage == 63)
			grade = 'C-';
		else if (gradeAverage >= 57 && gradeAverage == 59)
			grade = 'D+';
		else if (gradeAverage >= 54 && gradeAverage == 56)
			grade = 'D';
		else if (gradeAverage >= 50 && gradeAverage == 53)
			grade = 'D-';
		else if (gradeAverage < 50)
			grade = 'F';
	}
	return 0;
}


int main()
{
	string letterGrade = 0;
	float yourGrade = 0;
	float sum = 0;
	float average = 0;
	float math = 0;
	float english = 0;
	float science = 0;
	float history = 0;
	float geography = 0;

	//ask the user to enter 5 of his highschool grades
	cout << "Please enter your Math, Science, History and Geography " << endl;

	//ask for the Math grade
	cout << "Enter your math grade" << endl;

	cin >> math;

	//ask for the English grade
	cout << "Enter your English grade " << endl;

	cin >> english;

	//ask for the Science grade
	cout << "Enter your Science grade " << endl;

	cin >> science;

	//ask for the History grade
	cout << "Enter your History grade " << endl;

	cin >> history;

	//ask for the Geography grade
	cout << "Enter your Geography grade " << endl;

	cin >> geography;

	//call CalculateAverage function
	average = CalculateAverage(math, english, science, history, geography);

	//call calculateLetterGrade
	letterGrade = calculateLetterGrade(yourGrade);

	cout << endl << " ============================================== " << endl;

	cout << "Math " << math << "%" << "(" << letterGrade << ")" << endl;
	cout << "English " << english << "%" << "(" << letterGrade << ")" << endl;
	cout << "Science " << science << "%" << "(" << letterGrade << ")" << endl;
	cout << "History " << history << "%" << "(" << letterGrade << ")" << endl;
	cout << "Geography " << geography << "%" << "(" << letterGrade << ")" << endl;

	cout << endl << "Grade Average " << average << "%" << endl;




	cout << endl << " ============================================== " << endl;







	system("pause");
	return 0;
}

Last edited on
hey. Looking at it now.
in the meantime, just edit your post, highlight the code, and use the <> symbols in the box next to the editor to make it more codeish.

first thing is do a search and replace of float with double. Floats are nothing but trouble.
second, if it is an integer, use integers. grade seems like it should be type int or unsigned int.

strings use " not '
so you need to return
"A+" not 'A+'
this may be the issue. still looking though.
you assign grade a string. you should have a string variable instead, grade is a float...

looks like
string calc(int grade)
{
string result = "";
if(grade < 50)
result = "F"; //like this

... more if statements
return result.

also, going backwards makes it easier on the eyes. IF you just return the value this way you don't need result to hold the value until the end. logically that is:
if < 50
return "F";
if(< 53) //to get here, it has to be 50 or more
return "D-"; //to get here it has to be less than 53... see how they chain?
}
Last edited on
I was trying that, not sure why its not working. Im selecting all the code part of the post and clicking the <> icon but nothing seems to happen and when i submit it still looks the same. gonna try on a different browser maybe.

yeah another thing the calculateAverage has to be a float with 5 parameters of type float and the calculateLetterGrade has to be a string with one parameter of type float called grade. should have clarified beforehand.

Ill try the " instead of '

well the other week he had us make a average, min, max, sum calculator with only if statements. to teach us that they suck. next week were learning arrays so im looking forward to doing this even faster.
Last edited on
I was still editing above.
I think if you return an actual string from your function it will work now.
The other things I said are just prettys.

if you must use a float, then do what the professor said. Professors these days... they seem to all tell you "write this, but you can't use anything at all and you have to work around these goofy criteria". I guess its kinda like work, actually.

Last edited on
im now launching with a error window saying:

Error: Could not find 'C:\Users\Fillip\Desktop\Project 3\Project3\Debug\Source.obj'. Project3.exe was built with /DEBUG:FASTLINK which requires object files for debugging."

the project launches into a black cmd windows with nothing showing up anymore.
Code now looks like this. Not sure why its not launching properly.

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
#include <iostream>
#include <string>

using namespace std;

//make a function that will be called later 
float CalculateAverage(float a, float b, float c, float d, float e)
{
	float division;
	float addition;

	addition = (a + b + c + d + e);
	division = addition / 5;
	return division;
}

string calculateLetterGrade(string grade)
{
	int gradeAverage;
	int letter;

	{
		if (gradeAverage < 50)
			return "F";
		if (gradeAverage <= 54)
			return "D-";
		if (gradeAverage < 58)
			return "D";
		if (gradeAverage < 60)
			return "D+";
		if (gradeAverage < 64)
			return "C-";
		if (gradeAverage < 67)
			return "C";
		if (gradeAverage < 70)
			return "C+";
		if (gradeAverage < 74)
			return "B-";
		if (gradeAverage < 77)
			return "B";
		if (gradeAverage < 80)
			return "B+";
		if (gradeAverage < 85)
			return "A-";
		if (gradeAverage < 890)
			return "A";
		if (gradeAverage >= 90)
			return "A+";
	}

}

int main()
{
	string letterGrade = 0;
	string yourGrade = 0;
	float sum = 0;
	float average = 0;
	float math = 0;
	float english = 0;
	float science = 0;
	float history = 0;
	float geography = 0;

	//ask the user to enter 5 of his highschool grades
	cout << "Please enter your Math, Science, History and Geography " << endl;

	//ask for the Math grade
	cout << "Enter your math grade" << endl;

	cin >> math;

	//ask for the English grade
	cout << "Enter your English grade " << endl;

	cin >> english;

	//ask for the Science grade
	cout << "Enter your Science grade " << endl;

	cin >> science;

	//ask for the History grade
	cout << "Enter your History grade " << endl;

	cin >> history;

	//ask for the Geography grade
	cout << "Enter your Geography grade " << endl;

	cin >> geography;

	//call CalculateAverage function
	average = CalculateAverage(math, english, science, history, geography);

	//call calculateLetterGrade
	letterGrade = calculateLetterGrade(yourGrade);

	cout << endl << " ============================================== " << endl;

	cout << "Math " << math << "%" << "(" << letterGrade << ")" << endl;
	cout << "English " << english << "%" << "(" << letterGrade << ")" << endl;
	cout << "Science " << science << "%" << "(" << letterGrade << ")" << endl;
	cout << "History " << history << "%" << "(" << letterGrade << ")" << endl;
	cout << "Geography " << geography << "%" << "(" << letterGrade << ")" << endl;

	cout << endl << "Grade Average " << average << "%" << endl;

	cout << endl << " ============================================== " << endl;

	system("pause");
	return 0;
}
Forget the code for a moment.

Ask yourself:
- what thing do I need in order to work out a letter grade? What type of variable is this? This will be your input.
- what thing do I output as a result?


Your function definition currently starts:
string calculateLetterGrade(string grade)
Given that the input will be passed to the function as a parameter (in brackets after the function name) does this currently correspond to what you have decided your "input" is?

Now go through your function line by line and ask yourself: am I using a variable whose value I currently know?


(Unless you have some extremely able students you might also like to fix the typo on line 45. You are also suspiciously inconsistent with the < and <=.)
Last edited on
Yeah just noticed your reply. Im new to programming and we were taught to use <= and type the code the way i had written it the first time and so when i was writing it backwards ( that's what it is in my head) which really is the right way i was adding "=" which wasn't needed.

still learning but i had a lot of fun writing this.

Got upset that i couldn't find the mistake which was causing an error on debug but someone helped me find it which in the end was a variable that was initialized to 0 which it wasnt supposed to.

Thanks for all the help.
Topic archived. No new replies allowed.