0

I am running a series of if/ else if / else statements using strings, and regardless of the user input the if is always returning true:

cout << "Enter the element to look up: ";
cin >> elementLookup;
cout << endl << endl;

if (elementLookup == "H" || "Hydrogen" || "hydrogen") {
    cout << "Atomic Number: " << H_NUMBER << endl << endl;
    cout << "Atomic Mass: " << H_MASS << endl << endl;
}
else if (elementLookup == "He" || "Helium" || "helium") {
    cout << "Atomic Number: " << HE_NUMBER << endl << endl;
    cout << "Atomic Mass: " << HE_MASS << endl << endl;
}
else if (elementLookup == "Li" || "Lithium" || "lithium") {
    cout << "Atomic Number: " << LI_NUMBER << endl << endl;
    cout << "Atomic Mass: " << LI_MASS << endl << endl;
}

All of the variables have been declared earlier in the code. For some reason, every time I run this code it says the first if statement is true, regardless of user input. What am I doing wrong"

jww
  • 97,681
  • 90
  • 411
  • 885
Weylin
  • 11
  • 1
  • Make you question title more abstract and explain more about your issue in the body of question instead of it. – Vahid Boreiri Sep 15 '17 at 06:20
  • Also, you should always check [cin.fail()](http://en.cppreference.com/w/cpp/io/basic_ios/fail) before *assuming* there was valid input. (E.g. the user pressing Ctrl-D would make `cin >> elementLookup` fail (with the previous value preserved in `elementLookup` and your conditionals running with a string the user did not enter... *this* time). – DevSolar Sep 15 '17 at 09:23
  • Possible duplicate of [IF statement with logical OR](https://stackoverflow.com/q/33422793/608639), [Can you use 3+ OR conditions in an if statement?](https://stackoverflow.com/q/8781447/608639), [Multiple conditions in C++ if statement](https://stackoverflow.com/q/9214464/608639), etc. – jww Sep 24 '18 at 01:06

4 Answers4

3
if (elementLookup == "H" || "Hydrogen" || "hydrogen")

This is three tests, OR'ed together.

The first test, elementLookup == "H", tests the string elementLookup for equality with "H".

The other two tests test a string literal ("Hydrogen" / "hydrogen") for being non-null.

Which is always true.

What you wanted is:

if (elementLookup == "H" || elementLookup == "Hydrogen" || elementLookup == "hydrogen")
DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Corollary: **Always** use parenthesis to group subexpressions *explicitly*. Even if you (think you) know the operator precedence table by heart. You might be wrong, the next guy might have to look it up... and in this case, you would have gotten a warning / error for comparing a string with a boolean. – DevSolar Sep 15 '17 at 06:09
  • Definitely not **Always**. For example, mixing multiplication and addition. In this case, I absolutely wouldn't add the parentheses; I expect the readers of my code to understand that `==` has higher precedence than `||`. Mixing `||` and `&&` - while I find it intuitive - I would probably add the parentheses. – Justin Sep 15 '17 at 06:13
  • 1
    @Justin: Rules of thumb tend to be worded as absolutes. Of course any good developer knows when a rule of thumb starts getting in the way, i.e. when to ignore it. Personally, I very seldom do "naked" multiplication and addition in my source, and after ~20 years in the business I also very seldom "expect" anything from the readers of my code. ;-) – DevSolar Sep 15 '17 at 07:19
0

You have to change the if condition

    if (elementLookup == "H" || elementLookup == "Hydrogen" || elementLookup == "hydrogen") {

        cout << "Atomic Number: " << H_NUMBER << endl << endl;
        cout << "Atomic Mass: " << H_MASS << endl << endl;

    }
    else if (elementLookup == "He" || elementLookup == "Helium" || elementLookup == "helium") {
        cout << "Atomic Number: " << HE_NUMBER << endl << endl;
        cout << "Atomic Mass: " << HE_MASS << endl << endl;
    }

    else if (elementLookup == "Li" || elementLookup == "Lithium" || elementLookup == "lithium") {
        cout << "Atomic Number: " << LI_NUMBER << endl << endl;
        cout << "Atomic Mass: " << LI_MASS << endl << endl;
    }

For every condition a check has to be made.

if (elementLookup == "H" || "Hydrogen" || "hydrogen") 

This means first a condition check for equality is made. After which "Hydrogen/hydrogen" is not a condition check it always turns to be a logical "1" / true. As it has some value in it. So the final || ORed will be true always

Nerdy
  • 1,016
  • 2
  • 11
  • 27
0

It seems that there is a confusion with syntax. To check the value of elementLookup the statement should be:

    if (elementLookup == "H" || elementLookup == "Hydrogen" || elementLookup == "hydrogen") {
    //Rest of your code

Lets take an example to understand why if condition always evaluates to true: The statement if(variable) will evaluate to false only if variable is either 0 or NULL. Since there are other strings in your if statement, ORing them always evaluates to 1. Hence its always true.

avi
  • 1
  • 1
-1

Here it happens because of OR condition. "Hydrogen" is NOT-NULL so OR condition will be always true so each time block of IF will be executed.