Else ifs not 'catching?'

I've searched all over the place, but I can't seem to figure out just what is going wrong. The 'if' and 'else' work normally, but the 'else ifs' seem to be ignored completely. Any help would be appreciated.

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
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

int main() {


int grade;

cout<< "Enter student's percentage:\n";
cin>> grade ;

if (grade == 12(grade>=0) && (grade<60))
{
     cout<<"Student received an 'F'."<< endl;
     }

else if (grade == (grade>61) && (grade<70))
{
    cout<< "Student recieved a 'D.'"<< endl;
}


else if (grade == (grade>71) && (grade<80))
{
    cout<< "Student recieved a 'C.'"<< endl;
}


else if(grade == (grade>81) && (grade<90))
{
    cout<< "Student received a 'B.'"<< endl;
    }

else if(grade ==(grade>91) && (grade<=100))
{
    cout<< "Atudent recieved an 'A.' Well done, student."<< endl;
    }

else {cout<< "Please enter a value between 1 and 100."<< endl;
}

}
Last edited on
I think what you're looking for is...
1
2
3
4
5
6
7
8
9
if (grade >= 0 && grade < 60)
{
    cout<< "Student recieved an 'F.'"<< endl;
}

else if (grade > 61 && grade < 60)
{
    cout<< "Student recieved a 'D.'"<< endl;
} //.. etc etc 
Last edited on
Exactly as above. Your code should be as follows. "grade ==" was incorrect, as you're only testing if grade is greater than a number and less than a number.

#include<iostream>
using namespace std;

int main() {


int grade;

cout<< "Enter student's percentage:\n";
cin>> grade ;

if ((grade>=0) && (grade<60))
{
     cout<<"Student received an 'F'."<< endl;
     }

else if ((grade>61) && (grade<70))
{
    cout<< "Student recieved a 'D.'"<< endl;
}


else if ((grade>71) && (grade<80))
{
    cout<< "Student recieved a 'C.'"<< endl;
}


else if((grade>81) && (grade<90))
{
    cout<< "Student received a 'B.'"<< endl;
    }

else if((grade>91) && (grade<=100))
{
    cout<< "Atudent recieved an 'A.' Well done, student."<< endl;
    }

else {cout<< "Please enter a value between 1 and 100."<< endl;
}

}
closed account (S6k9GNh0)
@ OP: Where did you learn that syntax from exactly? I've seen this from various other beginners and I have no clue how people get the idea that this is proper syntax for what they want.
Last edited on
Try this format it looks cleaner:
else if ( grade >= 60 && grade <= 69 )

You'll need to use the ">=" and "<=" signs or change your values so you won't skip any numbers.

1
2
3
4
5
6
7
8
9
if (grade == 12(grade>=0) && (grade<60))
{
     cout<<"Student received an 'F'."<< endl;
     }

else if (grade == (grade>61) && (grade<70))
{
    cout<< "Student recieved a 'D.'"<< endl;
}


will skip 60 and 61.
Thank you all so very much, its a huge relief to know that it's a simple error.


@Computerquip: most online guides I've seen don't actually deal with boolean syntax as it relates to if statements (or maybe I managed to read over it several times). In either case, I was left more or less to my own devices when working out the correct syntax.
Topic archived. No new replies allowed.