C++ Can';我的IF语句似乎无法正常工作

C++ Can';我的IF语句似乎无法正常工作,c++,if-statement,conditional-statements,conditional-operator,C++,If Statement,Conditional Statements,Conditional Operator,我似乎无法让这些if语句按预期工作。 无论我在“字符串应答”中输入什么,它总是跳入第一个IF语句,其中的条件设置为仅在应答为“n”或“n”时执行块,或者在应答为“y”或“y”时执行块。如果键入任何其他内容,则应返回0 // Game Recap function, adds/subtracts player total, checks for deposit total and ask for another round int gameRecap() { string

我似乎无法让这些if语句按预期工作。 无论我在“字符串应答”中输入什么,它总是跳入第一个IF语句,其中的条件设置为仅在应答为“n”或“n”时执行块,或者在应答为“y”或“y”时执行块。如果键入任何其他内容,则应返回0

    // Game Recap function, adds/subtracts player total, checks for deposit total and ask for another round
    int gameRecap() {
    string answer;
    answer.clear();

    cout << endl << "---------------" << endl;
    cout << "The winner of this Game is: " << winner << endl;
    cout << "Player 1 now has a total deposit of: " << deposit << " credits remaining!" << endl;
    cout << "-------------------------" << endl;

    if (deposit < 100) {
       cout << "You have no remaining credits to play with" << endl << "Program will now end" << endl;
       return 0;       
    }
    else if (deposit >= 100) {
       cout << "Would you like to play another game? Y/N" << endl;
       cin >> answer;
       if (answer == ("n") || ("N")) {
          cout << "You chose no" << endl;
          return 0;
       }
       else if (answer == ("y") || ("Y")) {
          cout << "You chose YES" << endl;
          currentGame();
       }
       else {
            return 0;
       }
       return 0;
    }
    else {
         return 0;
    }
return 0;
}
//游戏重述功能,增加/减少玩家总数,检查存款总额并请求下一轮
int gameRecap(){
字符串回答;
回答。清楚();
cout这是不正确的:

if (answer == ("n") || ("N"))
应该是

if (answer == "n" || answer == "N")
<> p> >了解当前代码编译的原因:在C++和C中,将隐式<代码> > 0 < /代码>添加到不表示布尔表达式的条件中。因此,表达式的第二部分变为

"N" != 0

始终为
true
“N”
是一个字符串文字,它永远不能为
NULL

此部分计算不正确:

if (answer == ("n") || ("N")) {}
应该是:

if (answer == "n" || answer == "N") {}

|
操作符的工作方式与您似乎认为的不同

if (answer == ("n") || ("N"))
正在检查
答案
是否为
“n”
,如果不是,则将
“n”
作为布尔值进行计算,在本例中,布尔值始终为真。您真正想做的是

if (answer == ("n") || answer == ("N"))
您还应该针对
“y”
“y”
进行类似的调整