任何人都可以帮我解决C++中的if语句和字符串吗?

The*_*der 0 c++

我在使用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;
}
Run Code Online (Sandbox Code Playgroud)

在我的if语句中,它检查字符串输入是否等于yes,如果不是,则应该转到else语句.这是麻烦开始的地方,一旦我在控制台中运行我的程序,当我输入"a"或"A"之外的任何东西时,它仍然会说是.我尝试用字符/字符做,但我得到相同的输出.有人可以帮助我吗?

Cor*_*lks 6

它应该是input == "a" || input == "A".您必须单独测试每个案例.现在你的代码相当于(input == "a") || "A",true因为它"A"衰减到一个非零指针,所以它的计算结果为.