【问题标题】:How to save a change to an array in c++ [closed]如何在c ++中保存对数组的更改[关闭]
【发布时间】: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


【解决方案1】:

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;

【讨论】:

  • 是的,我意识到了这一点,尽管它仍然没有在售出的座位上显示 *,但我已将其删除。但是,它不允许我再次购买座位。
  • 那么在您制作 MRE 之前,我们无法真正解决您的问题。显示您为函数提供的输入,以及输出与您想要的不同的地方。
  • 我已经编辑了这个问题,请你再打开它。
  • 您的问题仍未完成。没有人可以仅使用提供的代码来重现您的结果。
  • 我已经制作了可重现的样本。能否请您打开问题并帮助我解决问题。
猜你喜欢
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多