Hi, im trying to complete a drill from a stroustrup book and i have encountered a problem,in the drill i have to prompt for the user to choose a friend_sex by typing "m" or "f" and with two "if" statements output the corresponding result. the problem is i cant get it to work , also note that the type is a string because i had problems with the types the drill wanted but i will try to fix it, so just try to help me with the "if" statements i will be very happy to move on with that drill. thanks in advance!.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
int main()
{ usingnamespace std;
string first_name;
string good;
string friend_name;
string s;
string friend_sex = s;
string m = friend_sex;
string f = friend_sex;
cout << " enter the name of the person you want to write to " << endl;
cin >> first_name;
cout << " Dear " << first_name << " how are you ? " << endl;
cin >> good;
cout << " whats was the name of your friend again? " << endl;
cin >> friend_name;
cout << " enter m or f for friends sex " << endl;
cin >> friend_sex;
if(friend_sex == m)
cout << " If you see " << friend_name << " please ask him to call me " << endl;
if(friend_sex == f)
cout << " If you see " << friend_name << " please ask her to call me " << endl;
return 0;
}
if(friend_sex == m)
cout << " If you see " << friend_name << " please ask him to call me " << endl;
if(friend_sex == f)
cout << " If you see " << friend_name << " please ask her to call me " << endl;
Since the friend_sex is a string. you want to do this.
1 2 3 4
if(friend_sex == "m")
cout << " If you see " << friend_name << " please ask him to call me " << endl;
if(friend_sex == "f")
cout << " If you see " << friend_name << " please ask her to call me " << endl;
You want to have the if(friend_sex == "m"). m and f in the if statements shoould be in quotes because they are strings.
You have a lot of unnecessary things. Here is how I would have done it -
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string first_name;
string good;
string friend_name;
string friend_sex;
cout << "Enter the name of the person you want to write to: ";
cin >> first_name;
cout << endl << "Dear " << first_name << " how are you ? ";
cin >> good;
cout << "whats was the name of your friend again? ";
cin >> friend_name;
cout << endl << "Enter m or f for friends sex: " << endl;
cin >> friend_sex;
if (friend_sex == "m")
{
cout << " If you see " << friend_name << " please ask him to call me " << endl;
}
if (friend_sex == "f")
{
cout << " If you see " << friend_name << " please ask her to call me " << endl;
}
system("pause");
return 0;
}
it works great now thanks alot! but i dont get it why do i need quotes if it's a variable?
when i use string variables in "cout" for example i dont need quotes to use the variable only to show the text that comes with it. why is that?
it works great now thanks alot! but i dont get it why do i need quotes if it's a variable?
when i use string variables in "cout" for example i dont need quotes to use the variable only to show the text that comes with it. why is that?
You're right. If you do it like this -
1 2
string m = "m";
string f = "f";
Then you won't have to put it with quotes in the if statements. But your program is very small and you dont use m/f too much. So it's just easier to type out "m" and "f" in the if statements rather than creating 2 string variables.