Accessing data within my 2D Array

I am attempting to write a program in C++ that can be used by a movie theater to sell tickets for viewings.
The program should create a seating chart for a movie theater which is 15 rows down and 20 seats across with each seat marked with an '*' to show availability.

Then the program should ask the user if they would like to buy a ticket.
If they do, it displays this seating chart, asks the user which row they would like, then which seat they would like and finally updates the seating chart's '*' with a '#' to show the seat is now taken.

My problem is that I wrote a function to create and display the seating chart, but without repeating the same loops over and over in the function, I am unaware of how to update the '*' to a '#'.

Is there a way to do this so that I can create a separate function that can access the 2D array and update it every time I call the function?


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 seatingChart()
{
	int userRow = 0;
	int userSeat = 0;
	char seats[ROW][COL];
	cout << "Seats:";
	for (int c = 0; c < COL; c++)
	{
		cout << setw(4) << c;
	}
	cout << endl;
	for (int row = 0; row < ROW; row++)
	{
		cout << "Row " << setw(2) << row;
		for (int col = 0; col < COL; col++)
		{
			
			seats[row][col] = '*'; // Initializes all rows to be available with * symbol
			cout << setw(4) << seats[row][col];
		}
		cout << endl;
	}
	cout << "Please choose the Row you would like to sit in (Price depends on Row) ";
	cin >> userRow;
	cout << "Please choose the Seat you would like to sit in: ";
	cin >> userSeat;
	if (seats[userRow][userSeat] == '*')
	{
		seats[userRow][userSeat] = '#';
	}
	else
		cout << "Sorry, that seat is already taken. Would you like to select another seat?" << endl;
// This is where the loop begins to repeat.
	cout << "Seats:";
	for (int c = 0; c < COL; c++)
	{
		cout << setw(4) << c;
	}
	cout << endl;
	for (int row = 0; row < ROW; row++)
	{
		cout << "Row " << setw(2) << row;
		for (int col = 0; col < COL; col++)
		{
			cout << setw(4) << seats[row][col];
		}
		cout << endl;
	}

}
Hi,
> Is there a way to do this so that I can create a separate function that can access the 2D array

Well, consider if ROWS and COLUMNS are const, you can do this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void BookSeat(int (& seats)[ROWS][COLUMNS])
{
cout << "Please choose the Row you would like to sit in (Price depends on Row) ";
	cin >> userRow;
	cout << "Please choose the Seat you would like to sit in: ";
	cin >> userSeat;
	if (seats[userRow][userSeat] == '*')
	{
		seats[userRow][userSeat] = '#';
	}
	else
	{
	cout << "Sorry, that seat is already taken. Would you like to select another seat? (Y) : " << endl;

  	string YesNo; cin >> YesNo;
  	if(toupper(YesNo[0]) == 'Y') return BookSeat(seats);
	}
}


And in your forementioned function, call it :
BookSeat(seats);
Last edited on
Does that help you? :)
So by using a pass by reference as a parameter, it is changing the original value of the seats[ROW][COLUMNS]? Even though the seats 2D array was initialized within the function seatingCharts().

Also if it is a void type function, wouldn't it produce and error if I try to return a value?
If so, would it be ok to change the function from void to char?
Last edited on
> Would it be ok to change the function from void to char?
You can freely customize your function the way you want you know :)
@closed account 5a8Ym39o6

An array cannot be referenced like that. Remove & from

void BookSeat(int& seats[ROWS][COLUMNS]) // Note: no &


So by using a pass by reference as a parameter, it is changing the original value of the seats[ROW][COLUMNS]?
What do you mean? If you pass an array you can modify the content of the array unless you pass it as const
closed account (48T7M4Gy)
http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function
Check out: "There are three ways to pass a 2D array to a function"
@coder777
You are right. I acknowledged it.
However, the OP might not want to create a whole new copy of the array when a function is called, the OP wants to modify it and the OP doesn't want his original array to remain unchanged after calling a function. And thus the reference sign (&) cannot be removed.

Well, however, the correct function syntax for BookSeat() is :
void BookSeat(int (& seats)[ROWS][COLUMNS])

Despite all this, we haven't heard anything negative from the OP. Has the OP really tried this?
Last edited on
closed account 5a8Ym39o6 wrote:
However, the OP might not want to create a whole new copy of the array when a function is called
The array will not be copied. The pointer to the first element will be passed. Everything within the square brackets will solely be used to determine the layout of the array.
@coder777
> The array will not be copied. The pointer to the first element will be passed.

I got it. Thanks! :)
So here is what I have as of now. And I am getting an error when I try to call my function in main using BookSeat(seats);
In my header the prototype is declared as: void BookSeat(int);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

void BookSeat(int (&seats)[ROW][COL])
{
	
	int userRow, userSeat = 0;
	char YesNo;
	do
	{
	cout << "Please choose the Row you would like to sit in (Price depends on Row) ";
	cin >> userRow;
	cout << "Please choose the Seat you would like to sit in: ";
	cin >> userSeat;
	if (!seats[userRow][userSeat] == '#')
	{
		seats[userRow][userSeat] = '#';
	}
	else
	{
		cout << "Sorry, that seat is already taken. Would you like to select another seat? (Y/N) : " << endl;
		cin >> YesNo;
	}if (toupper(YesNo) == 'Y')
		return BookSeat(seats);
}
> In my header the prototype is declared as: void BookSeat(int);

It is void BookSeat(int (&seats)[ROWS][COLS]);
Also in your new BookSeat() :
1
2
do	{
	cout << "Please choose the Row you would like to sit in (Price depends on Row) ";


Why did you modify the code this way? You have not tested my function before have you?

Well, if this function causes trouble to the compiler, you know what you have to do.
Last edited on
So I modified the code significantly, and now it runs pretty well.

@closed account 5a8Ym39o6. I could not figure out why, but the code would not produce anything whenever I tried to run it. The header file would continue to give me an error every time I tried to compile.

Here is what I have now:
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
void BookSeat(char seats[ROW][COL], int &numberTickets, double &totalPrice)
{
	int userRow, userSeat = 0;
	char YesNo;
	 double rowPrice[] = {50.00, 45.00, 40.00, 35.00, 30.00,
	           25.00, 20.00, 15.00, 12.00, 10.00,
                   8.00, 7.50, 7.00, 6.50, 5.00 };
	do
	{
	cout << "Please choose the Row you would like to sit in (Price depends on Row) ";
	cin >> userRow;
	cout << "Please choose the Seat you would like to sit in: ";
	cin >> userSeat;
	if (seats[userRow][userSeat] == '*')
	{
		seats[userRow][userSeat] = '#';
		numberTickets++;
		totalPrice = rowPrice[userRow] * numberTickets;
	}
	else
	{
		cout << "Sorry, that seat is already taken. Would you like to select another seat (Y/N): " << endl;
		cin >> YesNo;
	}
	
	} while (YesNo == 'Y');
}


Thank you all again for giving me a direction. I truly appreciate all the suggestions.
Last edited on
So what is the complier error the compiler gives you?
This is the error I am receiving.

"IntelliSense: a reference of type "int (&)[15][20]" (not const-qualified) cannot be initialized with a value of type "char [15][20]" g:\Project3\Project3\main4.cpp "
closed account (48T7M4Gy)
seats is an array of int's or an array of char's ? You must ensure compatibility throughout your program. It can't be both.
Last edited on
I did get it as a char array throughout. The program runs fine now.

Thanks all for the help
Glad it helped :)
closed account (48T7M4Gy)
So am I
Topic archived. No new replies allowed.