我无法理解这一点。该程序没有问题。问题在于,无论用户输入是什么,if语句都会继续,并且程序不会检查else if语句。我假设我的脚本中存在更大的结构问题,但我不确定它是什么。
这可能是find函数的问题。我还不熟悉这个功能。
这也可能与我如何在另一个if语句中嵌套这个新的if-else-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. Lonely\n";
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是否不存在。这是进步,并捏造一个解决方案,但它并没有真正让我更深刻的理解,我可以提出我,所以我仍然会欣赏答案:) **
答案 0 :(得分:4)
该行不正确:
if (input2 == "Yes" && "yes")
&&
并没有这样做。您必须与if语句中的每个值执行比较。它应该是:
if(input2 == "Yes" || input2 =="yes")
同样,该行
else if (input2 == "No" && "no")
应该是:
if(input2 == "No" || input2 =="no")