c++ If, else If不读else If.或发现函数不正确
C++ If, else if not reading else if. OR Find function incorrect?
我不太明白。程序构建没有问题。问题在于,无论用户输入什么,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")
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- 使用if-else将数字转换为单词
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- C++ If/Else 语句被跳过
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 具有嵌套 if-else 的循环的时间复杂度
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 简化对两个布尔值的 4 个 if/else 检查
- 如何在C++中创建 if else 循环而不是多个嵌套?
- 为什么切换 if else 语句的顺序会导致错误?
- 如何使用 if else 语句
- if/else 语句输出由于加号或减号而未显示正确的消息
- 如何解释 #if/#else 位域?(VC++菜鸟)
- if-else 语句仅按特定顺序工作,我不知道为什么