I am just starting to learn C++ using the "C++ for Dummies" book about 3 days ago and I'm on a chapter 3 dealing with conditionals and loops. It uses the Code Blocks program and the MinGW compiler.
I created a short word problem on who is older/younger what not. The first part when you enter the Betty, Sue, Jeff, or Abby strings work fine (entire sample at bottom). The problem I encounter is during the else part, where I want the console to recognize strings that are not Betty, Sue, Jeff, or Abby. It either does not show up at all, or it shows up every time regardless if I type in the correct strings.
I can only guess that this line is incorrect in some way
<< if (y != "Betty" || "Sue" || "Jeff" || "Abby") >>
and I have also tried to replace it with
<< else >>
added "else" infront of if:
<< else if (y ... >>
and replaced the "||" Operator with "&&" in all forms. Any suggestions?
the whole part:
string y, z;
cout << "Betty is older than Sue. Jeff is younger than Betty. Sue is older than Abby." << endl;
cout << "Abby and Jeff are the same age. Who is the youngest (choose one)?" << endl;
cin >> y;
if (y == "Betty" || "Sue" || "Jeff" || "Abby")
{
if (y == "Betty")
{
cout << "Betty? You are wrong." << endl;
}
if (y == "Sue")
{
cout << "No, \"Sue\" is not a correct answer." << endl;
}
if (y == "Jeff")
{
cout << "Correct! But there is one more. Who is the other youngest person?" << endl;
cin >> z;
if (z == "Abby")
{
cout << "Correct answer! You solved the question!" << endl;
}
else
{
cout << "Bzzt! Wrong." << endl;
}
}
if (y == "Abby")
{
cout << "Correct! But there is one more. Who is the other youngest person?" << endl;
cin >> z;
if (z == "Jeff")
{
cout << "Correct answer! You solved the question!" << endl;
}
else
{
cout << "Bzzt! Wrong." << endl;
}
}
}
if (y != "Betty" || "Sue" || "Jeff" || "Abby")
{
cout << "You Fail." << endl;
}