所有我的变量的运行时检查失败#2错误

时间:2016-02-02 04:17:50

标签: c++ c visual-studio

我的程序运行正常,但在我选择退出之后,它给了我“运行时检查失败#2 - 变量'col'周围的堆栈已损坏。”以为它也说过关于我所有的变数。

我想知道的就是如何解决正在发生的事情。

我的主要人物:

    int main()
{
    int row, col;
    double revenue = 0;
    char choice;
    char seats[MAX_ROWS][MAX_COLS];
    int tickets_sold = 0;

    double prices[MAX_ROWS] = { 40, 40, 35, 35, 35, 30, 30, 25, 25, 25, 12.5, 12.5, 12.5, 9.5, 9.5 };

    for (int rows = 0; rows < MAX_COLS; rows++)
    {
        for (int cols = 0; cols < MAX_COLS; cols++)
        {
            seats[rows][cols] = '*';
        }
    }

    do
    {
        menu();
        cin >> choice;

        if (choice == '1')
            display(seats, tickets_sold, revenue);

        else if (choice == '2')
        {
            do
            {
                cout << "\nEnter row number: " << endl;
                cin >> row;
                cin.clear();
                cin.ignore();

                cout << "Enter column number: " << endl;
                cin >> col;
                cin.clear();
                cin.ignore();

                if (row < MIN_ROWS || row > MAX_ROWS || col < MIN_COLS || col > MAX_COLS)
                    cout << "\nRow number must be between 0 and 14 and Column number must be between 0 and 19." << endl;
                else if (seats[row][col] != '*')
                    cout << "\nSeat is currently unavailable." << endl;
            } while (row < MIN_ROWS || row > MAX_ROWS || col < MIN_COLS || col > MAX_COLS || seats[row][col] != '*');

            seats[row][col] = '#';
            revenue += prices[row];

            tickets_sold++;
        }

        else if (choice == 'q')
            cout << "\nQuiting the program..." << endl;

        else
            cout << "\nInvalid selection." << endl;
    } while (choice != 'q');


    system("PAUSE");
    return 0;
}

1 个答案:

答案 0 :(得分:2)

这只是“堆栈溢出”问题。您从局部变量范围中写了一些数据。 大多数情况下,您应首先检查本地阵列上的操作。

在这种情况下,您对阵列的初始化存在问题:

for (int rows = 0; rows < MAX_COLS; rows++)
{
    for (int cols = 0; cols < MAX_COLS; cols++)
    {
        seats[rows][cols] = '*';
    }
}

行和列由相同的宏MAX_COLS初始化。显然MAX_COLS大于MAX_ROWS。