我在使用c ++中的if语句和字符串/字符时遇到了一些麻烦。这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "-----------------------------" << endl;
cout << "|Welcome to Castle Clashers!|" << endl;
cout << "-----------------------------" << endl;
cout << "Would you like to start?" << endl;
string input;
cout << "A. Yes ";
cout << "B. No " << endl;
cin >> input;
if(input == "a" || "A"){
cout << "Yes" << endl;
}else{
if(input == 'b' || 'B'){
return 0;
}
}
return 0;
}
在我的if语句中,它检查字符串输入是否等于yes,如果不是,则应转到else语句。这是麻烦开始的地方,当我在控制台中运行程序时,如果键入“ a”或“ A”以外的任何内容,它仍然表示是。我已经尝试使用chars / characters来做到这一点,但是我得到了相同的输出。有人可以帮我吗?
"A"
并'B'
始终处于典型的实现真。
您还应该input
与他们进行比较。
另外std::string
,char
似乎不支持与进行比较,因此您还应该为b
和使用字符串文字B
。
Try this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "-----------------------------" << endl;
cout << "|Welcome to Castle Clashers!|" << endl;
cout << "-----------------------------" << endl;
cout << "Would you like to start?" << endl;
string input;
cout << "A. Yes ";
cout << "B. No " << endl;
cin >> input;
if(input == "a" || input == "A"){
cout << "Yes" << endl;
}else{
if(input == "b" || input == "B"){
return 0;
}
}
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句