Trouble With Theater Ticket Arrays Program

Hi everyone, I'm having a bit of trouble finishing up this movie theater sales program. I've made it this far and I'm stuck on how to display my updated seat map for the theater once the "user" has sold some seats.

"Seats that are taken should be represented by a # symbol and seats that are available should be represented by a * symbol. The first thing your program should do is initialize all of the seats to available (*) and display the seating chart.(HINT: The seating chart should be a two dimensional array.) Below is an example of the seating chart with all seats initialized to available."

Also, the second thing I'm having trouble with is how to figure out and display the total tickets sold and total revenue from the tickets. I'd greatly appreciate any help at this point.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
using namespace std;

void seatsMap(); //function prototype
void seatCost(double[], int); //function prototype

int main()
{
	const int numRows = 15; //number of rows
	const int numCols = 20; //number of columns 
	const char TAKEN = '#'; //seats taken
	const char EMPTY = '*'; //seats available
	char selection; //user selection 
	char seatingMap[numRows][numCols]; //array that holds seating chart
	int userRow = 0;
	int userCol = 0;
	double rowValues[numRows]; //array that holds the prices of the seats
	//double totalRevenue = 0; 
	//int ticketsSold = 0; 

	for (int i = 0; i < numRows; ++i) //initializes all the seats in the theater to empty
	{
		for (int j = 0; j < numCols; ++j)
		{
			seatingMap[i][j] = EMPTY;
		}
	}

	seatsMap();
	seatCost(rowValues, numRows);
	cout << "\n";

	do
	{
		cout << "MOVIE THEATER MENU\n"
			<< "------------------\n"
			<< "1) Sell a ticket\n"
			<< "Q) Quit program\n"
			<< "Please make a selection : ";
		cin >> selection;

		if (selection == '1')
		{
				cout << "\nPlease enter a row number and seat number for the ticket:\n"
					<< "Row # : ";
				cin >> userRow;

				while (userRow < 0 || userRow > 14) //input validation that ensures user enters row # between 0 & 14
				{
					cout << "Row # must be between 0 and 14. Please enter again: \n";
					cin >> userRow;
				}

				cout << "Seat # : ";
				cin >> userCol;
				cout << "\n";

			if (seatingMap[userRow][userCol] == TAKEN) //in the event that user attempts to sell the same seat twice
			{
				cout << "Sorry. The ticket is not available for this seat.\n\n";
				continue;
			}

			else //"sells" the seat & marks it down as taken
			{
				seatingMap[userRow][userCol] = TAKEN;
			}
		}

		else if (toupper(selection) == 'Q')
		{
			cout << "\nUPDATED SEATING CHART AND SALES INFO\n"
				 << "------------------------------------\n";
			cout << "TOTAL TICKETS SOLD: "; //<< ticketsSold << endl
			cout << "TOTAL REVENUE: "; //<< totalRevenue << endl;
		}
		
		else if (selection != '1' || selection != 'Q')
		{
			cout << "Invalid selection. Please try again.\n\n";
		}
	} while (toupper(selection) != 'Q');
	system("PAUSE");
	return 0;
}

void seatCost(double rowPrice[], int row) //prompts user to enter the ticket price for each row
{
	cout << "\nPlease enter a ticket price for each row.\n";

	for (int i = 0; i < row; ++i)
	{
		cout << "Row #" << i << " : ";
		cin >> rowPrice[i];
	}
}

void seatsMap()
{
	const char TAKEN = '#';
	const char EMPTY = '*';
	const int row = 15;
	const int col = 20;

	/*
		************************************************************************************************************
						There are two for loops to align the spacing for numbers 1-9 and numbers > 9
		************************************************************************************************************
	*/

	cout << "Seats:";
	
	for (int k = 0; k < 10; ++k)
	{
		cout << fixed << setw(2) << " " << k;
	}

	for (int k = 10; k < col; ++k)
	{
		cout << fixed << setw(1) << " " << k;
	}

	/*
		************************************************************************************************************
			There are two for loops to align the "*" for empty seating output for numbers 1-9 and numbers > 9
		************************************************************************************************************
	*/

	for (int i = 0; i < 10; ++i)
	{
		cout << endl << "Row " << i << " ";

		for (int j = 0; j < col; ++j)
		{
			cout << fixed << setw(2) << " " << EMPTY;
		}
	}

	for (int i = 10; i < row; ++i)
	{
		cout << endl << "Row " << i;

		for (int j = 0; j < col; ++j)
		{
			cout << fixed << setw(2) << " " << EMPTY;
		}
	}
	cout << endl;
}
You may want to consider defining your constants as global variables, since you're using them in multiple functions and they don't change.

Additionally, from a user standpoint, no one would enter a seat into a program as the seat number - 1. People don't think in array subscript notation. A better interface design be to require them to enter a valid seat between 1 and the rownum and colnum, and then subtract 1 from that when determining which array subscript to update.

