简体   繁体   中英

Logical AND OR Operators and a simple C++ problem

I am working on a c++ problem from my workbook and I have had a hard time with the behavior of the logical operators in this problem (and many others I have faced.)

Here is the code:

#include <iostream>


using namespace std;



int main()
{
string input1, input2;

cout << "Enter two primary colors to mix. I will tell you which secondary color they make." << endl;
cin >> input1;
cin >> input2;


if ((input1 != "red" && input1 != "blue" && input1 != "yellow")&&(input2 != "red" && input2 != "blue" && input2 != "yellow"))
{
    cout << "Error...";
}

else
{
    if (input1 == "red" && input2 == "blue")
    {
        cout << "the color these make is purple." << endl;
    }
    else if (input1 == "red" && input2 == "yellow")
    {
        cout << "the color these make is orange." << endl;
    }
    else if (input1 == "blue" && input2 == "yellow")
    {
        cout << "the color these make is green." << endl;
    }
    
}





system("pause");
return 0;
}

The code works as it should the way it is written. Well, almost. I need the user input to meet certain conditions. If the user puts any color in other than red, blue, or yellow, I need the program to display an error message. It will not do so the way it is written. But with the original way it was written, it would only give me an error message, even if I would put in the requisite color. For example:

if ((input1 != "red" || input1 != "blue" || input1 != "yellow")&&(input2 != "red" || 
input2 != "blue" || input2 != "yellow"))

I tried to reason this out in pseudocode to see if it made sense, and it seems like it does. Here is my reasoning: If the input1 is not equal to red, blue, or yellow, and input2 is not equal to red, blue or yellow, return an error code.

I can't seem to figure out what I am doing wrong. Can someone walk me through this?

input1 = "red" || input1 = "blue" input1 = "red" || input1 = "blue" is always true, try to think about an input for which this would return false. It would have to be equal both to red and blue , that is impossible.

If you want "if the input is not any of the options", you want input1 = "red" && input1 = "blue" . I think you want the following in your first if :

if((input1 != "red" && input1 != "blue" && input1 != "yellow") 
   || (input2 != "red" && input2 != "blue" && input2 != "yellow"))

Meaning, "if input1 is not any of these three options or input2 is not any of the other three options, the input is incorrect."

In general, put these subexpressions into temporary variables and learn to use a debugger to debug the code.

Quimby is steering you in the correct direction with his answer to your specific question. I wanted to call out another issue and make a suggestion:

If I type "red" then "blue", your program will print "purple". But if I type "blue" then "red" (opposite order), your program will not print "purple" as I would expect.

Similarly, if type "red" then "red", I'd expect your program to print "the color these make is red.". Your program would not do that either.

There's an easy fix which is to use booleans of colors selected:

cin >> input1;
cin >> input2;

bool hasRed = (input1 == "red") || (input2 == "red");
bool hasBlue = (input1 == "blue") || (input2 == "blue");
bool hasYellow = (input1 == "yellow") || (input2 == "yellow");

bool isPurple = hasRed && hasGreen;
bool isOrange = hasRed && hasYellow;
bool isGreen = hasBlue && hasYellow;
bool isRed = hasRed && !hasYellow && !hasBlue;
bool isBlue = hasBlue && !hasYellow && !hasRed;
bool isYellow = hasYellow && !hasRed && !hasBlue;

Then your print statements are similar:

if (isPurple) {
   cout << "You've got purple" << endl;
}
else if (isOrange) {
   cout << "You've got orange" << endl;
}
etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM