Question: Why isn't this sufficient?

This is just part of a code I am working on.

Using Dev_C++.

Why is my compiler saying that 'm' and 'f' are not declared in the scope, and how would I fix this?

I am novice, thanks for your patience.

1
2
3
4
5
6
7
8
9
	char friend_sex = 0;
	cout<<"Please enter 'm' for male friend or 'f' for female friend.\n";
	cin>>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";
		}


-Incline
Last edited on
The compiler considers identifiers m and f in these statements

1
2
3
4
		if (friend_sex = m){
			cout<<"If you see "<<friend_name<<" please ask him to call me.\n";
		}
		if (friend_sex = f){


as undefined because it does not see where these identifiers were defined.

I think that you meant character constants instead of identifiers

1
2
3
4
		if (friend_sex = 'm'){
			cout<<"If you see "<<friend_name<<" please ask him to call me.\n";
		}
		if (friend_sex = 'f'){
Do the apostrophes specify the characters m and f are the options? I'm not sure I understand.


1
2
3
4
		if (friend_sex = 'm'){
			cout<<"If you see "<<friend_name<<" please ask him to call me.\n";
		}
		if (friend_sex = 'f'){


When I enter your code it returns both male and female responses.

Here's my full code:

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
#include "std_lib_facilities.h"

int main()
{
	cout<<"Please enter the name of the person you wish to write to.\n\n\n";
		string first_name;	// variable of type string
		cin>>first_name;	// read first_name
	cout<<"\n\n\nDear "<<first_name<<",\n";
	cout<<"    Hi, how are you? I miss you dearly. I am doing fine.\n\n\n";
	
	cout<<"Please provide the name of another friend.\n";
		string friend_name;
		cin>>friend_name;	// read friend_name
		cout<<"\n\n\nDear "<<first_name<<",\n";
	cout<<"    Hi, how are you? I miss you dearly. I am doing fine. ";
	cout<<"Have you seen "<<friend_name<<" lately?\n\n\n";
	
	char friend_sex = 0;
	cout<<"Please enter 'm' for male friend or 'f' for female friend.\n";
	cin>>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";
		}
}
You had time to make one more error. Instead of the assignment operator (=) you must use the comparision operator (==)

1
2
3
4
5
6
		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";
		}
I just figured it out! You are absolutely right. Thanks for the help again. I greatly appreciate it!

-Incline
Topic archived. No new replies allowed.