Theater Seating 2D Array passing through function

May 11, 2013 at 3:58am
I'm supposed to write a program that displays a theater seating, ask the user to give each row a price, then ask if they want to purchase a ticket. their response has to be saved into a 2d array and change the EMPTY seat to a TAKEN seat. The last past is where I'm having trouble. Any suggestions? I'm new to c++ so I'm having a really hard time figuring things out. any little advice will help.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# include <iostream>
# include <iomanip>
using namespace std;

void seats( double [] , int);
void mapSeats();
char movieMenu(char);

int main()
{
	const int rowNum = (15.0);
	double rowValue[rowNum]; //array to hold row pices
	char selection;
	int row2, col2;
	const char TAKEN = '#';//seats taken
	const char EMPTY = '*';//seats free
	const int row = 15;//number of rows
	const int col = 20;//number of col
	char map[row][col];//array to hold seat chart 


	
	for(int i= 0;i<row;i++)//initiating array 
	{
		for (int j=0;j<col;j++)
		{
			map[i][j]=EMPTY;
		}
	}

	mapSeats();

	seats(rowValue, rowNum);//ask user to enter price of each row
	cout << endl;
	
	do
		{
			cout << "MOVIE THEATER MENU" << endl;
			cout << "------------------" << endl;
			cout << "1) Sell a ticket" << endl;
			cout << "Q) Quit program" << endl;
			cout << "Please make a selection: ";
			cin >> selection;
			
			if(selection =='1')
			{
				cout << "Please enter a row number and a seat number for the ticket: " ;
				cout << "Row # :" ;
				cin >> row2;
				cout << endl;
				cout << "Seat # :" ;
				cin >> col2;
				cout << endl;

				for(int i = 0; i < row; i++)
				{
					for(int j=0;j<col;j++)
					{
						map[row2][col2]=TAKEN;
					}
				}

			}
			else if(selection =='q'||selection=='Q')
			{
				cout << "Thank you for using the program." << endl;
			}
			else if(selection != '1' || selection !='q' || selection !='Q')
			{
				cout << "Invalid selection." << endl;
			}
		}while(selection != '1' || selection !='q' || selection !='Q');
	

	system("PAUSE");
	return 0;
}


void seats(double rowPrice[], int row)
{

	cout << "Please enter a ticket price for each row." << endl;
	
	for(int i = 0 ; i < row; i++)
	{
		cout << "Row # " << i+1 << ": " ;
		cin >> rowPrice[i];	
	}
}

void mapSeats()
{
	const char TAKEN = '#';//seats taken
	const char EMPTY = '*';//seats free
	const int rw=20;
	const int cl=15;

	cout << "Seats " ;
	for(int k = 0 ; k <20;k++) //loop to display nums 0 to 19
	{
		cout << fixed<< setw(2) << " " << k ;
	}

	for(int i=0;i<rw;i++)//making array display what's in it
	{
		cout << endl<< "Row " << i;
		for(int j=0;j<cl;j++)
		{
			cout << fixed<< setw(2) << "" << EMPTY;
		}
	}
	cout << endl;
}
May 11, 2013 at 10:43am
lines 55-61
1
2
3
4
5
6
7
for(int i = 0; i < row; i++)
{
	for(int j=0;j<col;j++)
	{
		map[row2][col2]=TAKEN;
	}
}

Why are you updating the same spot map[row2][col2] inside this loop? You should only do it once.
Try changing the code above to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Check if seat is free
if(map[row2][col2] == TAKEN)
{
	cout << "This seat is taken! Try another one. \n";
	continue; // start the loop again
}
else // and if it is - sell the ticket
	map[row2][col2]=TAKEN;
// Add the next loop to immediately see the effects:
for (int i = 0; i < row; i++){
	for(int j = 0; j < col; j++){
		cout << map[i][j];
	}
	cout << endl;
}
Last edited on May 11, 2013 at 10:44am
May 12, 2013 at 12:34am
Thank you SOOO much for your help! I was able to get the program to work smoothly.
Topic archived. No new replies allowed.