What am i doing wrong?

Hi. First time posting here. I've run into a problem i can't solve by myself. I purchased a web course on programming basics using c++, and i'm on the chapter going trough the if-else statement.

I've been stuck with this exercise for 2 days now, and i'm slowly losing my motivation, so any help or hint what might be wrong would be awesome.

On the problem, here is the source code of the program:

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
#include <iostream>
using namespace std;
int main()
{
	int ika;
	char sukupuoli;
	char n;
	char m;
	
	
	cout<<"Oletko mies vai nainen (m/n) "<< endl;
	cin>>sukupuoli;
	cout<<"Anna ikäsi: "<< endl;
	cin>>ika;
	if(sukupuoli = m, ika > 0 && ika < 55)
		cout<<"Olet mies parhaassa iässä!"<< endl;
	else if(sukupuoli = m, ika >=55 && ika < 100)
		cout<<"Olet viisas mies!"<< endl; 
	else if(sukupuoli = n, ika > 0 && ika < 55) 
		cout<<"Olet neito kauneimmillaan!"<< endl;
	else if(sukupuoli = n, ika >=55 && ika < 100)
		cout<<"Olet nuori ikäiseksesi!"<< endl;
	else
		cout<<"Ohjelmassa tapahtui virhe!"<< endl;
		
	return 0;
	
}


Variable names and text is in Finnish but it shouldn't matter. Here's how the program is supposed to work:

User is asked his/her gender (m/n). The gender is written into the variable sukupuoli. Then the user is asked his/her age. The age is written into the variable ika.

Depending on user input, the program will print a phrase. For example, user is male and 44 years old. The program prints "Olet mies parhaassa iässä!".

All fine till now. I enter "n" and "12" and it's supposed to print "Olet neito kauneimmillaan!" but it's not. Instead it prints the same as the last example. The program seems to completely ignore the else-if's for female users.

I've been reading the code from all angles including standing on my head, but i can't for the life of me figure out what's wrong with it.

Sorry for my English skills and thanks for any help on advance!






sukupuoli = m, ika > 0 && ika < 55 should look like sukupuoli == 'm' && ika > 0 && ika < 55
see http://www.cplusplus.com/doc/tutorial/constants/ and http://www.cplusplus.com/doc/tutorial/operators/
When you to know particula variable contains particular value, you need to use '==' instead of just '='. using '=' just assigns right value to left. That is reason why you always print male.

f(sukupuoli == m && ika > 0 && ika < 55)
cout<<"Olet mies parhaassa iässä!"<< endl;
else if(sukupuoli == m, && ika >=55 && ika < 100)
cout<<"Olet viisas mies!"<< endl;
else if(sukupuoli == n && ika > 0 && ika < 55)
cout<<"Olet neito kauneimmillaan!"<< endl;
else if(sukupuoli == n && ika >=55 && ika < 100)
cout<<"Olet nuori ikäiseksesi!"<< endl;
Alright, that clears it up. Thanks alot!
Topic archived. No new replies allowed.