【发布时间】:2020-09-26 12:11:17
【问题描述】:
我必须编写一个程序来读取座位数并将其存储在二维数组中。空座位是标签,如果用户购买座位,它将变为 *。奇数排有 15 个座位,甚至有 20 个。 当我购买座位时,它会将 * 放在座位上,但当我购买另一个座位时,它会将其删除并在新购买的座位上加上 *。我怎样才能让它保存它打印在每个座位上的 *。
全球
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
int column2, row2,total = 0;
char ab[20][15];
char EMPTY = '#';
char FULL = '*';
int seat = 300;
int seat2 = 0;
int Quit = 1;
int choice;
int cost,answer,price;
void ShowSeats()
{
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];
}
}
}
int main()
{
while (true){
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;
cout<< "Enter the price";
cin >> price;
if (ab [row2] [column2] == '*')
{
cout << "Sorry that seat is sold-out, Please select a new seat.";
cout << endl;
}
else
{
cost = price;
cout << "That ticket costs: " << cost << endl;
cout << "Confirm Purchase? Enter (1 = YES)";
cin >> answer;
seat = seat - answer;
seat2 += answer;
if (answer == 1)
{
cout << "Your ticket purchase has been confirmed." << endl;
ab [row2][column2] = FULL;
total = total + cost;
cout << "Would you like to look at another seat? (1 = YES)";
cin>>Quit;
}
ShowSeats();
}
}}
当我购买第 2 排和第 2 座时,它会显示这个 https://gyazo.com/0d8bd7ed02e969110db47b428c512f24
但是当我购买第 2 排座位 3 时,它不会保存之前的购买,我希望它同时保存两者。 https://gyazo.com/f865ba7145d1fafac246836975f2ee00
【问题讨论】:
-
问题不完整。制作minimal reproducible example。对于初学者来说,
ab是在Show_Chart中声明的,但你也在其他函数中使用它? -
对不起,我忘了说我已将其声明为全局。
-
您可以将
ab设为向量向量,并通过引用将其传递给 Show_Chart,而不是使用全局变量。这也可以让你有不同的行有不同的长度。
标签: c++ arrays for-loop arraylist multidimensional-array