c++ If, else If不读else If.或发现函数不正确

C++ If, else if not reading else if. OR Find function incorrect?

本文关键字:If else 函数 不正确 发现 不读 c++      更新时间:2023-10-16

我不太明白。程序构建没有问题。问题在于,无论用户输入什么,if语句都会继续执行,程序不会检查else if语句。我想我的脚本中有一个更大的结构问题,但我不确定是什么。

可能是find函数有问题。我对这个功能还是个新手。

这也可能与我如何将这个新的if-else-if语句嵌套在另一个if语句中有关。

问题出现在代码的末尾,在最后的if-else-if中。我将整个东西粘贴起来,以防在代码的其他地方出现更深层次的问题。前面的if语句运行良好。下面是有问题的特定代码段:

if (song.find(str2)){
        cout << "Thank you! I, personally, love this song.n";
        cout << "I probably wouldn't listen to you if you asked me to turn it off.n";
        Sleep(20000);
    }
    else if (song.find(str3)){
        cout << "It's not coming off, " << name <<"." << endl;
    }

谢谢你的帮助。非常感谢:)

完整代码:

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

void Sleep()
{
    Sleep();
}
void loneliness()
{
    cout << "Lonely. You must be strong, and loved, if you choose this feeling to explore.";
}
void inlove()
{
    cout << "In love. You've been watching telenovelas again.";
}
void ambitious()
{
    cout << "Ambitious. Steve Jobs Steve Jobs.";
}
void happy()
{
    cout << "Happy. Is this a thing you can just feel?";
}
int main() {
    string input;
    string input2;
    string name;
cout << "I'm bad." << endl;
cout << "My name's Raab." << endl;
cout << "Your name?" << endl;
getline(cin, name);
cout << "Tell me in one word what emotion you're feeling right now." << endl;
getline(cin, input);
cout << "Haha. " << input << "?" << " " << "Alright. I can work with that." << endl;
cout << "..." << endl;
cout << "Okay, I'm going to give you a choice." << endl;
cout << "You can trade that emotion for one other, if you'd like." << endl;
cout << "Would you like to do that?" << endl;
getline(cin, input2);
if (input2 == "Yes" && "yes") {
    int emotion;
    cout << "Nice one, bro.n";
    Sleep(350);
    cout << "Here are your options!n";
    Sleep(300);
    cout << "1. Lonelyn";
    cout << "2. In love.n";
    cout << "3. Ambitious.n";
    cout << "4. Happy.n";
    cin >> emotion;
switch (emotion) {
case 1:
    loneliness();
    break;
case 2:
    inlove();
    break;
case 3:
    ambitious();
    break;
case 4:
    happy();
    break;
}
cin.get();

}
else if (input2 == "No" && "no")
{
    std::string song;
    std::string str2 ("like");
    std::string str3 ("off");
    cout << "Well" << endl;
    Sleep(250);
    PlaySound(TEXT("whip"), NULL, SND_ASYNC|SND_FILENAME);
    cout << "." << endl;
    Sleep(300);
    PlaySound(TEXT("whip"), NULL, SND_ASYNC|SND_FILENAME);
    cout << "." << endl;
    Sleep(300);
    PlaySound(TEXT("whip"), NULL, SND_ASYNC|SND_FILENAME);
    cout << "." << endl;
    Sleep(300);
    PlaySound(TEXT("gravity"), NULL, SND_ASYNC|SND_FILENAME);
    Sleep(2500);
    cout << "Here we go. You like this song? Or do you want me to turn it off?n";
    Sleep(2000);
    cout <<"Wait.n";
    Sleep(2000);
    cout << "Don't tell me yet. I'm grooving.n";
    Sleep(7000);
    cout << "Okay, tell me what to do, " << name << "." << endl;
    getline(cin, song);
    if (song.find(str2)){
        cout << "Thank you! I, personally, love this song.n";
        cout << "I probably wouldn't listen to you if you asked me to turn it off.n";
        Sleep(20000);
    }
    else if (song.find(str3)){
        cout << "It's not coming off, " << name <<"." << endl;
    }
    return 0;
    }
return 0;
}

* *编辑:

我有点修复了这个问题。

我将上面的代码摘录改为:

if (~song.find(str2))
    {
        cout << "Thank you! I, personally, love this song.n";
        cout << "I probably wouldn't listen to you if you asked me to turn it off.n";
        Sleep(20000);
    }
    else if (!~song.find(str2))
    {
        cout << "It's not coming off, " << name <<"." << endl;
        Sleep(20000);
    }

不再检查str3,但检查str2是否不存在。这是一个进步,并且捏造了一个解决方案,但它并没有真正给我一个更深入的理解,我可以提出我,所以我仍然希望得到答案:)* *

行不正确:

if (input2 == "Yes" && "yes")

&&不是这样工作的。您必须对if语句中的每个值执行比较。应该是:

if(input2 == "Yes" || input2 =="yes")

同样,

else if (input2 == "No" && "no")
应:

if(input2 == "No" || input2 =="no")