Help with functions

Hey all I'm having trouble solving the last part of my code. I want to determine letter grade from the percentage score.When I try to run the code I get "You got a F" every time, no matter if the percentage is 90 or 50. Can any help me figure out what I'm doing wrong??
Last edited on
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
double calcLetterGrade()
{
	double percentage = 0;

	if (percentage >= 95)
	{
		cout << "You got an A" << endl;
	}
	else if (percentage >= 90 && percentage < 95)
	{
		cout << "You got a A-" << endl;
	}
	else if (percentage >=85 && percentage < 90)
	{
		cout << "You got a B" << endl;
	}
	else if (percentage >= 80 && percentage < 85)
	{
		cout << "You got a B-" << endl;
	}
	else if (percentage >= 75 && percentage < 80)
	{
		cout << "You got a C" << endl;
	}
	else if (percentage >= 70 && percentage < 75)
	{
		cout << "You got a C-" << endl;
	}
	else if (percentage >= 65 && percentage < 70)
	{
		cout << "You got a D" << endl;
	}
	else if (percentage >= 60 && percentage < 65)
	{
		cout << "You got a D-" << endl;
	}
	else
	{
		cout << "You got a F" << endl;
	}
	return percentage;
}


Look at what exactly that function does, line by line, and you should be able to tell what is going wrong. (Pay attention to the value of percentage.

Also, you make redundant checks in your else ifs. If execution reaches line 9, that would mean that line 5 was false. So percentage would be less than 95, yet you check for that in line 9 regardless. It will always be true, so you don't need it.

EDIT: Fixed line numbers.
Last edited on
Topic archived. No new replies allowed.