You're over-complicating displaying the seat map. You have an array that has been initialized to all empty seats, and your main function updates the seats to # values when a seat is sold. All you need to do is display the array as it currently exists in row x col format. I'm also not clear on why you're only displaying 10 at a time - just display the 15 x 20 array, unless there's a reason you haven't communicated.

To get total tickets sold and revenue, just use a running total/accumulator. Each time you sell a ticket, increment a counter by 1. The counter at any time will reflect the number of tickets sold, and from the tickets sold you can get the revenue. If you've not used a running total/accumulator before, some basic Google searching will get you up to speed.
Last edited on
Hi mastakhan, thanks for the input!

In regard to using globals for my const variables, I forgot to mention, but our professor has specifically instructed us: "IMPORTANT NOTE: Make sure and use the required functions as indicated in the instructions for this program! NO GLOBALS -no exceptions."

Additionally, from a user standpoint, no one would enter a seat into a program as the seat number - 1. People don't think in array subscript notation. A better interface design be to require them to enter a valid seat between 1 and the rownum and colnum, and then subtract 1 from that when determining which array subscript to update.

I concur with what you're saying, but I'm just trying to follow the required output. I would definitely do things differently if this was a personal program I was creating.

As for displaying the seat map array in two portions, I did so to match the required output on Hypergrade. Without it, my "*" and spacing for number display will not line up.

I'm going to try and work on some changes including what you've mentioned, thank you.


EDIT: I've updated this post with the professor's instructions for further clarification.
Step 1: The program should have a FUNCTION that displays a screen that shows which seats are available and which are taken. Seats that are taken should be represented by a # symbol and seats that are available should be represented by a * symbol. The first thing your program should do is initialize all of the seats to available (*) and display the seating chart. (HINT: The seating chart should be a two dimensional array.)

Step 2: Each row in the auditorium has a different ticket price. So tickets in row 0 may be 5.00 each and tickets in row 1 may be 10.00 each. Your program should have a FUNCTION that asks the user to enter a ticket price for each row. The price of tickets for each row should be stored in a one dimensional array.

Step 3: Your program should have variables tracking the total number of tickets sold and the total revenue for all tickets sold.

Step 4: Your program should allow the user to sell tickets one at a time. The user should be able to sell as many tickets as they would like (you need a loop for this). Do this with some sort of prompt or menu asking the user if they would like to sell another ticket. Don't forget to validate input data if you need to.

To allow the user to sell a ticket your program should have the user enter a row number and a seat number for the ticket they would like to sell. The program should do four things with this information:

