如何转义 while 循环?C++

How do I escape the while loop? c++

本文关键字:while 循环 C++ 转义 何转义      更新时间:2023-10-16
void fireShip1(int Numbers2[], bool notFound, int position1, string playerOne, int numberOfSunkenShips, int numberOfShips)
{
    numberOfSunkenShips = 0;
    while (notFound = true)
    {
        cout << playerOne << ", please enter a location to fire at." << endl;
        cin >> position1;
            if (Numbers2[position1] == 0)
            {
                cout << "You missed!" << endl;
            }
            else if (Numbers2[position1] == 1)
            {
                cout << "Bullseye!" << endl;
                numberOfSunkenShips++;
                cout << "You have sunk " << numberOfSunkenShips << " ships." << endl;
                if (numberOfSunkenShips == numberOfShips)
                {
                    notFound = false;
                }
                Numbers2[position1] = 0;
                return;
            }
    }
    cout << playerOne << " has won the match!" << endl;
}

沉船数量不会高于 1。它需要具有 3 的值才能让我到达 while 循环之外的"已赢得比赛"字符串。有什么帮助吗?

while (notFound = true)

应该是:

//assume you have declared notFound
while (notFound == true)
              //^^^

或者简单地:

while (notFound)