Or operator not working

后端 未结 8 1214
一向
一向 2020-12-12 07:44

When I enter start then the program outputs the else function even though I fulfilled the criteria, I have tried with && as well and it still didn\'t work. Any answe

8条回答
  •  余生分开走
    2020-12-12 07:58

    The || works only in logical boolean expression.

    From the standard (emphasis is mine):

    5.15 Logical OR operator [expr.log.or]

    The || operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). It returns true if either of its operands is true, and false otherwise.

    So in input.find("end" || "End"), it tries to convert "end" and "End" to bool. And the operator || will return a bool also.


    Here to solve your problem you need to replace:

    if (input.find("end" || "End") != std::string::npos)
    

    by

    if ( input.find("End") != std::string::npos ||
         input.find("End") != std::string::npos )
    

    And do the same in the second find.

提交回复
热议问题