Begginer help with if statements and char initiate

hello,

I am completely new to programming and working through B Stroustrup's Programming: Principles and Practice Using C++ (2nd Edition. I am currently working through a drill and I'm struggling to get it to work.
this is what the drill wants me to do: declare a char variable called friends_sex and initialize its value to O. prompt the user to enter a value of f or m for female and male. assign the value to the variable friend_sex. then use two if staements to write the following. if the friend is male "can you get him to call me". if female "can you get her to call me".

now I have added in the extra code but it wont compile because I haven't initalized "O" and also "f" and "m" in the if statements. now I can work around that by making friend_sex a string and initializing f and m as a char.
For the if statement. they will not run. when i can get the code to compile by said changes above. the program ends after the user enters f or m. what have I done wrong. I have reread the book but nothing is jumping out at me.

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
  int main()
{
    string first; //this will be the first name of the addressee
    string second;//this will be the second name of the addressee
    string friend_name;//this will be the friends name when prompted
    string name =first+" "+second;//initiate name string and combine first and second.
    char friend_sex=o;//cant get this to initiate

    cout<<"Please type the first and second name of the addressee\n";
    cin>>first>>second;//get first and second name.



    cout<<"Dear "<< name<<",\n";
    cout<<"How are you holding up? It must be terrible not being able to move around.\n";
    cout<<"I am fine BTW. I just won the lottery. lucky me.\n";
    cout<<"Any way, who are you hanging out with now?\n";//ask friends name

    cin>>friend_name;//get friends name

    cout<<"Is "<<friend_name<<" Male or female?(please enter f or m)\n";//ask friends sex

    cin>>friend_sex;//get friends sex

    if (friend_sex==f)
        cout<<"well when you see "<<friend_name<<", tell her to ring me\n";
    if (friend_sex==m)
        cout<<"well when you see "<<friend_name<<", tell him to ring me\n";
        return 0;
        }


sorry for the wall of text. and thanks for reading.

Ren
char means character. you use these to initialize them ' '

char friend_sex = 'o';//cant get this to initiate

You have to do this - string name = first + " " + second;//initiate name string and combine first and second.

After you input the first and second name, otherwise it doesnt make any sense.

and with the if statements. you have to use the character thingI showed you ' ' for the characters m and f.
1
2
3
4
if (friend_sex == 'f')
		cout << "well when you see " << friend_name << ", tell her to ring me\n";
else if (friend_sex == 'm') // use  else if 
		cout << "well when you see " << friend_name << ", tell him to ring me\n";
Last edited on
thank you for the quick reply. I will remember those now.
Topic archived. No new replies allowed.