【问题标题】:Or operator not working或操作员不工作
【发布时间】:2013-11-10 12:47:00
【问题描述】:

当我输入 start 时,即使我满足条件,程序也会输出 else 函数,我也尝试过 && ,但它仍然不起作用。任何答案将不胜感激。

#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int main ()
{
    float timer;
    bool end;
    std::string input;

    end = false;

    cout << "Enter start then a number to count down from" << ".\n";

    while (end == false){
        cin >> input;

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

        else if (input.find("start" || "restart" || "Start" || "Restart") != std::string::npos)
        {
            cin >> timer;

            while (timer>0){
                timer -= 0.1;

                Sleep(100);

                cout << timer << ".\n";
            }

            cout << "Finished! Enter restart then another number to perform another countdown, or enter end to close the program" << ".\n";
        }

        else
        cout << "Enter start" << ".\n";
    }

    return 0;
}

【问题讨论】:

  • "end" || "End" 结果为bool

标签: c++ or-operator


【解决方案1】:

替换

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

与:

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

同样适用于您的其他if

你的表达的意思似乎很明显,但是当你分解它时,它真的没有意义。 find 需要一个字符串,而"end" || "End" 不是一个字符串。

【讨论】:

  • 对于不区分大小写的比较,将输入字符串转换为全大写(或全小写)可能毫无价值,因此只需要进行 1 次比较(在本例中为 1 find )。
【解决方案2】:

逻辑或运算符,|| 仅适用于布尔表达式。

例如,如果你有

bool A = true
bool B = false
bool C = A||B;  

您将设置bool CTrue。 IT 只需要 2 个布尔值,如果其中任何一个布尔值为真,则返回真。这都是合乎逻辑的。

你可能想尝试类似的东西

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

【讨论】:

    【解决方案3】:

    || 仅适用于逻辑布尔表达式。

    来自标准(重点是我的):

    5.15 逻辑或运算符 [expr.log.or]

    || 运算符从左到右分组。 操作数都根据上下文转换为bool(第 4 条)。如果任一操作数为true,则返回true,否则返回false

    所以在input.find("end" || "End") 中,它会尝试将"end""End" 转换为bool。运算符|| 也会返回一个bool


    这里解决你需要更换的问题:

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

    通过

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

    在第二个find 中做同样的事情。

    【讨论】:

      【解决方案4】:

      C++ 根本不能那样工作。当你写

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

      编译器会看到 logical 或两个非空 const char 指针,这会产生布尔值 true。然后将其解释为具有值 1 ('\1') 的 char,然后在字符串中进行搜索 - 当然不是您想要的。如果你想知道你的字符串是否在一组字符串中,你可以使用:

      static std::set<std::string> s = { "end", "End" };
      s.find( input ) != s.end();
      

      虽然可能不是世界上最高效的代码,但使用 C++11 编译器,您也可以将其压缩成一行,如下所示:

      if( std::set<std::string>{ "end", "End" }.count( input ) ) {
          // found...
      }
      

      【讨论】:

        【解决方案5】:
         if (input.find("end" || "End") != std::string::npos)
         //             ^^^^^^^^^^^^^^
        

        || 运算符在此处未正确使用。右边的表达式将返回真,因为它是非零的,然后它将被返回。所以该语句解析为input.find("end")。您需要在那里使用两个单独的条件语句:

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

        【讨论】:

          【解决方案6】:

          我建议使用正则表达式来代替: regex

          【讨论】:

            【解决方案7】:

            函数调用的参数

            input.find("end" || "End") 
            

            具有 bool 类型,表示字符串文字“end”的地址或/和字符串文字“End”的地址不等于 0。很明显,这两个字符串文字的地址都不为零。所以调用相当于

            input.find(true) 
            

            编译器找到一个最适合这个参数的重载函数find。这个函数是

            find(charT, c, size_tipe pos = 0);

            值 true 被隐式转换为值 charT( 1 ),并且该函数尝试在字符串中查找值为 1 的 char。

            【讨论】:

              【解决方案8】:

              这里有一个修复:

              #include <iostream>
              #include <string>
              #include <windows.h>
              using namespace std;
              
              int main()
              {
                  float timer;
                  bool end;
                  std::string input;
              
                  end = false;
              
                  cout << "Enter start then a number to count down from" << ".\n";
              
                  while (end == false) {
                      cin >> input;
              
                      if (input.find("end") != std::string::npos | input.find("End") != std::string::npos)
                          end = true;
              
                      else if (input.find("start") != std::string::npos | input.find("Start") != std::string::npos | input.find("restart") != std::string::npos | input.find("Restart") != std::string::npos)
                      {
                          cin >> timer;
              
                          while (timer > 0) {
                              timer -= 0.1;
              
                              Sleep(100);
              
                              cout << timer << ".\n";
                          }
              
                          cout << "Finished! Enter restart then another number to perform another countdown, or enter end to close the program" << ".\n";
                      }
              
                      else
                          cout << "Enter start" << ".\n";
                  }
              
                  return 0;
              }
              

              应该是 if (input.find("end") != std::string::npos | input.find("End")!= std::string::nposif (input.find("end") != std::string::npos || input.find("End")!= std::string::npos 而不是 if (input.find("end" || "End") != std::string::npos)你可以使用逻辑或或逐位或。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-03-04
                • 2019-05-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多