I\'m supposed to write a C++ program that manages theater tickets. If the reques
ID: 3581512 • Letter: I
Question
I'm supposed to write a C++ program that manages theater tickets. If the requested seats don't exist or aren't available, it should display error and ask if user wants to return to main menu. Now there are a few issues:
1. It doesn't display the error for all situations that some or all seats don't exist or are unavailable
2. Sometimes when the seat(s) don't exist or are unavailable, it displays the seat info (number of requested seats, price per seat, total cost)
3. It always calls the PurchaseTicket function. It should skip that if the seats don't exist or unavailable
4. When the seats do exist and are available, sometimes the program crashes after printing the tickets
Here is my code:
#include
#include
#include
#include
#include
using namespace std;
int seatsRequested,
rowRequested,
seatNumber;
double cost;
class TicketManager
{
private:
ifstream SeatAvailability,
SeatPrices;
ofstream SeatOutput;
static const int NUM_ROWS = 15;
static const int NUM_SEATS = 30;
char SeatStructures[NUM_ROWS][NUM_SEATS];
int seatsSold = 0,
seatsTotal = 450;
double price[NUM_ROWS],
totalRevenue = 0.0;
string out = "";
public:
TicketManager();
~TicketManager();
string TicketRequest(int, int, int);
string PurchaseTicket(int, int, double);
string PrintTicket(int, int, double);
};
TicketManager::TicketManager() //Constructor - read in data from data files and set up the array
{
SeatAvailability.open("SeatAvailability.dat"); // file indicates which seats have been sold
if (!SeatAvailability)
cout << "Error opening SeatAvailability data file. ";
else
{
for (int rows = 0; rows < NUM_ROWS; rows++)
{
SeatAvailability >> SeatStructures[rows];
}
}
SeatAvailability.close();
SeatPrices.open("SeatPrices.dat");
if (!SeatPrices)
cout << "Error opening SeatPrices data file. ";
else
{
for (int rows = 0; rows < NUM_ROWS; rows++)
{
SeatPrices >> price[rows];
cout << fixed << showpoint << setprecision(2);
}
cout << endl << endl;
}
SeatPrices.close();
}
string TicketManager::TicketRequest(int seatsRequested, int rowRequested, int seatNumber)
{
string out = "";
if (seatsRequested < 1 || seatsRequested > 30 || rowRequested < 1 || rowRequested > 15
|| seatNumber < 1 || seatNumber > 30)
{
out = " Invalid entry. The seats requested do not exist. ";
}
for (int count = 0; count < seatsRequested; count++)
{
seatNumber++;
if (SeatStructures[rowRequested - 1][seatNumber - 1] == '#')
{
cost = seatsRequested * price[rowRequested - 1];
out = " Requested seats: " + to_string(seatsRequested) +
" Price per seat: $" + to_string(price[rowRequested - 1]) +
" Total cost: $" + to_string(cost);
}
else
{
out = " Sorry, your requested seat(s) is unavailable. ";
}
}
return out;
}
string TicketManager::PurchaseTicket(int row, int seat, double cost)
{
double payment;
cout << "Please submit your payment below: " << endl;
cin >> payment;
if (payment < cost)
return string("Sorry, your payment amount is insufficient. ");
else if (payment >= cost)
{
cout << " Thank you for your payment. ";
seatsSold = +seatsRequested;
totalRevenue += cost;
for (int ticketsPrinted = 0; ticketsPrinted < seatsRequested; ticketsPrinted++)
{
cout << PrintTicket(row, seat, cost);
seat++;
}
if (payment > cost)
{
return string("Change due: $") + to_string(payment - cost);
}
}
}
string TicketManager::PrintTicket(int row, int seat, double cost)
{
return string(" **************************************** Theater Ticket Row: ") +
to_string(row) +
string(" Seat: ") +
to_string(seat) +
string(" Price: $") +
to_string(price[rowRequested - 1]) +
string(" **************************************** ");
}
TicketManager::~TicketManager()
{
SeatOutput.open("seatmap.txt");
for (int rows = 0; rows < NUM_ROWS; rows++)
{
if (rows != 0)
{
SeatOutput << endl;
}
for (int seats = 0; seats < NUM_SEATS; seats++)
SeatOutput << SeatStructures[rows][seats];
}
SeatOutput.close();
}
int main()
{
TicketManager TicketManager1;
char choice,
confirmation,
repeat;
do
{
cout << " Please choose from the following menu options and enter the number you wish to select: ";
cout << "1. View Available Seats ";
cout << "2. Request Tickets ";
cout << "3. Display Theater Sales Report ";
cout << "4. Exit the Program ";
cin >> choice;
switch (choice)
{
case '2': cout << "How many seats would you like to purchase (1 - 30)? ";
cin >> seatsRequested;
cout << "In which row would you like to find seats(1 - 15)? ";
cin >> rowRequested;
cout << "What is your desired starting seat number in the row (1 - 30)? ";
cin >> seatNumber;
cout << TicketManager1.TicketRequest(seatsRequested, rowRequested, seatNumber);
cout << " Do you wish to purchase these seats? (Y/N)";
cin >> confirmation;
if (toupper(confirmation) == 'Y')
{
cout << TicketManager1.PurchaseTicket(rowRequested, seatNumber, cost);
}
break;
default: cout << "Invalid menu option. ";
break;
}
cout << " Do you want to return to the main menu? (Y/N)" << endl;
cin >> repeat;
} while (toupper(repeat) == 'Y');
cout << endl << endl;
return 0;
}
Data from SeatAvailability Text File:
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
Data from SeatPrices text file:
12.50
12.50
12.50
12.50
10.00
10.00
10.00
10.00
8.00
8.00
8.00
8.00
5.00
5.00
5.00
Explanation / Answer
/*There were quite changes in the file , I made come changes , check my comments by //added by chegg EA..belowe is the code*/
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
//#include
//#include
using namespace std;
int seatsRequested,
rowRequested,
seatNumber;
double cost;
class TicketManager
{
private:
ifstream SeatAvailability,
SeatPrices;
ofstream SeatOutput;
static const int NUM_ROWS = 15;
static const int NUM_SEATS = 30;
char SeatStructures[NUM_ROWS][NUM_SEATS];
int seatsSold = 0,
seatsTotal = 450;
double price[NUM_ROWS],
totalRevenue = 0.0;
string out = "";
public:
TicketManager();
~TicketManager();
string TicketRequest(int, int, int);
string PurchaseTicket(int, int, double);
string PrintTicket(int, int, double);
//method to view seats available. Added by chegg EA
void view();
void sales_report();
};
//method to view seats available. Added by chegg EA
void TicketManager::view()
{
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_SEATS; j++)
{
cout <<SeatStructures[i][j];
}
cout << endl;
}
}
//method to print sales_report
void TicketManager::sales_report()
{
cout << "Seats sold = " << seatsSold << " and total revenue = " << totalRevenue << endl;
}
TicketManager::TicketManager() //Constructor - read in data from data files and set up the array
{
SeatAvailability.open("SeatAvailability.dat"); // file indicates which seats have been sold
if (!SeatAvailability)
cout << "Error opening SeatAvailability data file. ";
else
{
for (int rows = 0; rows < NUM_ROWS; rows++)
{
SeatAvailability >> SeatStructures[rows];
}
}
SeatAvailability.close();
SeatPrices.open("SeatPrices.dat");
if (!SeatPrices)
cout << "Error opening SeatPrices data file. ";
else
{
for (int rows = 0; rows < NUM_ROWS; rows++)
{
SeatPrices >> price[rows];
cout << fixed << showpoint << setprecision(2);
}
cout << endl << endl;
}
SeatPrices.close();
}
string TicketManager::TicketRequest(int seatsRequested, int rowRequested, int seatNumber)
{
string out = "";
if (seatsRequested < 1 || seatsRequested > 30 || rowRequested < 1 || rowRequested > 15
|| seatNumber < 1 || seatNumber > 30)
{
out = " Invalid entry. The seats requested do not exist. ";
}
//added by chegg
if (SeatStructures[rowRequested - 1][seatNumber - 1] == '*')
{
out = " Sorry, your requested seat(s) is unavailable. ";
//added by Chegg
return "";
}
for (int count = 0; count < seatsRequested; count++)
{
seatNumber++;
if (SeatStructures[rowRequested - 1][seatNumber - 1] == '#')
{
cost = seatsRequested * price[rowRequested - 1];
out = " Requested seats: " + to_string(seatsRequested) +
" Price per seat: $" + to_string(price[rowRequested - 1]) +
" Total cost: $" + to_string(cost);
}
else
{
out = " Sorry, your requested seat(s) is unavailable. ";
//added by Chegg
return "";
}
}
return out;
}
string TicketManager::PurchaseTicket(int row, int seat, double cost)
{
double payment;
cout << "Please submit your payment below: " << endl;
cin >> payment;
if (payment < cost)
return string("Sorry, your payment amount is insufficient. ");
else if (payment >= cost)
{
cout << " Thank you for your payment. ";
//added by chech EA to change ticket available # to *
SeatStructures[row-1 ][seat-1 ] = '*';
seatsSold = +seatsRequested;
totalRevenue += cost;
for (int ticketsPrinted = 0; ticketsPrinted < seatsRequested; ticketsPrinted++)
{
cout << PrintTicket(row, seat, cost);
seat++;
}
if (payment > cost)
{
return string("Change due: $") + to_string(payment - cost);
}
//added by Chegg EA
else
return "";
}
}
string TicketManager::PrintTicket(int row, int seat, double cost)
{
return string(" **************************************** Theater Ticket Row: ") +
to_string(row) +
string(" Seat: ") +
to_string(seat) +
string(" Price: $") +
to_string(price[rowRequested - 1]) +
string(" **************************************** ");
}
TicketManager::~TicketManager()
{
SeatOutput.open("seatmap.txt");
for (int rows = 0; rows < NUM_ROWS; rows++)
{
if (rows != 0)
{
SeatOutput << endl;
}
for (int seats = 0; seats < NUM_SEATS; seats++)
SeatOutput << SeatStructures[rows][seats];
}
SeatOutput.close();
}
int main()
{
TicketManager TicketManager1;
//added by chegg EA
string retStr;
char choice,
confirmation,
repeat;
do
{
cout << " Please choose from the following menu options and enter the number you wish to select: ";
cout << "1. View Available Seats ";
cout << "2. Request Tickets ";
cout << "3. Display Theater Sales Report ";
cout << "4. Exit the Program ";
cin >> choice;
switch (choice)
{
case '1':
//view available seats
TicketManager1.view();
break;
case '2': cout << "How many seats would you like to purchase (1 - 30)? ";
cin >> seatsRequested;
cout << "In which row would you like to find seats(1 - 15)? ";
cin >> rowRequested;
cout << "What is your desired starting seat number in the row (1 - 30)? ";
cin >> seatNumber;
//added by Chegg EA
retStr = TicketManager1.TicketRequest(seatsRequested, rowRequested, seatNumber);
if (!retStr.empty())
cout << retStr << endl;
else
{
cout << " Sorry, your requested seat(s) is unavailable. ";
continue;
}
//cout << TicketManager1.TicketRequest(seatsRequested, rowRequested, seatNumber);
cout << " Do you wish to purchase these seats? (Y/N)";
cin >> confirmation;
if (toupper(confirmation) == 'Y')
{
cout << TicketManager1.PurchaseTicket(rowRequested, seatNumber, cost);
}
break;
case '3':
//added by Chegg EA
TicketManager1.sales_report();
break;
case '4':
//added chegg EA
cout << "Exiting the program" << endl;
exit(0);
break;
default: cout << "Invalid menu option. ";
break;
}
cout << " Do you want to return to the main menu? (Y/N)" << endl;
cin >> repeat;
} while (toupper(repeat) == 'Y');
cout << endl << endl;
return 0;
}