或操作员无法正常工作 [英] Or operator not working

查看:90
本文介绍了或操作员无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入开始时,即使我满足了标准,我也会尝试使用&&同样,它仍然没有用。

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 answers would be appreciated.

#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 类似。

您的表情似乎很明显,但是当您将其分解时,这实际上没有任何意义。 查找需要一个字符串,而 end || End 不是字符串。

It seems obvious what your expression means, but when you break it down it really doesn't make sense. find expects a string, and "end" || "End" is not a string.

这篇关于或操作员无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