Write a program that can be used by a small theater to sell tickets for performa
ID: 3695488 • Letter: W
Question
Write a program that can be used by a small theater to sell tickets for performances. The theater’s auditorium has 15 rows of seats, with 30 seats in each row. The program should display a screen that shows which seats are available and which are taken. For example, the following screen shows a chart depicting each seat in the theater. Seats that are taken are represented by an * symbol, and seats that are available are represented by a # symbol:
Seats 123456789012345678901234567890
Row 1 ***###***###*########*****#### Row 2 ####*************####*******## Row 3 **###**********########****### Row 4 **######**************##****** Row 5 ********#####*********######## Row 6 ##############************#### Row 7 #######************########### Row 8 ************##****############ Row 9 #########*****############**** Row 10 #####*************############ Row 11 #**********#################** Row 12 #############********########* Row 13 ###***********########**###### Row 14 ############################## Row 15 ############################## Here is a list of task this program must perform: • When the program begins, it should ask the user to enter the seat prices for each row. The prices can be stored in a separate array. (Alternatively, the prices may be read from a file.) • Once the prices are entered, the program should display a seating chart similar to the one shown above. The user may enter the row and seat numbers for tickets being sold. Every time a ticket or group of tickets is purchased, the program should display the total ticket prices and update the seating chart. • The program should keep a total of all ticket sales. The user should be given an option of viewing this amount. • The program should also give the user an option to see a list of how many seats have been sold, how many seats are available in each row, and how many seats are available in the entire auditorium. Input Validation: When tickets are being sold, do not accept row or seat numbers that do not exist. When someone requests a particular seat, the program should make sure that seat is available before it is sold.
Explanation / Answer
main.cpp
#include "theater-program.hpp"
void printTheater (std::string **arr, int numRows, int numSeats) {
std::cout << "Available seats are marked with a '#'. Seats" << std::endl;
std::cout << " 12345678901234567890234567890" << std::endl;
for (int r=0; r<9; r++) {
std::cout << "Row " << r+1 << " ";
for (int c=0; c<numSeats; c++)
std::cout << arr[r][c];
std::cout << " ";
}
for (int r=9; r<numRows; r++) {
std::cout << "Row " << r+1 << " ";
for (int c=0; c<numSeats; c++)
std::cout << arr[r][c];
std::cout << " ";
}
}
bool isValidSeat(std::string **arr, int row, int seat) {
if (row > 15) {
std::cout << "ERROR: Please enter a valid row number." << std::endl;
return false;
}
if (seat >30) {
std::cout << "ERROR: Please enter a valid seat number." << std::endl;
return false;
}
if (arr[row-1][seat-1] == ".") {
std::cout << "That seat is not available." << std::endl;
return false;
}
return true;
}
void updateSeatingChart(std::string **arr, int row, int seat) {
arr[row-1][seat-1] = ".";
}
float *setPriceArray(int numRows) {
std::ifstream priceFile("rowPrices.txt");
float *priceArray = new float [numRows];
if (priceFile.is_open()) {
for (int r=0; r<numRows; r++)
priceFile >> priceArray[r];
}
return priceArray;
}
std::string **theaterSetUp(int numRows, int numSeats) {
std::string **seatingChart = new std::string *[numRows];
for (int r=0; r<numRows; r++)
seatingChart[r] = new std::string[numSeats];
for (int r=0; r<numRows; r++)
for (int c=0; c<numSeats; c++)
seatingChart[r][c] = "#";
return seatingChart;
}
int calculateSeatsSold(std::string **arr, int numRows, int numSeats) {
int seatsSold = 0;
for (int r=0; r<numRows; r++)
for (int c=0; c<numSeats; c++)
if (arr[r][c] == ".")
seatsSold++;
return seatsSold;
}
int calculateAvailableRowSeats(std::string **arr, int row, int numRows, int numSeats) {
int rowSeats = 0;
for (int r=0; r<numRows; r++)
if(r==row)
for (int c=0; c<numSeats; c++)
if (arr[r][c] == "#")
rowSeats++;
return rowSeats;
}
int calculateTotalAvailableSeats(std::string **arr, int numRows, int numSeats) {
int totalAvailableSeats = 0;
for (int r=0; r<numRows; r++)
for (int c=0; c<numSeats; c++)
if(arr[r][c] == "#")
totalAvailableSeats++;
return totalAvailableSeats;
}
void theaterPrompt() {
int numRows = 15; int numSeats = 30;
float *prices = setPriceArray(numRows);
std::string **seatingChart = theaterSetUp(numRows, numSeats);
float totalTicketSales = 0;
std::cout << "Welcome to the Theater!" << std::endl;
printTheater(seatingChart, numRows, numSeats);
std::string option = "y"; // if option == "q", program quits
while (1) {
int row, seat;
std::cout << " Would you like to buy a seat (y/n)? "; std::cin >> option;
if (option == "n") {std::cout << " Ok! Thank you for your purchase. "; break;}
else if (option == "y") {
std::cout << "What is the row and seat number you'd like to buy? " << std::endl;
std::cout << " Row Number: "; std::cin >> row;
std::cout << " Seat Number: "; std::cin >> seat;
if (!isValidSeat(seatingChart, row, seat)) continue; else {
totalTicketSales += prices[row-1];
updateSeatingChart(seatingChart, row, seat);
std::cout << " Ok! You have purchased seat " << seat << " in row " << row << " for $" << prices[row-1] << ". " << std::endl;
printTheater(seatingChart, numRows, numSeats);
}
}
printResults(seatingChart, numRows, numSeats, totalTicketSales);
}
}
void printResults(std::string **arr, int numRows, int numSeats, float totalSales) {
std::ofstream seatingChartFile("finalSeatingChart.txt");
seatingChartFile << " 123456789012345678901234567890" << std::endl;
for (int r=0; r<9; r++) {
seatingChartFile << "Row " << r+1 << " ";
for (int c=0; c<numSeats; c++)
seatingChartFile << arr[r][c];
seatingChartFile << " ";
}
for (int r=9; r<numRows; r++) {
seatingChartFile << "Row " << r+1 << " ";
for (int c=0; c<numSeats; c++)
seatingChartFile << arr[r][c];
seatingChartFile << " ";
}
std::ofstream finalSalesFile("finalSales.txt");
finalSalesFile << "Total Ticket Sales: $" << totalSales << std::endl;
int seatsSold = calculateSeatsSold(arr, numRows, numSeats);
finalSalesFile << "Number of Seats Sold: " << seatsSold << std::endl;
finalSalesFile << "Seats Available By Row: " << std::endl;
for (int r=0; r<numRows; r++) {
int availableRowSeats = calculateAvailableRowSeats(arr, r, numRows, numSeats);
finalSalesFile << " Row " << r+1 << ": " << availableRowSeats << std::endl;
}
int totalAvailableSeats = calculateTotalAvailableSeats(arr, numRows, numSeats);
finalSalesFile << " Total Number of Seats Available: " << totalAvailableSeats << std::endl;
}
int main() {
theaterPrompt();
}
theater-program.hpp
#ifndef THEATER_PROGRAM_HPP_
#define THEATER_PROGRAM_HPP_
#include <iostream>
#include <fstream>
// Displays the seating chart of the theater
void printTheater (std::string **arr, int numRows, int numSeats);
// Checks if the specified row and seat numbers are valid
bool isValidSeat(std::string **arr, int row, int seat);
// Updates the seating chart after each time
// the user purchases a ticket
void updateSeatingChart(std::string ** arr, int row, int seat);
// Reads the price for a set in each row from a file named
// rowPrices.txt and stores them in an array
float *setPriceArray(int numRows);
// Prompts the user to purchase a seat until
// they want to quit.
void theaterPrompt();
// Sets up the theater. All of the seats are available
std::string **theaterSetUp(int numRows, int numSeats);
// Calculates the number of seats sold
int calculateSeatsSold(std::string **arr, int numRows, int numSeats);
// Calculates the number of seats available in each row
int calculateAvailableRowSeats(std::string **arr, int row, int numRows, int numSeats);
// Calculates the number of seats available in the entire auditorium
int calculateTotalAvailableSeats(std::string **arr, int numRows, int numSeats);
// Writes the seating chart to a file named finalSeatingChart.txt.
// Writes the total ticket sales, seats sold, seats available in
// each row and seats available in the entire auditorium to a file
// named finalSales.txt
void printResults(std::string **arr, int numRows, int numSeats, float totalSales);
#endif /* THEATER_PROGRAM_HPP_ */
rowPrices.txt
3.00
3.00
2.75
2.75
2.50
2.50
2.25
2.25
2.00
2.00
1.75
1.75
1.50
1.50
1.25
sample output
Welcome to the Theater!
Available seats are marked with a '#'.
Seats
12345678901234567890234567890
Row 1 ##############################
Row 2 ##############################
Row 3 ##############################
Row 4 ##############################
Row 5 ##############################
Row 6 ##############################
Row 7 ##############################
Row 8 ##############################
Row 9 ##############################
Row 10 ##############################
Row 11 ##############################
Row 12 ##############################
Row 13 ##############################
Row 14 ##############################
Row 15 ##############################
Would you like to buy a seat (y/n)? y
What is the row and seat number you'd like to buy?
Row Number: 4
Seat Number: 6
Ok! You have purchased seat 6 in row 4 for $2.75.
Available seats are marked with a '#'.
Seats
12345678901234567890234567890
Row 1 ##############################
Row 2 ##############################
Row 3 ##############################
Row 4 #####.########################
Row 5 ##############################
Row 6 ##############################
Row 7 ##############################
Row 8 ##############################
Row 9 ##############################
Row 10 ##############################
Row 11 ##############################
Row 12 ##############################
Row 13 ##############################
Row 14 ##############################
Row 15 ##############################
Would you like to buy a seat (y/n)? n
Ok! Thank you for your purchase.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.