my first IF statment

Hello, im working on 3 chapter of programming book. It tell me to test myself by see if i can have myself asking which sex my friend is. However the book didnt explain much about if and else. The test told me to add if there. I couldnt get it to working. M and F doesnt work. If i type f twice it'll pop both if statment on cmd. Im confused on how it work.

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
43
44
45
46
47
48
49
50
  #include "Std_lib_facilities.h"

int main()

{
		cout << "Hello! Please type your first name.\n";
	string first_name;
	cin >> first_name;
	cout << "\n";
		cout << "Hi " << first_name <<"\n";
	cout << "\n";
	{
	
		cout << "Please enter first name you want to write to.\n";
	string first_name;
	cin >> first_name;
	cout << "\n";
		cout << "Dear " << first_name << "," << "\n";
	cout << "\n";
	cout << "\n";
		cout << "How are you? What have you been doing these day?\n";
	cout << "\n";
		{
		cout << "Is there any other friend you would like to write?\n";
	cout << "\n";
	string friend_name;
	cin >> friend_name;
	cout << "\n";
		cout << "Have you seen " << friend_name << "?" <<"\n";
	cout << "\n";
	
		cout << "Enter m if your friend is male, enter f if your friend is female.\n";
	char friend_sex1 = ' ';
	char friend_sex2 = ' ';
	char m = friend_sex1;
	char f = friend_sex2;
	cin >> friend_sex1;
	cin >> friend_sex2;
			if(m = friend_sex1){
			
		cout << "If you see " << friend_name << "please ask him to call me.\n";}
			if(f = friend_sex2){
			
		cout << "If you see " << friend_name << "please ask her to call me.\n";}
	
	
		}
	
	}
}
Hi,
Firstly :
1
2
3
4
5
6
if(m = friend_sex1){
			
		cout << "If you see " << friend_name << "please ask him to call me.\n";}
			if(f = friend_sex2){
			
		cout << "If you see " << friend_name << "please ask her to call me.\n";}


==>
1
2
3
4
5
6
if(m == friend_sex1){
			
		cout << "If you see " << friend_name << "please ask him to call me.\n";}
			if(f == friend_sex2){
			
		cout << "If you see " << friend_name << "please ask her to call me.\n";}
(=) : Assignment operator
(==) : Equal comparison operator
Does that help you? :)
Line 33-34: You're setting friend_sex1 and friend_sex2 to blanks.

Line 35-36: You setting m and f to blanks. You probably want to set these to 'm' and 'f' respectively.

Line 36-37: Why are you asking for the sex twice?

Line 39,42: You're using the assignment operator (=), not the comparison operator (==).

33
34
35
36
37
38
39
	char friend_sex;
	cin >> friend_sex;
	
	if (friend_sex == 'm')
	    cout << "If you see " << friend_name << "please ask him to call me.\n";
	if (friend_sex == 'f')
            cout << "If you see " << friend_name << "please ask her to call me.\n";


Thanks, it work perfectly, I see that i should had put assign first then variable in plus that equal comparison, i completely missed that. to make sure, age is number as in int. Will i need to conversion?

number 4 on test, said to put declare a char variable called friend)sex and initialize its value to 0. Can somebody explain what reason to initialize value to 0. What is different between giving it a assigned and 0 value?
age is number as in int. Will i need to conversion?

No, you will not need to convert. cin will convert input to an int automatically.

Can somebody explain what reason to initialize value to 0.

It's a good idea to always initialize your variables. I did not do so above, because the declaration was immediately followed by a cin which set the variable. Sometimes however, the declaration and the input are widely separated in the code, or the input is subject to conditionals which may cause the cin to be skipped. In those cases, it's always a good idea to explicitly initialize the variable.
Topic archived. No new replies allowed.