Problems with char and logical operators

Hey! I'm just starting to learn C++ with Bjarne Stroustrup's book, and in the drill on page 83-83 it asks you to create a program that emulates you writing a letter to someone.
I'm having a problem with the part where it asks if they're male or female, leading into the requesting an age. My code is probably littered with inefficiencies and such, but my specific problem is that I can't use the logical operator ||(or) with my char friend_sex.

Am I just going about this completely wrong or am I missing something obvious?

Sorry if the code is long. Didn't really know what to cut out and have it still make sense.

Thanks!

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
36
37
38
39
40
41
42
#include "../std_lib_facilities.h" 

int main()
{
	string first_name;
	string friend_name;
	char friend_sex;
	int age;
	cout << "What's the first name of the person you want to write to? " << '\n';
	while (cin >> first_name){
		cout << "What's another friend's first name?" << '\n';
		while (cin >> friend_name){
			cout << "Are they male or female(m or f)?" << '\n';
			while (cin >> friend_sex){
				if (friend_sex != 'm' || 'f')   //Problem
					cout << "That's neither male nor female!" << '\n';
				else{
					cout << "How old is the recipient?" << '\n';
					while (cin >> age){
						if (age < 0){
							cout << "I'm pretty sure they're not under 0 years old!" << '/n';
						}
						else if (age > 100){
							cout << "I'm pretty sure they're not over 100 years old!" << '/n';
						}
						else{
							if (friend_sex == 'm')
								cout << "Dear, " << first_name << '\n' << "Have you seen " << friend_name << " lately?" << '\n'
								<< "If you see " << friend_name << ", please ask him to call me." << '\n';
							else if (friend_sex == 'f')
								cout << "Dear, " << first_name << '\n' << "Have you seen " << friend_name << " lately?" << '\n'
								<< "If you see " << friend_name << ", please ask her to call me." << '\n';
							else
								cout << "That's neither male nor female!" << '\n';
						}
					}
				}
			}
		}
	}
	return 0;
}
if (friend_sex != 'm' || 'f') //Problem this is wrong. You cannot "chain" (if you will) the logical operators like that. You need to compare each case against the variable explicitly: if (friend_sex != 'm' || friend_sex != 'f') //Problem solved .
You can't use a shorthand like that, you need to write the full condition.
Also be careful what you use, logical and && or logical or ||.

1
2
if (friend_sex != 'm' && friend_sex != 'f')
// if friend sex is not 'm' AND friend sex is not 'f' either... 

Fantastic! That worked like a charm.
Thanks so much for the quick answer (:
Topic archived. No new replies allowed.