你好,我目前正在学校学习c ++,并有一个项目需要帮助。
我必须编写一个程序,以读取座位数并将其存储在二维数组中。空座位是标签,如果用户购买了座位,它将变成*。奇数行有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] = '*';
}
}
}
}
我的购买座位代码:
case 2:
cout << "Purchase a Ticket\n\n";
do {
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
。