あなたの質問は少しわかりにくいですが、このループで解決すべきいくつかの条件があります。
- ユーザーに入力を求める
- ユーザーの入力が有効かどうかを確認します (1 ~ 9 の間で、以前は使用されていません)。
- 有効な選択肢がある場合は、ループを終了します
したがって、どのような動きが行われたかを記録し、ユーザーの入力が有効な選択肢の範囲内であることを確認する必要があります。有効な選択肢が選択された場合にのみ終了するループを使用できます。
int choice;
bool used[9] = { false }; // Set all values to false
std::cout << "Interesting move, what is your next choice?: ";
do {
std::cin >> choice;
// Here we check if the choice is within the range we care about
// and not used, note if the first condition isn't true then
// the second condition won't be evaluated, so if choice is 11
// we won't check used[10] because the && will short circuit, this
// lets us avoid array out of bounds. We also need to
// offset the choice by 1 for the array because arrays in C++
// are indexed from 0 so used runs from used[0] to used[8]
if((choice >= 1 && choice <= 9) && !used[choice - 1]) {
// If we're here we have a valid choice, mark it as used
// and leave the loop
used[choice - 1] = true;
break; // Exit the loop regardless of the while condition
}
// If we've made it here it means the above if failed and we have
// an invalid choice. Restart the loop!
std::cout << "\nInvalid choice! Input: ";
} while (true);