如何在 c++ 中保存对数组的更改
How to save a change to an array in c++
我必须制作一个程序来读取席位数并将其存储在二维数组中。空座位是主题标签,如果用户购买座位,它就会变成*。奇数排有 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;
对于每一个i
和j
.这意味着每次调用此函数时都会擦除旧值。
删除该行以避免覆盖保存的席位。
要在请求任何用户输入之前初始化所有席位,您可以在进入while
循环之前main
执行此操作:
for (int i = 0; i < 15; i++)
for (int j = 0; j < 20; j++)
ab[i][j] = EMPTY;
相关文章:
- 保存/加载大量短数组到二进制文件
- 如何在数组和向量 c++ 中存储和保存输入
- 2D 数组,某些单元格保存特定数据
- 如何在c++中将字符串中的所有数字保存到多维数组中
- 将值更改为 c++ 的数组并保存文件
- 数组不保存初始化值
- 按顺序遍历 AVL 树,将值保存在数组中
- 将 QImage 保存到字节数组
- 将特征数组行保存在变量中
- 将数组保存到二进制文件不起作用 c++
- 如何将数组保存到C 中的文件中
- C++一维Char数组保存为二维Char数组
- 如何使用相同的功能模板将3个不同的数组保存到3个不同的文件
- 通过神经网络中的函数将数据从 2D 数组保存到文件中
- HDF5:将线性数组保存到三维数据集
- 如何将排序后的数组保存在文本文件中
- 将结构的 c++ 数组保存到文件和问题中以向其添加新元素
- c++多维数组保存/加载到文件错误
- 在cpp中用2d数组保存的列长度
- 如何快速将无符号字符数组保存到图像文件