-4

I have this code running by chance and when i put anything as the answer it is showing me correct. I know we have to put ans before YES and yay, but this code was compiled too, as i mentioned if i put any word as the input the output is correct:

string ans;
cin >> ans;

if(ans == "yes" || "YES" || "yay") {
    cout << "Correct";
}else {
    cout << "Incorrect";
}
Shahryar
  • 324
  • 1
  • 3
  • 17
  • Boolean operators don't work like that. All they do is take two bool values (true or false) and return a bool value. That's the only thing they do. – eesiraed Apr 27 '18 at 04:33
  • Anything nonzero is true. `"YES"` is always true. `"yay"` is always true. You are forgetting to compare those against something. – Drew Dormann Apr 27 '18 at 04:33
  • @FeiXiang i didn't get it. – Shahryar Apr 27 '18 at 04:34
  • @DrewDormann why is it always true? – Shahryar Apr 27 '18 at 04:35
  • Forget everything you know about the word "OR" in English. Like I said, the only thing `||` does is return true if at least one of its inputs is true. – eesiraed Apr 27 '18 at 04:36
  • @FeiXiang i know what is a OR in logic. But if i input ANYTHING even if i put NO, i will get Correct as an output. I know the working form is to write ans=="yes" || ans=="YES" || ans=="yay", but why the form in the question is working like that. – Shahryar Apr 27 '18 at 04:38
  • You are trying to make three comparisons, but you have only written one. – Drew Dormann Apr 27 '18 at 04:39
  • 2
    there should be a duped of this somewhere. – Joseph D. Apr 27 '18 at 04:39
  • When you give `"YES"` to the `||` operator which takes a boolean value, it get's converted to a boolean value. When you convert something to a boolean, you get false if it is 0, and true for anything else. – eesiraed Apr 27 '18 at 04:40
  • @codekaizer there will be, but for naught. This isn't the sort of thing you can easily Google. – user4581301 Apr 27 '18 at 04:41
  • I just asked a question (which is stackoverflow is made for), but you are giving negative rates. Nice. – Shahryar Apr 27 '18 at 04:44
  • `if(ans == "yes" || "YES" || "yay")` is equivalent to `if(ans == "yes" || true || true )`. That is always true. Maybe or true or true is never false. – Drew Dormann Apr 27 '18 at 04:45
  • 1
    @Shahryar downvotes are most likely because this question is asked every other day and is easily explained by working through statement and taking [operator precedence](http://en.cppreference.com/w/cpp/language/operator_precedence) into account. I don't downvote over this one because, like I said, above, it's really hard to websearch. – user4581301 Apr 27 '18 at 04:47
  • I didn't downvote, but others probably did because they considered this to be too basic, and can be solved by carefully reading a textbook or asking your professor. – eesiraed Apr 27 '18 at 04:48
  • I'm not saying that you shouldn't try stuff that you haven't been taught, but just don't assume that they will work and get confused when they don't. – eesiraed Apr 27 '18 at 04:49
  • @user4581301 thanks a lot. – Shahryar Apr 27 '18 at 04:50
  • Thanks. A question may seem to be basic. But look at the answers some people provided below. I have questions with 10000 views... I just didn't know this answer. – Shahryar Apr 27 '18 at 04:51
  • 1
    Someone should make a canonical "Why does my comparison to multiple values always return true" question. – AShelly Apr 27 '18 at 04:51
  • @AShelly I thought of doing that, but I was afraid it would be downvoted as too basic. – eesiraed Apr 27 '18 at 04:52

3 Answers3

5

Ok here's the precedence (L->R) and associativity of logic wise operators:

(((ans == "yes") || "YES") || "yay")

Since C/C++ has no chaining unlike Python.

1st: ans == "yes" -> str to str comparison

2nd: bool result of 1st || "YES" -> bool and str comparison = always true for "YES" is not null

3rd: true || "YES" = always true

Thus, the condition will always be true for "YES" and "yay" are not null.

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
3

You have the precedence wrong.

if (var == A || B || C) means "if (var is equal to A) OR (B is not zero) OR (C is not zero)"

You want to do if (var == A || var == B || var == C). That means "if (var is equal to A) OR (var is equal to B) or (var is equal to C)"

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • i know what is a OR in logic. But if i input ANYTHING even if i put NO, i will get Correct as an output. I know the working form is to write ans=="yes" || ans=="YES" || ans=="yay", but why the form in the question is working like that. – Shahryar Apr 27 '18 at 04:42
  • Because "YES" is a non-zero value, so inside the if statement, it is always true. – AShelly Apr 27 '18 at 04:46
1

You should compare ans with each value. Every comparison with value is not "0" or "fail" value is assumed as "true". So if you input the if a condition like if("yes") it always returns a true.

int main()
{
    std::string ans;
    std::cin >> ans;

    if("yes" == ans || "YES" == ans || "yay" == ans)
    {
        std::cout << "Correct\n";
    }
    else
    {
        std::cout << "Incorrect";
    }
}
Le Ngoc Thuong
  • 279
  • 2
  • 8