How come it gives me like 3,2,1 when I type 75 when it should just be 2. |
Okay. So you type 75.
The code reaches this:
1 2 3
|
if (studentGrade >= 90) {
cout << "4" << endl;
}
| |
Is the studentGrade over 90? No. So the
if block doesn't execute.
Now, the code reaches this:
1 2 3
|
if (studentGrade <= 89) {
cout << "3" << endl;
}
| |
Is the studentGrade under 89? Yes. So the
if block DOES execute. "3" is output.
Now, the code reaches this:
1 2 3
|
if (studentGrade <= 79) {
cout << "2" << endl;
}
| |
Is the studentGrade under 79? Yes. So the
if block DOES execute."2" is output.
Now, the code reaches this:
1 2 3 4 5 6
|
if (studentGrade <= 69) {
cout << "1" << endl;
}
else if (studentGrade < 60) {
cout << "0" << endl;
}
| |
Is the studentGrade under 69? No. So the first if block doesn't execute, and we move on to the
else section, which looks like this:
1 2
|
else if (studentGrade < 60) {
cout << "0" << endl;}
| |
and now we see that studentGrade is NOT under 60, so this final
if block doesn't execute.
Put another way, see this?
1 2 3 4 5 6
|
if (studentGrade >= 90) {
cout << "4" << endl;
}
if (studentGrade <= 89) {
cout << "3" << endl;
}
| |
This is TWO independent blocks. Maybe you meant to use a few more
else in your code.
when it should just be 2.
Why would it be just 2? You first test for it being over 90, and then you test for it being under 89. Is it under 89? Yes. With your current logic, ANY value under 89 will get a "2". With your current logic, the score of -567 will get a "2", because any number less than 89 gets a "2".