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."
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
usingnamespace 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.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
usingnamespace 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
}