Need help

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!.

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
31
32
33
34
35
 #include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

int main()    
{ 	using namespace 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;

}
Last edited on
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;


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 -

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
31
32
33
#include <iostream>
#include <string>
using namespace 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;
}
Last edited on
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 <string>
#include <iostream>

using namespace std;

int main()
{
	string male("m");
	string female("f");
	string sex("");

	cout << "Please enter sex: ";
	cin >> sex;
	if (sex == male) // if (sex == "m")
	{
		cout << "Male" << endl;
	}
	else if (sex == female) // if (sex == "f")
	{
		cout << "Female" << endl;
	}
	else
	{
		cout << "Sex is rubbish" << endl;
	}
}
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.
Topic archived. No new replies allowed.