I'm trying to code a program that will tell me the average and letter grade for three tests put in. I have the averaging worked out, but I can't seem to figure out how to get the correct letter grade. I'm fairly new to programming, so any help would be nice. Thanks
// What grade?
#include<iostream>
usingnamespace std;
int main()
{
int a; // First test's grade
int b; // Second test's grade
int c; // Third test's grade
cout << "What did you get on the first test?";
cin >> a; // User imputs first test's grade
if(a == 100){
cout << "Thats a perfect grade!";
}
elseif(100 > a >= 90){
cout << "That's an A";
}
elseif(90 > a >= 80){
cout << "That's a B";
}
elseif(80 > a >= 70){
cout << "That's a C";
}
elseif(70 > a >= 65){
cout << "That's a D";
}
else{
cout << "That's an F, you failed, better luck next time..." << endl;
}
cout << "What did you get in the second test?";
cin >> b; // User imputs second test's grade
cout << "What did you get on the third test?";
cin >> c; // User imputs third test's grade
int average = (a+b+c)/3; // Calculates average
cout << "Your test average is :" << average << endl; // Displays average
return 0;
}
This is the output after putting in the first grade:
What did you get on the first test? 89
That's an F, you failed, better luck next time...
What did you get in the second test?
// What grade?
#include<iostream>
usingnamespace std;
int main()
{
int a; // First test's grade
int b; // Second test's grade
int c; // Third test's grade
cout << "What did you get on the first test?";
cin >> a; // User imputs first test's grade
if(a == 100)
{
cout << "Thats a perfect grade!";
}
elseif(a >= 90)
{
cout << "That's an A";
}
elseif(a >= 80)
{
cout << "That's a B";
}
elseif(a >= 70)
{
cout << "That's a C";
}
elseif(a >= 65)
{
cout << "That's a D";
}
else
{
cout << "That's an F, you failed, better luck next time..." << endl;
}
cout << "What did you get in the second test?";
cin >> b; // User imputs second test's grade
cout << "What did you get on the third test?";
cin >> c; // User imputs third test's grade
int average = (a+b+c)/3; // Calculates average
cout << "Your test average is :" << average << endl; // Displays average
return 0;
}
if (100 > a >= 90)
is not doing what you want it to do. Every comparison operator must stand on its own.
What you actually meant was if (100 > a && a >= 90)
SamuelAdams simplified the redundant nature of the logic by removing the first condition of each statement.
Ganado is correct. In the statement, else if(90 > a >= 80), the reason I removed the "90 > a" part is because it was redundant in this case. If you were doing something highly important, you might want to include a check like you tried.
Sometimes less typing is faster but at other times it can get you into trouble.
Another way to write that condition is
if ((100 > a) && (a >= 90))
I may be wrong but personally I think it's safer and easier to debug when combining statements such as this.