if-else if, What am I doing wrong here?

This should be one of the easiest things I do. I look at other people's code, and everything looks like it should be right. It just will not work.

My code:

------------------------------------
#include <iostream>
using namespace std;

int main()
{

int grade;
cout << "Enter your grade: ";
cin >> grade;

if (grade = 100)
cout << "Curvebuster!" << endl;
else if (grade >= 90)
cout << "You got an A!" << endl;
else if (grade >= 80)
cout << "You got a B." << endl;
else if (grade >= 70)
cout << "You got a C. Could have done better." << endl;
else if (grade >= 60)
cout << "You got a D. Shop class is that way." << endl;
else if (grade >= 0)
cout << "The short bus is this way." << endl;
else
cout << "Pease enter a valid grade." << endl;

return 0;

}

-------------------------------------

Code I am comparing to:

-------------------------------------
#include <iostream>
using namespace std;

int main(void)
{
int testScore;
cout << "Enter your test score: ";
cin >> testScore;

if (testScore >= 90 )
cout << "Your grade is an A" << endl;
else if (testScore >= 80 )
cout << "Your grade is a B" << endl;
else if (testScore >= 70 )
cout << "Your grade is a C" << endl;
else if (testScore >= 60 )
cout << "Your grade is a D" << endl;
else
cout << "Your grade is an F" << endl;
return 0;
}

-------------------------------------------

The second code works. My code always gives me the output from the if statement. It never changes. Help, please.
In your if statement you're initializing grade to alwasy be 100.

your if should be if (grade==100)
Last edited on
It's double equals. == is the equality operator. It evaluates to true if its operands are the same. single equals (=) is the assignment operator. It returns the value of its left operand and therefore it is NOT a logical test.
Thanks, guys! I knew I was brain freezing on something simple.
#include <iostream>
using namespace std;

int main(void)
{
int testScore;
cout << "Enter your test score: ";
cin >> testScore;

if (testScore >= 90 )
cout << "Your grade is an A" << endl;
else if (testScore >= 80 )
cout << "Your grade is a B" << endl;
else if (testScore >= 70 )
cout << "Your grade is a C" << endl;
else if (testScore >= 60 )
cout << "Your grade is a D" << endl;
else
cout << "Your grade is an F" << endl;
return 0;
}

dude dut this doesn't satify the condition for score above 100 or below 0. there should be conditions that even checks it and shows that the entered grade is invalid.

#include <iostream>
using namespace std;
int main()
{

int grade;
cout << "Enter your grade: ";
cin >> grade;

if (grade > 100)
cout << "Pease enter a valid grade." << endl;
else if (grade>90)
cout << "Curvebuster!" << endl;
else if (grade >= 90)
cout << "You got an A!" << endl;
else if (grade >= 80)
cout << "You got a B." << endl;
else if (grade >= 70)
cout << "You got a C. Could have done better." << endl;
else if (grade >= 60)
cout << "You got a D. Shop class is that way." << endl;
else if (grade >= 0)
cout << "The short bus is this way." << endl;
else
cout << "Pease enter a valid grade." << endl;
system("pause");
return 0;

}
this shows the perfect result as al the conditions are included in it!
Last edited on
You can have a score over 100 if there was extra credit.

You can't have a score under 0, tho.
You could quite possibly. Take those tests where wrongs are -marks. (Cdn math contests are that way). You would have to screw up bigtime, but it is possible.
And stop bolding the code. There are code tags for that.
I'm going to go back and add in the logic for greater than 100 and less than 0. Also, i'm going to loop it with a letter or word as an exit condition. I just needed to figure out what I was doing wrong there. I was staring at the iff, but wasn't looking at my condition.
Topic archived. No new replies allowed.