If Else Statement

closed account (EwkNwA7f)
if gender is male and grade is 70 and above,display "goodboy";
if gender is female and grade is 70 above, display "goodgirl";
but if grade is below 70 regardless of gender,display "badchild";


heres my code:

#include<iostream.h>
#include<conio.h>
char grade;
main()
{
int grade;
cout<<"gender:";
cin>>gender;
cout<<"grade:";
cin>>grade;
if(gender=='M'||'m')
if(grade>=70)
cout<<"goodboy";
if(gender=='F'||'f')
if(grade>=70)
cout<<"\n goodgirl";
else
cout<<"\n badchild";

getch();
return 0;
}

PROBLEM:
The only problem is for example
for gender , i put F
for grade , i put 90
goodboy and goodgirl ,
both appears on my output .
i dont have any problem in badchild .

again, for example

gender:M
grade:75
goodboy
goodgirl

thats how it looks like .
instead of goodboy only , the goodgirl apeears also.

Note: its for turbo c++

THANKYOU SO MUCH FOR YOUR HELP






if(gender=='M'||'m') will always be true, because 'm' is non-zero and therefore always true.

By the same rationale, if(gender=='F'||'f') will always be true.

You need to go back to your textbook and look more carefully at how to construct if statements with compound conditions (e.g. with || conditions).
Last edited on
There is nothing wrong with if(gender=='F'||'f').

The problem with your code is that after if(gender=='M'||'m'), if(grade>=70) disregards the previous. The same way with if(gender=='F'||'f'), if(grade>=70) disregards the previous conditional statement. Thus, as long as the grade>=70 is met, it prints both.

you can have this code:

-----
1
2
3
4
5
6
7
8
if (grade < 70)
	cout<<"\n badchild";
else {
	if (gender == 'm' || gender == 'M')
		cout<<"goodboy";
	else 
		cout<<"goodgirl";
}

-------

or this code:

1
2
3
4
5
6
7
8
9
10
11
12
if (gender == 'm' || gender == 'M')  {
     if (grade < 70)
          cout<<"\n badchild";
     else
          cout<<"goodboy";
}
else {
     if (grade < 70)
          cout<<"\n badchild";
     else
         cout<<"goodgirl";
}

-----------------
or have the if(gender=='F'||'f') first.
Last edited on
I would agree with jrfrago, but would like to add that in line 6 of his first example and line 10 of his second should be if (gender == 'F' || gender == 'f') as opposed to just an else. This is because if someone were to put any letter other than an M/m, it would return a "goodgirl" if their score was high enough.

You could have an initial if statement at the beginning that would test for valid input and continue to ask for the input until it receives it.
@BranL- That is more accurate but it is with the assumption that if gender is not 'm' nor 'M' it must be 'f' or 'F'. Data validation is already assumed.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
char gender;
int grade;
cout << "Enter the gender?" << endl;
cin >> gender;
cout << "Enter the grade?" << endl;
cin >> grade;
if (gender=='f'&& grade>=70)
cout << gender <<"is a goodgirl" << endl;
else if(gender=='m' && grade>=70)
cout<< gender <<"is a goodboy" << endl;
else
cout <<"\n Badboy" << endl;
return 0;
}




Last edited on
That also works but line 16 should be cout<<"\n badchild ";
That also works but line 16 should be cout<<"\n badchild ";

If I had a penny for every time a developer didn't bother to read the specification properly, I'd be a rich man :D
Topic archived. No new replies allowed.