【发布时间】:2016-02-02 04:17:49
【问题描述】:
我的程序运行良好,但在我选择退出后,它给了我 “运行时检查失败 #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;
}
【问题讨论】:
-
您有没有预定过边缘的一个座位(row=MAX_ROWS 或 col=MAX_COLS)?
-
这是错误的:
if (row < MIN_ROWS || row > MAX_ROWS || col < MIN_COLS || col > MAX_COLS)它应该是if (row < MIN_ROWS || row => MAX_ROWS || col < MIN_COLS || col => MAX_COLS),因为您分配了char seats[MAX_ROWS][MAX_COLS];并且 c++ 使用基于 0 的索引意味着席位[MAX_ROWS][MAX_COLS] 超出范围。 -
我尝试将其更改为 >= 和
-
你的 while() 有同样的问题
while (row < MIN_ROWS || row > MAX_ROWS || col < MIN_COLS || col > MAX_COLS || seats[row][col] != '*') -
谢谢,它没有解决我想要的问题,但它解决了我不知道的问题!
标签: c++ c visual-studio