Program 3

Hello,

I moved to exercise 4.

Declare a char variable called friend_sex and initialize its value to 0. Prompt the user to enter an m if the friend is male and an f if the friend is female. Assign the value entered to the variable friend_sex. Then use two if-statements to write the following:

If the friend is male, write "If you see friend_name please ask him to call me."

If the friend is female, write "If you see friend_name please ask her to call me."

Now I put some code together

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>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;


int main()

{
      string friend_sex;               // friend_sex is a variable of type string
      cin>>friend_sex;                 // read characters into friend_sex
      char x=friend_sex;               // Declares a char variable called friend_sex
      x=0;                             // Initializes it`s value to 0
      cout<<"Enter m if your friend is male, if female enter n:\n";
      char m=friend_sex           // Assigns the value entered to the variable friend_sex
      char f=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";

      // If-statements

} 


The compiler doesn`t accept it, does my code look right at all?
The compiler is likely complaining about a few things.

In line 14, 17 and 18 - you're declaring a char variable and then assigning it the value of friend_sex, which you've declared to be a string.

line 17 and 18 - you're missing a semi-colon at the end of the statements (but you don't need these lines at all)

line 20 and 22 - check the syntax needed for if statements. You're missing some parenthesis. Also - if you want to see if something is equal to something, you need to use the equality operator ==, not the assignment operator =. And - if you're comparing with a char value - use single quotes around the character, e.g. 'm'.

That said,

I think you may have meant to make line 12 a string variable for friend_name, not friend_sex? And then get input to friend_name on line 13, and refer to the friend_name variable (not just "friend_name" as text) on lines 20 and 22.

Line 14/15 - I think you meant to just declare a char variable friend_sex and initialize it to '0'. There's no need for a separate x variable.

You don't need the vector, algorithm and cmath includes for this program.
Thanks for your reply, I changed the code does it look better now?

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


int main()

{
      string friend_name;               // friend_name is a variable of type string
      cin>>friend_name;                 // read characters into friend name
      char friend_sex;                // friend_sex is a variable of type char
      cin>>friend_sex;                  // read characters into friend_sex
      char m=0;          // declaring a char variable friend_sex and initialize it to '0'
      char f=0;         // declaring a char variable friend_sex and initialize it to '0'
      cout<<"Enter the name of your friend :\n";
      cout<<"Enter m if your friend is male, if female enter n:\n";
      
                     
 

      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";

      // If-statements

} 
Lines 24,26: You need () around your conditional expressions.
 
if (friend_sex=='m')


edit: You're also using the wrong single quote. You need then ' character not the ` character around your character literals.
Last edited on
You don't need lines 16 and 17. The assignment wants you to initialize the variable you declare on line 14 to 0.

Line 18 should come before line 13 - so the prompt asking for friend name comes before the program waits for input.

Line 19 should come before line 15 - so the prompt asking for friend sex comes before the program waits for input.

Lines 24 and 26 - you need to break out friend_name from the text expressions if you want to pull the actual name stored in that variable.

cout<<"If you see " << friend_name << " please ask him to call me:\n";


Edit: and on line 19 - you want them to enter f for female, not n?
Last edited on
Topic archived. No new replies allowed.