If then Else

im trying to make this program work. it is supposed to print man when we enter male,Male,M or m and female if anything else is entered.... but it prints man all the time... thank you

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

int main()
{
	string name;
	string gender;
	cout<<"Enter Your Name"<<endl;
	cin>>name;
	cout<<"Enter your Gender"<<endl;
	cin>>gender;

	if(gender = 'Male'||'male'||'M'||'m')
	{
		cout<<"man"<<endl;
	}
	
	else
	{
		cout<<"Female"<<endl;
	}
	cout<<"Hello "<<name<<"."<<endl;
	return 0;
}
if(gender = 'Male'||'male'||'M'||'m')

You can't do compound expressions in this form. you must specify each test separately. also = is asignment, == tests equality.

if( gender == "Male" || gender == "male" || gender == "M" || gender = "m" )
Last edited on
Single quotes vs double quotes.

See http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters
'male' is an integer.
'm' is a char.

See http://www.cplusplus.com/reference/string/string/operators/
You do get the char * required by == with double quotes.
oops. I missed that one :)
i copied the exact amendments you gave..... but it gives an error .. no operator "||" matches these operands ....
Did you make sure to have == and not = for all of your conditions in the if statement? In Esslercuffi's post, it appears he made the mistake of only putting one equals sign for the last condition when checking if gender == "m"
now its working.. thanks alot.. much appreciated... :)
Topic archived. No new replies allowed.