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] = '*';
| |