CodersSC
CodersSC

Reputation: 710

C++ if else statement keeps executing the if and not the code under the else

I have an if else statement but it seems to be executing the code under the if and it should execute the code under the else but i cannot see the reason why my code is below.

If you look at the last if statement where it checks to see if those characters are shown if you notice from 0 - 3 D,W,O,P are not in cells 0 - 3 but it still executes the print statement there can someone tell me why?

will be happy for any help

        order.push_back("V"); //V
            order.push_back("I");//F
            order.push_back("F");//I
            order.push_back("N");//O
            order.push_back("D");//O
            order.push_back("W");//O
            order.push_back("O");//O
            order.push_back("P");//O
            order.push_back("Y");//O
            order.push_back("C");//O
            order.push_back("L");//O
            order.push_back("E");//O
            order.push_back("R");//O
            order.push_back("X");//O

                if(order.front() == "V")
                {
                     it = find(order.begin(), order.end(), "I");
                     ++it;
                     std::string o = *it;
                     DCS_LOG_DEBUG("NEXT 0 " << o);
                     DCS_LOG_DEBUG("NEXT " << *it);


                     int i = find(order.begin(), order.end(), "N") - order.begin();
                     int pos = i;

                     DCS_LOG_DEBUG("POS " << pos);

                     for(int i1 = 0; i1 < pos; i1++)
                     {
                         DCS_LOG_DEBUG("IN LINE " << order[i1]);

                         if(order[i1] == "D" || "W" || "O" || "P")
                         {
                             DCS_LOG_DEBUG("It matches one of the above incorrect");
                         }
                         else
                         {
                             for(pos; pos < order.size(); pos++)
                             {

                                 DCS_LOG_DEBUG("FOUND AFTER POS INDEX " << order[pos]);
                                 }
                       }

Upvotes: 0

Views: 405

Answers (4)

Boris Strandjev
Boris Strandjev

Reputation: 46943

this check is broken:

if(order[i1] == "D" || "W" || "O" || "P")

It is always true. In c++if you want to compare against many values you do like:

if(order[i1] == "D" || order[i1] == "W" || order[i1] == "O" || order[i1] == "P")

Your check will always be true, because the second check is just "W" which is a char [], value, not 0, which means it is always true.

Upvotes: 3

karlphillip
karlphillip

Reputation: 93410

I think you meant this:

if ( (order[i1] == "D") || 
     (order[i1] == "W") || 
     (order[i1] == "O") || 
     (order[i1] == "P") )
{
    // code...
}

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 612794

if(order[i1] == "D" || "W" || "O" || "P")

checks order[i1] == "D" for truth. If that is not true it then checks "W" for truth. That is always true and so the compiler can probably work out that this test always evaluates to true.

What you really mean is

if(order[i1] == "D" || order[i1] == "W" || order[i1] == "O" || order[i1] == "P")

Upvotes: 7

CB Bailey
CB Bailey

Reputation: 791441

When you say:

if(order[i1] == "D" || "W" || "O" || "P")

you probably mean:

if(order[i1] == "D" || order[i1] == "W" || order[i1] == "O" || order[i1] == "P")

"W", decaying to a non-null pointer converts to true when converted to a bool.

Upvotes: 4

Related Questions