我不明白到底是怎么了。最后尝试的次数为9,即使有while循环也是如此。我希望它的猜测检查是字符串中的坐标之一,但它永远不会起作用。 :/
我将if语句移到哪里?
int main() {
int guesses, destroy, numAttempts = 11;
string guess, i;
string coordinate[3] = {"B1", "C4", "D3"};
cout << "Enter in a coordinate between A-1 and D-4 (i.e. C4): ";
cin >> guess;
guesses = 0;
destroy = 0;
while (guess != coodinate[i] && guesses < numAttempts - 1) {
cout << "Target missed. Try again: ";
cin >> guess;
guesses++;;
}
if (guess != coordinate[i])
cout << "Failed!";
else
cout << "Congrats!";
/*if (guess == coordinate) {
cout << "Target hit! Next target: ";
cin >> guess;
destroy++;
guesses++;
}
*/
}
最佳答案
您忘记增加i
.. i++
(至少,我认为您是?)。
可能是错误的一部分。
如果必须增加i,请确保它不会超出范围。
guess = input ;
guesses = 0;
while (guesses < numAttempts && guess != coodinate[i] ) {
cout << "Target missed. Try again: ";
cin >> guess;
guesses++;
i = (i+1)%3;
}
关于c++ - 字符串/数组情况c++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16265048/