如何使用c ++从文本文件中读取和写入3D数组?

时间:2017-02-03 03:04:29

标签: c++ arrays 3d

我目前正在制作一个涉及座位预订系统的学校计划,我试图实施一个系统,在每次运行程序后保存座位,并且一旦程序完成再次打开,显示当前的座位或可用座位。

这是我目前的代码:

#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    int seats[6][23], ticket_count, h, l;
    char column;
    column = 'A';

    for (int i = 0; i < 6; i++) {

        for (int j = 0; j < 23; j++){

            seats[i][j] = 0;

        }
    }

    for (int i = 0; i < 6; i++) {
        cout << column << " ";
        column++;
        for (int j = 0; j < 23; j++){

            cout << seats[i][j];
            cout << " ";

        }

        cout << endl;
    }

    cout << "\nEnter number of tickets to purchase: ";
    cin >> ticket_count;

    if (ticket_count > 23)
    {
        cout << "You can only purchase a maximum of 23 tickets.\n";
    }
    else if (ticket_count < 1)
    {
        cout << "You have to buy a minimum of 1 ticket.\n";
    }
    else
    {

        for (int q = 0; q < ticket_count; q++)
        {

            cout << "Ticket " << q + 1 << endl;
            cout << "Row (1,2,3,4,5,6): ";
            cin >> h;
            cout << "Seat (1-23): ";
            cin >> l;
            h = h - 1;
            l = l - 1;
            seats[h][l] = 1;
            column = 'A';

        }

        for (int i = 0; i < 6; i++) {

            cout << column << " ";
            column++;
            for (int j = 0; j < 23; j++){

                cout << seats[i][j];
                cout << " ";

            }

            cout << endl;
        }
    }

    ofstream myfile;
    myfile.open("seats.txt");
    myfile << seats;
    myfile.close();
    return 0;

}`

但是,当我打开文本文件时,它会给我一个随机的十六进制字符串。我真的可以使用一些帮助。谢谢!

0 个答案:

没有答案