我得做一个程序,读取座位数,并将其存储在二维数组中。 空座位是一个标签,如果用户买了一个座位,它就变成了*。 奇数排有15个座位,甚至有20个。 当我购买一个座位时,它把*放在座位上,但当我购买另一个座位时,它把它移除,把*放在新购买的座位上。 我怎样才能使它省下它在每个座位上打印的*。
全球
char ab[15][20];
我的座位打印代码:
void Show_Chart()
{
cout << "\tSeats" << endl;
cout << " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n";
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 20; j++) {
ab[i][j] = EMPTY;
if (i % 2 && j == 14) // 0 is false, 1 is true
{
break;
}
if (i == row2 && j == column2) // assuming these numbers start from 0
{
ab[i][j] = '*';
}
}
}
for (int i = 0; i < 15; i++) {
cout << endl
<< "Row " << (i + 1);
for (int j = 0; j < 20; j++) {
cout << " " << ab[i][j];
if (i % 2 && j == 14) // 0 is false, 1 is true
{
break;
}
if (i == row2 && j == column2) // assuming these numbers start from 0
{
ab[i][j] = '*';
}
}
}
}
我的购买座位代码:
cout << "Please select the row you would like to sit in: ";
cin >> row2;
cout << "Please select the seat you would like to sit in: ";
cin >> column2;
if (ab[row2][column2] == '*') {
cout << "Sorry that seat is sold-out, Please select a new seat.";
cout << endl;
}
else {
cost = price[row2] + 0;
cout << "That ticket costs: " << cost << endl;
cout << "Confirm Purchase? Enter (1 = YES / 2 = NO)";
cin >> answer;
seat = seat - answer;
seat2 += answer;
if (answer == 1) {
cout << "Your ticket purchase has been confirmed." << endl;
ab[row2][column2] = '*';
total = total + cost;
cout << "Would you like to look at another seat? (1 = YES / 2 = NO)";
cin >> Quit;
}
else if (answer == 2) {
cout << "Would you like to look at another seat? (1 = YES / 2 = NO)";
cout << endl;
cin >> Quit;
}
当我购买第2排和座位2时,它会显示这一点https://gyazo.com/0d8bd7ed02e969110db47b428c512f24
但当我购买第2排第3座位时,它并不能保存前一次购买的座位,我希望它能同时保存这两个座位。 https://gyazo.com/f865ba7145d1fafac246836975f2ee00
您有一个全局数组:
char ab[15][20];
但是,在SHOW_CHART
中,您还有一个局部变量:
void Show_Chart()
{
char ab[15][20]; // <-- shadows global
// ...
}
这个局部隐藏了全局的ab
,因此这个函数根本没有引用全局的ab
。 只需删除这一行,以引用函数中的全局ab
。