I've already did a couple searches on the forum but wasn't able to find a particular solution to my problem. The first while loop in my code should only be used when the letter entered is not equal to m or f but unfortunately the while loop runs even when m or f are entered and the program does not pass this point.
But when I run the while loops when it says while (!(friend_sex=='m') || (friend_sex=='f')) then the code somehow works for m but of course not for f. Also, when I delete one of the conditions, it works for the condition left.
#include "std_lib_facilities.h"
int main ()
{
string first_name;
cout<<"Enter the name of the person you want to write to\n";
cin>>first_name;
cout<<"Dear "<<first_name<< ", \n"<< "wie geht's dir? Ich vermisse dich.";
string friend_name;
cout<<" What's your best friends name?\n";
cin>>friend_name;
cout<<"Hast du irgenwann "<<friend_name<<" gesehen?\n";
char friend_sex;
cout<<"Click m or f if your friend is male or female\n";
cin>>friend_sex;
while (!(friend_sex=='m') || !(friend_sex=='f')) {
cout<<"You have pressed an incorrect character. Please try again.\n";
cin>>friend_sex;
}
if (friend_sex=='m')
cout<<"If you see "<<friend_name<<" please tell him to call me.\n";
elseif (friend_sex=='f')
cout<<"if you see "<<friend_name<<" please tell her to call me.\n";
int age;
cout<<"How old are you?\n";
cin>>age;
while ((age<0) || (age>110)){
cout<<"you're kidding me! Try again.\n";
cin>>age;
}
cout<<"I heard you just had a birthday and are "<<age<<" years old.\n";
if (age==12)
cout<<"Next year you'll be "<<++age<<"\n";
elseif (age==17)
cout<<"Next year you can vote.\n";
elseif (age==70)
cout<<"I hope you're having a nice retirement!\n";
cout<<"Yours sincerely,\n"<<"\n";
cout<<first_name<<"\n";
}
thanks for the response. It worked but just out of curiosity, why wouldn't || work? E.g. when I enter " f ", then the first condition would be true and the second would be false so it should also work?
Just wrote out a truth table and it makes sense now. || would make the while statement true even if one was true, leading the while statement to always run.