Im not sure how this looks in code form!

Mar 14, 2017 at 11:02pm
Hi cplusplus forum,

I am trying to do a drill from a c++ learning book and am unsure how to write this in code form.

it is asking me to write a program that you can have a conversation with and i have got the first part working but then i reached this part.....

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, (some sort of manly reply)
if the friend is female, write (again replies with a silly sentence).

i would write it as follows:

cout <<"enter m for male friend and f for female\n";
char friend_sex {0};
cin >> friend_sex;
if (friend_sex = m) cout<< "reply1";
if (friend_sex = f) cout<< "reply2";

this obviously doesn't work! as M and F are not proper identifiers.......
any help would be much appreciated
Mar 15, 2017 at 12:06am
1
2
3
4
5
cout <<"enter m for male friend and f for female\n";
char friend_sex {0};
cin >> friend_sex;
if (friend_sex = m) cout<< "reply1";
if (friend_sex = f) cout<< "reply2";


not sure what line two mean but 0 is not a char and it should not work. Instead change it to something like z so its initialized and that way if you see z somewhere you know it was an error after initialization.

lines 4 and 5 dont work because youre initializing friend_sex to "m" not checking to see if friend_sex is "m". To do this you must use ==. if (friend_sex == m) cout<< "reply1";

Lastly you are right. M and F are not proper identifiers but you can use "m" and "f" in quote so it knows you are passing in a a literal char right there and not a variable.
so those lines should look like: if (friend_sex == "m") cout<< "reply1";

Hope I helped!
Last edited on Mar 15, 2017 at 12:16am
Mar 16, 2017 at 6:00pm
if (friend_sex == "m") cout<< "reply1";
May I suggest
if (friend_sex == 'm') cout<< "reply1";
instead?
Mar 16, 2017 at 10:48pm
Whats the difference? I've always been curious but never understood why.

And also I don't believe reply should be in quotes it should just be:if (friend_sex == 'm') {cout<< reply1;}
Last edited on Mar 16, 2017 at 10:48pm
Mar 16, 2017 at 10:51pm
Whats the difference?

Single quotes denote character literals.
Double quotes denote string literals.

The type of "m" is const char[2]; the type of 'm' is char.
Mar 16, 2017 at 11:18pm
and the reason for that is that
"m"
is
{'m',0} for the end of string zero byte.

Mar 17, 2017 at 1:28am
Would it be safe to say "" for strings and '' for char? And what is the benefit for using one over the other? I know it takes it both ways.

Apologies for getting off topic
Last edited on Mar 17, 2017 at 1:29am
Mar 17, 2017 at 3:31am
Would it be safe to say "" for strings and '' for char?

Yes

It shouldn't take it both ways and if your compiler does, then it assumes you know what your doing.

Also char friend_sex {0}; doesn't look right to me.
I think it should be char friend_sex='0';

It may work but if you cout the value for friend_sex, I bet you don't get a 0;
Mar 17, 2017 at 10:12am
And also I don't believe reply should be in quotes it should just be:if (friend_sex == 'm') {cout<< reply1;}

I suppose you are right, kingkush.
reply1 it's likely to be the name of a variable, so it should be passed without commas; but of course we can't be sure: it could also be a string literal.
Only Ben1011001 knows... :-)
Mar 17, 2017 at 7:25pm
@Enoizat Got me there ;)
Topic archived. No new replies allowed.