c++ - 或者接线员不工作

标签 c++ or-operator

当我输入 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;
}

最佳答案

替换

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" 不是字符串。

关于c++ - 或者接线员不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19690826/

相关文章:

c++ - Makefile 隐式规则不起作用

c++ - 指针赋值

c++ - 或无效 C++ : why does this code compile?

azure - DocumentDB 的 IN 与 OR 哪个在什么情况下更好、更快?

c++ - boost:asio 线程池实现,用于偶尔同步的任务

c++ - 在 C++ 中将大于 5 位的数字除以 2 时会失去精度

c++ - 如何判断 const char* 是否指向有效字符串?

c++ - 或无效 C++ : why does this code compile?

c++ - 在 if 语句中使用 != 运算符来检查两个条件

javascript - 三元条件下的多个 OR 运算符,