C ++ for循环中途崩溃

时间:2013-08-03 06:30:36

标签: c++ for-loop

我刚刚开始学习C ++,我现在仍然坚持使用这个for循环,每当我执行它时崩溃(它是更大代码的一部分)

void placeItem()
{
    int settler = 20, castle = 5, tower = 10, mine = 10, E = 100;
    int player1Settler = 0, player1Castle = 0, player1Tower = 0, player1Mine = 0;
    int player2Settler = 0, player2Castle = 0, player2Tower = 0, player2Mine = 0;
    int dice, x, y;
    currentBoardStatus();
    cout << "Player " << playerTurn(1 || 2) << " starts first." << endl;
    for (int game = 1; game <= 46; game++)
    {
        system("CLS");
        cout << "Turn #" << game << endl;
        dice = rand() % 6 + 1;
        x = rand() % 10 + 1;
        y = rand() % 10 + 1;
        cout << "Dice number: " << dice << endl;
        if (dice < 4 && settler > 0)
        {
            cout << "Settler placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "S";
            settler = settler--;
            if (playerTurn(1))
            {
                player1Settler = player1Settler + 1;
            }
            else
            {
                player2Settler = player2Settler + 1;
            }
            currentBoardStatus();
        }
        else if (dice = 4 && castle > 0)
        {
            cout << "Castle placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "C";
            castle = castle--;
            if (playerTurn(1))
            {
                player1Castle = player1Castle + 1;
            }
            else
            {
                player2Castle = player2Castle + 1;
            }
            currentBoardStatus();
        }
        else if (dice = 5 && tower > 0)
        {
            cout << "Tower placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "T";
            tower = tower--;
            if (playerTurn(1))
            {
                player1Tower = player1Tower + 1;
            }
            else
            {
                player2Tower = player2Tower + 1;
            }
            currentBoardStatus();
        }
        else if (dice = 6 && mine > 0)
        {
            cout << "Mine placed on [" << x << "] [" << y << "]" << endl;
            gameBoard(x, y) = "M";
            mine = mine--;
            if (playerTurn(1))
            {
                player1Mine = player1Mine + 1;
            }
            else
            {
                player2Mine = player2Mine + 1;
            }
            currentBoardStatus();
        }
        else
        {
            cout << "Player skips his/her turn." << endl;
        }
        nextTurn();
        //currentBoardStatus();
        system("pause");
    }
}

一旦循环转到16,整个控制台应用程序就会停止响应,然后停在那里,只让我退出。

1 个答案:

答案 0 :(得分:2)

您的代码由于

而崩溃
string gameBoard(int i, int j)
{
    string arr[10][10];
    return arr[i][j];
}

x = rand() % 10 + 1;
y = rand() % 10 + 1;

xy的值介于1和10之间,但arr的大小为10,其最大有效索引为9.因此,如果x或{{1} }是10(或两者),他们将访问未定义的内存。

除此之外,您的代码存在许多缺陷:

  1. y想要比较值(实际上将值4分配给dice = 4为@KeithSmith指出)
  2. 您在dice中创建string arr并且从不使用它,但在int main()内声明另一个string arr然后从中返回索引(未定义)