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.
#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';
}
elseif (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';
elseif (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 .