C++ Assign Seating Chart Array

Hello Everyone,

Looking for some assistance with my code. I was able to get the initial code to allow a user option to enter data and display the .dat file in order, but I need to cut out the option for the user to enter information and just have the code compile and run with the output to show an array chart with A-F at the top and 1-13 on the side,

A B C D E F
1 * X * X * *
2 * * * X * *

Stars are for the spots not taken and X is for spots that are. The .dat file has characters for each spot, R1C is Row 1 Column C. Any help with changing this code would be great, I'm pretty spent.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int totalDigits(string s)
{
int c = 0;
for (int i = 0; i < s.size(); ++i)
{
if (s[i] >= '0' && s[i] <= '9')
{
c++;
}
}
return c;
}

void readData(ifstream &inFile, char planeTickets[13][6])
{
vector<string> v;
string s;
while (inFile >> s)
{
stringstream sstream;
sstream << s;
string temp1, temp2;
int n;
sstream >> temp1 >> n >> temp2;
int t = totalDigits(s);
int row = atoi(s.substr(1, t).c_str()) - 1;
int column = s[1 + t] - 'A';
planeTickets[row][column] = 'X';
}
}

void writeToFile(ostream &out, char planeTickets[13][6])
{
for (int i = 0; i < 13; ++i)
{
for (int j = 0; j < 6; ++j)
{
if (planeTickets[i][j] == 'X')
out << "R" << (i + 1) << (char)('A' + j) << "\t";
}
out << endl;
}
}

int main()
{
ifstream inFile;
inFile.open("reservation.dat");
if (inFile.is_open())
{
char planeTickets[13][6];
readData(inFile, planeTickets);
while (true)
{
int row;
char column;
cout << "Enter row(1 - 13)(-1 to exit): ";
cin >> row;
if (row == -1)
{
break;
}
cout << "Enter colum(A - F): ";
cin >> column;
planeTickets[row - 1][column - 'A'] = 'X';
}
ofstream outFile;
outFile.open("reservation_table.dat");
writeToFile(outFile, planeTickets);
writeToFile(cout, planeTickets);
}
else
{
cout << "Can not open reservation.dat" << endl;
}
return 0;
}


/*
reservation.dat
R1C R1E R1F
R2B R2D R2F
R3C R3D R3F
R4A R4C R4E R4F
R5B R5D
R6B R6F
R7A R7E R7F
R8B R8D R8E
R9A R9C R9D R9F
R10B R10D R10E R10F
R11C R11E
R12C R12D R12F
R13E

reservation_table.dat
R1C R1E R1F
R2B R2D R2F
R3B R3C R3D R3F
R4A R4C R4E R4F
R5B R5D
R6B R6F
R7A R7E R7F
R8B R8D R8E
R9A R9C R9D R9F
R10B R10D R10E R10F
R11C R11E
R12C R12D R12F
R13E R13F

I tried putting the code into the code brackets, but the button is not working.
Last edited on
Hello there,
Your text files look complicated. Can you please explain what you want to do with reservation.dat and reservation_table.dat?
Probably don't have to look at the reservation_table.dat file that I posted above.


reservation.dat
R1C R1E R1F
R2B R2D R2F
R3C R3D R3F
R4A R4C R4E R4F
R5B R5D
R6B R6F
R7A R7E R7F
R8B R8D R8E
R9A R9C R9D R9F
R10B R10D R10E R10F
R11C R11E
R12C R12D R12F
R13E

Each data entry represents a passenger’s seating assignment. For example, R1C represents Row 1, Seat C and R12D represents Row 12, Seat D.



B. Use a user defined function to read the data into an array. (20%)
C. Output the seating plan to an output file (reservation_table.dat) instead of to the output console.

I guess it's not just an array, it has to be setup like a chart where each spot mention in the .dat file is to mark an X in an array chart.
Last edited on
Your code seems to be working pretty well, though it could be tidied up a little bit in places.

One thing I'd recommend. Rather than having lots of number 13 and number 6 scattered throughout the code. just specify those values once, right at the start.
1
2
const int ROWS = 13;
const int COLS = 6;
Then change the code to use the constants ROWS and COLS wherever needed.

Your function readData() does work, but it seems you were trying out multiple ideas before settling on a solution. There is a vector v, a stringstream and several other variables which are used but then discarded.
Your code simplifies to this:
1
2
3
4
5
6
7
8
9
    string s;

    while (inFile >> s)
    {
        int t = totalDigits(s);
        int row = atoi(s.substr(1, t).c_str()) - 1;
        int column = s[1 + t] - 'A';
        planeTickets[row][column] = 'X';
    } 
Well, it looks ok, but it makes use of the additional function totalDigits(), so it is more weighty than at first sight.

As an alternative, you could have used the stringsteam, perhaps like this:
1
2
3
4
5
6
7
8
9
10
11
    string s;

    while (inFile >> s)
    {
        istringstream ss(s);
        char R, C;
        int row;
        ss >> R >> row >> C;
        int col = C - 'A';
        planeTickets[row-1][col] = 'X';
    }


The seating chart which you mentioned is fairly straightforward, you just need something like this:
1
2
3
4
5
6
7
    for (int row = 0; row < ROWS; ++row)
    {
        for (int col = 0; col < COLS; ++col)
            out << setw(2) << planeTickets[row][col]; 
            
        out << endl;
    }


To polish the output, it just needs a heading line and row number at the start of each line.

However - the output will contain garbage instead of '*' symbols. That's because the array was not initialised. You could add something like this at the start of the function readData()
1
2
3
    for (int row = 0; row < ROWS; ++row)
        for (int col = 0; col < COLS; ++col)
            planeTickets[row][col] = '*'; 

Topic archived. No new replies allowed.