Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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" << ".
";

    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 << ".
";
            }

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

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

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

Replace

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

with:

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

Similarly for your other if.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...