Averaging Grades

Hey everyone. I'm just starting to get into C++ (I've taken a week long course so far) and am finding I have a passion for it. I was recently doing an exercise in which I had to design a program to average 10 grades which were entered by the user. In an effort to make it more complex I tried to make the program display the letter grade equivalent.

The problem I've been having is that almost any combination of numbers (even 100 x10) will return that your average is a D.

I've looked through repeatedly for errors but I can't find anything. I wouldn't be surprised if I'm doing something blatantly wrong such as misusing if/else statements.

If anyone has any ideas about how to fix this, it would be greatly appreciated.

For reference here's the 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
83
84
85
86
87
88
89
90
#include <iostream>

using namespace std;

int main()

{
	int scores[10];
	cout<<"Please enter the ten test scores (from 0-100) which you would like to average."<<endl;

	cin>>scores[0];

	cin>>scores[1];

	cin>>scores[2];

	cin>>scores[3];

	cin>>scores[4];

	cin>>scores[5];

	cin>>scores[6];

	cin>>scores[7];

	cin>>scores[8];

	cin>>scores[9];

	int sum = scores[0]+scores[1]+scores[2]+scores[3]+scores[4]+scores[5]+scores[6]+scores[7]+scores[8]+scores[9];
	int average = sum/10;
	cout<<endl<<endl;

	if( average < 65)
	{
		cout<<"Your average is an F."<<endl;
	}
	else if( average >= 65 <= 66)
	{
		cout<<"Your average is a D."<<endl;
	}
	else if( average >= 67 <= 69)
	{
		cout<<"Your average is a D+."<<endl;
	}
	else if( average >= 70 <= 72)
	{
		cout<<"Your average is a C-."<<endl;
	}
	else if( average >= 73 <= 76)
	{
		cout<<"Your average is a C."<<endl;
	}
	else if( average >= 77 <= 79)
	{
		cout<<"Your average is a C+."<<endl;
	}
	else if( average >= 80 <= 82)
	{
		cout<<"Your average is a B-."<<endl;
	}
	else if( average >= 83 <= 86)
	{
		cout<<"Your average is a B."<<endl;
	}
	else if( average >= 87 <= 89)
	{
		cout<<"Your average is a B+."<<endl;
	}
	else if( average >= 90 <= 92)
	{
		cout<<"Your average is an A-."<<endl;
	}
	else if( average >= 93 <= 99)
	{
		cout<<"Your average is an A."<<endl;
	}
	else if( average == 100)
	{
		cout<<"Your average is an A+."<<endl;
	}
	else
	{
		cout<<"You have entered invalid grades."<<endl;
	}

	system("PAUSE"); 
	return 0;
}


Thanks,

Csoda

P.S.

I know system{"PAUSE") is not a good command but I'm not sure of any better equivalents.
Last edited on
else if( average >= 93 <= 99)
this isnt a good check. :P
try
else if( average >= 93 && average <= 99)

also
1
2
int sum = scores[0]+scores[1]+scores[2]+scores[3]+scores[4]+scores[5]+scores[6]+scores[7]+scores[8]+scores[9];
	int average = sum/10;

dividing integers might lose precision, use doubles or floats.
I could be way off, but in your else if statements, you need && in there or else it's only going to check the opening condition.

Ie. It's only checking if average >= 65. Once that passes as true, everything else is ignored, so D is the result.

To further verify what I mean: each statement (after the first) should say:

else if (average >= [min_value] && average <= max_value])
cout << "[message]" << endl;
etc...

This will make the program check against both the minimum value AND the maximum value of each grade.
Last edited on
1
2
3
4
	else if( average >= 65 <= 66)
	{
		cout<<"Your average is a D."<<endl;
	}


This if condition is wrong. It evaluates as ((average >= 65) <= 66)

Note that these expressions evaluate to a boolean value. true (1), or false (0).

With that in mind, say the average is 90:

1
2
3
((90 >= 65) <= 66)
( (1) <= 66 )  // 90 >= 65 is true, so replace with 1
( 1 )  // 1 <= 66 is true, so proceed to print "average is a D" 


What you probably wanted to do was this:

 
else if((average >= 65) && (average <= 66))



HOWEVER

This is overly complicated.

The previous 'if' statement already guarantees that average is >= 65, so you don't need to check that again:

1
2
3
4
5
6
7
8
9
10
if(average < 65)
{
  // F
}
else if(average < 66)  // this works because the 'else'
{  // ensures the previous if condition was false.  Therefore for it
    // to get here, average must be >= 65.  So no need to repeat that
    // condition in this if statement.
  // D
}
Just me, but I would also clean up the input with a loop. Perhaps

1
2
3
4
5
6
7
8
9
10
int total;

For (x = 0; x < 10; x++)
{
   cout << "Enter score: " << endl;
   cin >> score[x];
   total = total + score[x];
}

average = total / 10;


...or something like that.
Last edited on
Wow, thank you so much for the fast and incredible answers. It's running perfectly now.

And Disch, your explanation made perfect sense, thanks for putting time into your answer :)



@Gmorris, how would the loop take care of the 10 different inputs? I thought of doing a loop but couldn't quite figure out how to make it work.

Edit: Ah, never mind, I see. That looks great actually, thanks
Last edited on
Topic archived. No new replies allowed.