2D Array Help!

This is a code that I am working on and need to take in a chart of a airplane seating assignments and need users to be able to reserve a seat. I have to create a two-dimensional array that will be filled up from this file and need a little guidance on how to go about this because I want the code to be able to handle any size chart. Am I doing this right? is there a better way to go about this?
Example chart file:
1 A B C D E F
2 A B C D E F
3 A B C D E F
4 A B C D E F
5 A B C D E F
6 A B C D E F
7 A B C D E F
8 A B C D E F
9 A B C D E F
10 A B C D E F

Code:
void ReadChart() {
ifstream inChart;
const char COL = 100;
const int ROW = 100;
char seatChart[ROW][COL];



inChart.open("chartIn.txt");



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


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



if (inChart.eof()) {
break;
}

inChart >> seatChart[i][j];

cout << seatChart[i][j] << " ";
if (seatChart[i][j] == 'F') {

cout << endl;
}


}

}





}
Is this a valid chart file?

6 D


Are you assuming that:
a) all rows are numbered sequentially.
b) all rows have the same number of seats.
Neither of these things are always true in real aircraft.

If the row number is in the file, then you need a inChart >> to deal with it.

> I want the code to be able to handle any size chart.
Then you should probably be using std::vector instead of arrays fixed at 100.






Directions just say be able to work with any size airplane.. I thought that also about the row number being involved in the file and the row number is suppose to remain in the file. Would I just create a separate array and include that within one of the for loops? I tried to do something like this, but apparently did not do it right because it came out with some crazy numbers.

so vector of a vector would probably work better? I will have to freshen up on my vectors and redo some of the code and see how that works.
Read each line of the file using getline, then do something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>
using namespace std;
void foo ( ) {
    istringstream is("7 A B C D E F G H I");
    int row;
    is >> row;
    char seat;
    while ( is >> seat ) {
        cout << "Row=" << row << ", seat=" << seat << endl;
    }
}

int main ( ) {
    foo();
}

$ g++ foo.cpp
$ ./a.out 
Row=7, seat=A
Row=7, seat=B
Row=7, seat=C
Row=7, seat=D
Row=7, seat=E
Row=7, seat=F
Row=7, seat=G
Row=7, seat=H
Row=7, seat=I
That would work! However, our professor actually wants us to use two-dimensional arrays, probably will allow vectors, so sorry about that!

I just reread the project tasks and the extra credit just says that we need to handle any size airplane but without the extra credit aspect, the file chart is 10 rows with 6 seats in each which I have shown above. That is why I changed the ROW and COL to be 100 because I want that index to stay high enough to handle any size. Got any advice how to tackle that kind of chart with two dimensional arrays or vectors?
The first problem is that '100' isn't really any size.

The other problem is that
1
2
for ( c = 0 ; c < COL ; c++ )
    inChart >> seat;

does a very bad job of figuring out where one row ends, and another row starts.
You can do it, but it's messy.

That's why I suggested you read a whole line using getline(), then pick it apart once it's in memory.

You evolve the program in 3 steps.
- solve the minimal requirement of 6x10 using arrays (then backup your code).
- solve the expanded requirement for any size up to 100x100 (using arrays) (and backup your code).
- solve the expanded requirement for any size using vectors.
No worries, I decided to just not do that extra credit part, because I have too much going on in other classes and decided to just do the 6x10. It would take a lot more coding to keep track of the size..

Came up with just using this:
void ReadChart(string seatChart[10][6], int RowNum[10]) {
ifstream inChart;



inChart.open("chartIn.txt");



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

inChart >> RowNum[i];

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



if (inChart.eof()) {
break;
}

inChart >> seatChart[i][j];


}

}





}

However, I do have another problem if you'd like to help?

In order to reserve the seat, we have to obtain the seat that the user decides (EX: 2A, 3D). We have to obtain that with a string, but this is what I have done. I obtained the input separately with an int and a char, but he wants to obtain it as a string and after obtaining it as a string, we can play around with it however we want, just as long as we initially obtain it as a string. Is there anyway for me to obtain it with a string and take that string and return back to what I have here so I do not have to change anything? Also, all the if statements are a work in progress still.

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
void ReserveSeat(string seatChart[10][6], int RowNum[10]) {
	int RowChoice;
	char SectionChoice;
	string SeatChoice;


	do {
		cout << "Please choose a seat to reserve, with the row number and section seat (Ex: 3A, 2D): " << endl;
		cin >> RowChoice >> SectionChoice;


		
		



		if (islower(SectionChoice)) {

			SectionChoice = toupper(SectionChoice);
		}
		else if (RowChoice < 1 || RowChoice > 10 || SectionChoice < 'A' || SectionChoice > 'F') {

			cout << "Not a valid option, please try again. " << endl;
			

		}
		else {

			SectionChoice = SectionChoice;

		}
		
		SectionChoice = SectionChoice - 'A';

		if (seatChart[RowChoice - 1][SectionChoice] == "X") {

			cout << "Sorry, this seat is already reserved please try another reservation " << endl;
			
		}
		else {

			seatChart[RowChoice - 1][SectionChoice] = 'X';
			cout << "Seat was successfully reserved" << endl;
		

		}

	} while (seatChart[RowChoice - 1][SectionChoice] != "X");

}
> cin >> RowChoice >> SectionChoice;
Use getline() to read a string.
Use string stream (as shown above) to parse the line into your variables.
In the example you provided the string has spaces, so I am not sure how to take in the string and separate them into my original variables because of the input the user is suppose to provide Ex.) "3A"

so unfortunately this is all I have currently:
1
2
3
4
5
6
7
8
9
void ReserveSeat(string seatChart[10][6], int RowNum[10]) {
	int RowChoice;
	char SectionChoice;
	string SeatChoice;
	stringstream ss;

	do {
		cout << "Please choose a seat to reserve, with the row number and section seat (Ex: 3A, 2D): " << endl;
		getline(cin, SeatChoice);
Last edited on
Topic archived. No new replies allowed.