C++ 在c+;中包含字符串的if语句和+; #包括 使用名称空间std; int main(){ cout
行C++ 在c+;中包含字符串的if语句和+; #包括 使用名称空间std; int main(){ cout,c++,string,if-statement,C++,String,If Statement,行if(response==“yes”,“y”,“yes”,“y”)没有做您认为它做的事情。逗号运算符计算其每个操作数,丢弃左侧的结果,并将右侧的结果作为表达式的结果。因此,您编写的内容相当于if(“y”)。您需要使用逻辑OR运算符来组合不同的情况。例如,if(response==“yes”| response==“y”| response==“yes”| response==“y”)您的if语句条件错误 #include <iostream> using namespace st
if(response==“yes”,“y”,“yes”,“y”)
没有做您认为它做的事情。逗号运算符计算其每个操作数,丢弃左侧的结果,并将右侧的结果作为表达式的结果。因此,您编写的内容相当于if(“y”)
。您需要使用逻辑OR运算符来组合不同的情况。例如,if(response==“yes”| response==“y”| response==“yes”| response==“y”)您的if语句条件错误
#include <iostream>
using namespace std;
int main(){
cout << endl;
cout << "Welcome to the wonderful world of a spy" << endl;
cout << "Today we are to decode some information that has been provided." <<endl;
string response;
cout << "Are you ready?" << endl;
cin >> response;
if (response == "yes", "y", "Yes", "Y"){
cout << "Alright, let's go!!!" << endl;
}
else {
cout << "Well, too bad. We are going to do it anyways." << endl;
}
}
让我们从实际的代码片段开始,而不是屏幕截图。请将代码粘贴到您的问题中。请不要尝试猜测语法。if语句的结果是对
“Y”
的求值,该值始终为true
(其余部分被丢弃,它们没有副作用)。请获取
if(response == "yes" || response == "y" || response == "Yes" || response == "Y")
{
//then do whatever...
}else{
//do it anyway...
}