It should check to see if the seat is available. If the seat is taken the program should not allow the user to sell the ticket. If this happens, print a message to the user saying the ticket is not available and prompt the user to see if they would like to sell another ticket.
If the seat is available the program should update the seating chart by putting a taken symbol (#) in that seat's position in the chart.

The program should then look up the row price for the seat sold. Your program should have a variable tracking the total revenue, the price of the seat sold should be added to this total after each sale.

Your program should have a variable tracking the total tickets sold. The next thing your program should do when selling a ticket is update the total tickets sold.

Step 5: Once the user is finished selling tickets print out an updated seating chart followed by the total tickets sold and the total revenue generate from those tickets.

NOTE: You are required to use two arrays in this program, one for the seating chart and one to store the prices for each row. You are also required to use two functions: one to display the seating chart and one to read in the price per row data and store it in the array with the prices for each row in it. You may use other functions if you want to but they are not required.
Last edited on
Hello khovsepa,

mastakhan has made some good points. A user will be thinking in terms of 1 - 20 or 1 - 15 for making a seat choice. It is up to the programmer to think in terms the arrays and what the user inputs.

Before I saw the requirements of the program I have been working things differently and need to change some things back.

For displaying the seating map I left what you started and wrote another function to display the "seatsMap" array. Take a look and see what ideas it might give you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void ShowMap(char seatingMap[][numCols])
{
	std::cout << "Seats:";

	for (int lp = 1; lp < 21; lp++)
	{
		std::cout << ((lp > 10) ? static_cast<string>(" ") : static_cast<string> ("  "));  // <--- Revised see edit.
		std::cout << lp;
	}

	std::cout << std::endl;

	for (int lpo = 0; lpo < 15; lpo++)
	{
		std::cout << "Row: " << std::setw(2) << lpo + 1 << " ";

		for (int lpi = 0; lpi < 20; lpi++)
		{
			std::cout << seatingMap[lpo][lpi] << "  ";
		}
		std::cout << std::endl;
	}
	//std::cout << std::endl;  // <--- Used for testing.
}


Hope that helps,

Andy
Edit: I found that this works just as well for line 7 std::cout << ((lp > 10) ? " " : " ") << lp;
Last edited on
Hi Handy Andy,

This is my updated code after working on it for a while.
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <iomanip>
using namespace std;

const int numRows = 15;
const int numSeats = 20;

void displaySeats(char[numRows][numSeats], bool[numRows][numSeats]);
void ticketPrice(double[numRows]);
void rowSeat(bool[numRows][numSeats], int&); 
double calculator(bool[numRows][numSeats], double[numRows]);

int main()
{
	char seats[numRows][numSeats];
	bool availableSeat[numRows][numSeats];
	double ticketsP[numRows];
	char choice;
	int counter = 0;
	double totalAmount = 0.0;

	for (int count = 0; count < numRows; count++)
	{
		for (int count1 = 0; count1 < numSeats; count1++)
		{
			availableSeat[count][count1] = true;
			seats[count][count1] = '*';
		}
	}
	displaySeats(seats, availableSeat);
	ticketPrice(ticketsP);
	do
	{
		cout << "MOVIE THEATER MENU\n"
			<< "------------------\n"
			<< "1) Sell a ticket\nQ) Quit program\n"
			<< "Please make a selection : ";
		cin >> choice;
		if (choice == '1')
		{
			rowSeat(availableSeat, counter);
		}
		else if (toupper(choice) != 'Q')
		{
			cout << "Invalid selection. Please try again.\n\n";
		}
		else
		{
			totalAmount = calculator(availableSeat, ticketsP);
			cout << fixed << setprecision(2)
				<< "\nUPDATED SEATING CHART AND SALES INFO\n"
				<< "------------------------------------\n";
			displaySeats(seats, availableSeat);
			cout << "\nTOTAL TICKETS SOLD: " << counter << "\nTOTAL REVENUE: $"
				<< totalAmount << endl;
		}
	} while (toupper(choice) != 'Q');

	//system("PAUSE");
	//return 0;
}

void displaySeats(char screen[numRows][numSeats], bool available[numRows][numSeats])
{
	cout << "Seats:";

	for (int count = 0; count < numSeats; count++)
	{
		if (count < 10)
		{
			cout << "  " << count;
		}

		else
		{
			cout << " " << count;
		}
	}
	cout << "\n";

	for (int count1 = 0; count1 < numRows; count1++)
	{
		if (count1 < 10)
		{
			cout << "Row " << count1 << " ";
		}

		else
		{
			cout << "Row " << count1;
		}

		for (int count2 = 0; count2 < numSeats; count2++)
		{
			if (available[count1][count2] == true)
			{
				screen[count1][count2] = '*';
				cout << "  " << screen[count1][count2];
			}
			else
			{
				screen[count1][count2] = '#';
				cout << "  " << screen[count1][count2];
			}
		}

		cout << "\n";
	}
}

void ticketPrice(double prices[numRows]) // function prompts user to enter a ticket price for each row 
{
	cout << "\nPlease enter a ticket price for each row.\n";

	for (int i = 0; i < numRows; ++i)
	{
		cout << "Row #" << i << " : ";
		cin >> prices[i];
	}
	cout << "\n";
}

void rowSeat(bool position[numRows][numSeats], int &counter)
{
	int row;
	int seat;
	cout << "\nPlease enter a row number and seat number for the ticket:\nRow # : ";
	cin >> row;
	if (row < 0 || row > 14)
	{
		do
		{
			cout << "Row # must be between 0 and 14. Please enter again: \n";
			cin >> row;
		} while (row < 0 || row > 14);
	}
	cout << "Seat # : ";
	cin >> seat;
	cout << endl;
	if (seat < 0 || seat > 19)
	{
		do
		{
			cout << "Seat # must be between 0 and 19. Please enter again: \n";
			cin >> seat;
		} while (seat < 0 || seat > 19);
	}
	if (position[row][seat] != true)
	{
		cout << "Sorry. The ticket is not available for this seat.\n\n";
	}
	else
	{
		position[row][seat] = false;
		counter++;
	}
}

double calculator(bool array[numRows][numSeats], double prices[numRows])
{
	double totalAmt = 0.0;
	for (int count = 0; count < numRows; count++)
	{
		for (int count1 = 0; count1 < numSeats; count1++)
		{
			if (array[count][count1] != true)
			{
				totalAmt += prices[count];
			}
		}
	}
	return totalAmt;
}

Hello khovsepa,

I hope I did not mislead you into completely changing your program. This new version may work, but I have not tested it yet. One of the first things I noticed is lines 5 and 6 are two global variables that you are not allowed to use. The second thing I noticed is that you have made it more complicated than it need to be.

There is very little wrong with your original code. I added two variables "seatCount" and "totalRevune" along with a bool and char variables I used in a do/while loop in "if (selection == '1')" in main. In the last if/else in the else part I dealt with adding to "totalRevnue" and "seatCount". The last part of the do/while loop prompt the user to continue entering seat choices or exit.

Two reasons I added the "ShowMap" function I wrote was 1. to preserve what you have and 2. to have a more generic function without rewriting your original code.

I will have to work on your new code tomorrow morning and see how it works.

Hope that helps,

Andy

Topic archived. No new replies allowed.