I can't seem to get these if statements to work as intended. Whatever i input to "string answer" it always jumps into the first IF statement where the condition is set to only execute the block if the answer is exactly "n" or "N" or the block where the answer is exactly "y" or "Y". If you type in anything else it should return 0.
我似乎不能让这些if语句像预期的那样工作。无论我输入什么"string answer"它总是跳转到第一个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;
}
3 个解决方案
#1
9
This is not correct:
这是不正确的:
if (answer == ("n") || ("N"))
It should be
它应该是
if (answer == "n" || answer == "N")
It is instructive to find out why your current code compiles though: in C++ and in C, an implicit != 0
is added to conditions that do not represent a Boolean expression. Therefore, the second part of your expression becomes
找出当前代码编译的原因是很有意义的:在c++和C中,隐式!= 0被添加到不代表布尔表达式的条件中。因此,表达式的第二部分变成。
"N" != 0
which is always true
: "N"
is a string literal, which can never be NULL
.
这总是正确的:“N”是一个字符串字面量,它永远不会为空。
#2
5
The ||
operator does not work the way you seem to think it does.
||运算符的工作方式并不像你想象的那样。
if (answer == ("n") || ("N"))
is checking if answer
is "n"
, and if not, it is evaluating "N"
as a boolean, which in this case is always true. What you really want to do is
检查答案是否为“n”,如果不是,它将“n”作为布尔值,在这种情况下,它总是为真。你真正想做的是
if (answer == ("n") || answer == ("N"))
You should also make similar adjustments for the check against "y"
and "Y"
.
你也应该对“y”和“y”进行类似的调整。
#3
2
This part is not evaluating correctly:
这部分评价不正确:
if (answer == ("n") || ("N")) {}
Shold be:
还得是:
if (answer == "n" || answer == "N") {}
#1
9
This is not correct:
这是不正确的:
if (answer == ("n") || ("N"))
It should be
它应该是
if (answer == "n" || answer == "N")
It is instructive to find out why your current code compiles though: in C++ and in C, an implicit != 0
is added to conditions that do not represent a Boolean expression. Therefore, the second part of your expression becomes
找出当前代码编译的原因是很有意义的:在c++和C中,隐式!= 0被添加到不代表布尔表达式的条件中。因此,表达式的第二部分变成。
"N" != 0
which is always true
: "N"
is a string literal, which can never be NULL
.
这总是正确的:“N”是一个字符串字面量,它永远不会为空。
#2
5
The ||
operator does not work the way you seem to think it does.
||运算符的工作方式并不像你想象的那样。
if (answer == ("n") || ("N"))
is checking if answer
is "n"
, and if not, it is evaluating "N"
as a boolean, which in this case is always true. What you really want to do is
检查答案是否为“n”,如果不是,它将“n”作为布尔值,在这种情况下,它总是为真。你真正想做的是
if (answer == ("n") || answer == ("N"))
You should also make similar adjustments for the check against "y"
and "Y"
.
你也应该对“y”和“y”进行类似的调整。
#3
2
This part is not evaluating correctly:
这部分评价不正确:
if (answer == ("n") || ("N")) {}
Shold be:
还得是:
if (answer == "n" || answer == "N") {}