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
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";
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... :-)