如何在 c++ 中保存对数组的更改

How to save a change to an array in c++

本文关键字:数组 保存 c++      更新时间:2023-10-16

我必须制作一个程序来读取席位数并将其存储在二维数组中。空座位是主题标签,如果用户购买座位,它就会变成*。奇数排有 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 19n";
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

Show_Chart中,您正在执行以下操作:

ab[i][j] = EMPTY;

对于每一个ij.这意味着每次调用此函数时都会擦除旧值。

删除该行以避免覆盖保存的席位。

要在请求任何用户输入之前初始化所有席位,您可以在进入while循环之前main执行此操作:

for (int i = 0; i < 15; i++) 
for (int j = 0; j < 20; j++) 
ab[i][j] = EMPTY;