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.
#include <iostream>
usingnamespace 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;
}
elseif( average >= 65 <= 66)
{
cout<<"Your average is a D."<<endl;
}
elseif( average >= 67 <= 69)
{
cout<<"Your average is a D+."<<endl;
}
elseif( average >= 70 <= 72)
{
cout<<"Your average is a C-."<<endl;
}
elseif( average >= 73 <= 76)
{
cout<<"Your average is a C."<<endl;
}
elseif( average >= 77 <= 79)
{
cout<<"Your average is a C+."<<endl;
}
elseif( average >= 80 <= 82)
{
cout<<"Your average is a B-."<<endl;
}
elseif( average >= 83 <= 86)
{
cout<<"Your average is a B."<<endl;
}
elseif( average >= 87 <= 89)
{
cout<<"Your average is a B+."<<endl;
}
elseif( average >= 90 <= 92)
{
cout<<"Your average is an A-."<<endl;
}
elseif( average >= 93 <= 99)
{
cout<<"Your average is an A."<<endl;
}
elseif( 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.
elseif( 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:
elseif((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
}
elseif(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
}