What is wrong with this script?

I looked a project on this site (grading project) and it basically asks the user to input a grade, and your program will decide whether it is a A, B, C etc etc. Let me past the code first for you to see.

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
#include <iostream>
using namespace std;

int main()
{
	float usergrade = 0;

	cout << "Enter your grade here: ";
	cin >> usergrade;

	if (usergrade = 100)
		cout << "Congratulations! You have a perfect score! " << endl;

	else if (usergrade >= 90 && usergrade <= 99)
		cout << "You have an A! " << endl;

	else if (usergrade >= 80 && usergrade <= 89)
		cout << "You have a B! " << endl;

	else if (usergrade >= 70 && usergrade <= 79)
		cout << "You have a C! " << endl;

	else if (usergrade >= 60 && usergrade <= 69)
		cout << "You have a D! " << endl;

	else if (usergrade > 0 && usergrade < 60);
		cout << "You have failed. " << endl;


	return 0;

}


When I run it, it gives the first statement (you have a perfect score) and the last statement (you failed) no matter the grade I input. I haven't learned else if statments properly yet, just using common sense and prior knowledge on this one.
Check line 11. You want to perform a comparison between usergrade and 100, but what you're doing is an assignment. This explains why the first line always prints.
Assignment: if (usergrade = 100)
Comparison: if (usergrade == 100)

The reason the last line is always printing is because you have a semicolon at the end of line 26. Review the syntax of 'if' statements. Even though you've indented line 27, that does not mean it falls within the scope of else if (usergrade > 0 && usergrade < 60); You should remove the semicolon.
Last edited on
Wow, thanks man. Solved everything.
Topic archived. No new replies allowed.