Hi, I'm supposed to create a program where I want to purchase tickets to a theater based on rows and columns of the seats. The program uses two-dimensional arrays and constructors, and is supposed to display somethine like this:

"ASU Gammage Theater

1. View available seats
2. Request tickets
3. Display theater sales report.
4. Exit the Program
"
If I input "1", it'll give me a diagram of the seats available (available is #, unavailable is *).

If I input "2", then it'll give me the following options:

"Number of seats desired (1-30):
Desired row (1-15):
Desired starting seat number in the row (1-30):"

It'll then calculate the total ticket prices based on what row you chose, determine whether those seats are available or not, and then asks you what payment you want to make. It calculates the change that's left after you make the payment.

If I input "3", it'll give me how many seats are available and how many are sold, as well as total revenue.

Here is the code for the main program:

// BEGINNING OF MAIN PROGRAM
#include<iostream>
#include<iomanip>
#include"TicketManager.h"

using namespace std; 

int main()
{
	TicketManager ticketsGammage;
	// Displays Menu
	int choice;
	do 
	{
		cout << "ASU Gammage Theater " << endl;
		cout << endl;
		cout << "1. View Available Seats " << endl;
		cout << "2. Request Tickets " << endl;
		cout << "3. Display Theater Sales Report " << endl;
		cout << "4. Exit the Program " << endl;
		cin >> choice; 
		
		// Displays what seats are available (in rows and columns)
		if (choice == 1)
		{
			ticketsGammage.displaySeats();
		}

		// Requests the number of seats desired, the desired row, and the desired seat number,
		// and returns it to the constructors.
		else if (choice == 2)
		{
			int tickets, row, startSeat; 
			cout << "Number of seats desired (1-30): ";
			cin >> tickets;
			cout << endl;
			cout << "Desired row (1-15): ";
			cin >> row; 
			cout << endl;
			cout << "Desired starting seat number in the row (1-30): "; 
			cin >> startSeat; 
			ticketsGammage.requestTickets(tickets, row, startSeat);
		}

		// Displays how many seats are available.
		else if (choice == 3)
		{
			ticketsGammage.displaySalesReport();
		}

		// If user wishes to exit program
		else if (choice == 4)
		{
			
		}
		
		// If the user does not input a valid 
		else if (choice != 4)
		{
			return 0;
		}
	} while (choice != 4);
		return 0;
}

Here is the code for the headers:

using namespace std; 

class TicketManager
//BEGINNING OF HEADER FILE
{
	// Private variables
private:
	static const int NUMROWS = 15; 
	static const int NUMCOLUMNS = 30; 
	char seats[NUMROWS][NUMCOLUMNS];
	double price[NUMROWS];
	int seatsSold;
	double totalRevenue; 

	// Public variables
public:
	TicketManager();
	bool requestTickets(int seatNum, int requestedRow, int startingSeatNum);
	void purchaseTickets(int seatNum, int requestedRow, int startingSeatNum);
	double getTotalRevenue();
	int getSeatsSold();
	double getPrice(int row);
	char getSeat(int row, int column);
	void printTickets(int seatNum, int requestedRow, int startingSeatNum);
	void displaySeats();
	void displaySalesReport();
	double amountPaid;
	int numTickets;
	
};

Here is the .cpp file with all of the constructors:

// BEGINNING OF TICKETMANAGER.CPP

#include<iostream>
#include<iomanip>
#include"TicketManager.h"
#include<fstream>
using namespace std; 


// The Ticket manager constructor
TicketManager::TicketManager()
{
	seatsSold = 0; totalRevenue = 0; 
	ifstream availFile, priceFile;
	availFile.open("seatAvailability.txt");
	priceFile.open("seatPrices.txt");
	double rowPrice;

	for(int rows = 0; rows <= NUMROWS; rows++)
	{
		priceFile >> rowPrice;
		for(int col = 0; col <= NUMCOLUMNS; col++)
		{
			availFile >> seats[rows][col];
			price[rows] = rowPrice;
		}
	}
	availFile.close();
	priceFile.close();
};

// Says which seats are available for purchase and the total purchase price.
bool TicketManager::requestTickets(int seatNum, int requestedRow, int startingSeatNum)
{
	bool avail;
	char wantPurchase;
	int numTickets = seatNum; 
	int dif= (startingSeatNum+seatNum);

	for(seatNum; dif > startingSeatNum; startingSeatNum++)
	{
		if(seats[requestedRow][startingSeatNum] == '#')
		{
			avail = true; 
			cout << "The seats you have requested are available for purchase." << endl;
			cout << "The total purchase price is " << numTickets << " X" << "$" << getPrice(requestedRow) << "= " << "$" << ( (numTickets) * getPrice(requestedRow))<< endl;
			cout << "Do you wish to purchase these tickets (Y/N)?";
			cin >> wantPurchase; 
			if(wantPurchase == 'y' || wantPurchase == 'Y')
			{
				purchaseTickets(numTickets, requestedRow, startingSeatNum);
			}
			return avail;
		}
		else 
		{
			avail = false; 
			cout << "The seats you have requested are not available for purchase." << endl;
			return avail;
		}
	}
};
/*
void ticketconfirmation(bool request)
{

	if request = 'true';
	{
		purchaseTickets (numTickets, requestedRow, startingSeatNum);
	}
	else
	{
	cout << "The seats you have requested are not available for purchase." << endl;
	}
}
*/

// Displays and calculates the number of seats and the payment the user wishes to pay.
void TicketManager::purchaseTickets(int seatNum, int requestedRow, int startingSeatNum)
{
	double totalRevenue = 0; 
	int seatsSold = 0;
	int numTickets = seatNum; 
	//char wantPurchase; 
	double totalTicketPrice = (price[requestedRow]*(seatNum));

	for(seatNum; seatNum > startingSeatNum; seatNum--)
	{
		seats[requestedRow][seatNum] = '*';
	}
	cout << fixed << showpoint << setprecision(2);
	cout << "Num Seats: " << numTickets << endl;
	cout << "The price for the requested tickets is $" << (numTickets) * getPrice(requestedRow)<< endl; 
	cout << "Please input amount paid: "; 
	cin >> amountPaid; 
	printTickets(seatNum, requestedRow, startingSeatNum);

	/*
	seatsSold = seatsSold + (seatNum - startingSeatNum) +1; 
	totalRevenue = totalRevenue + totalTicketPrice; 
	*/
}

// Determines the total revenue.
double TicketManager::getTotalRevenue()
{
	for(int i = 0; i < NUMROWS; i++)
	{
		for(int j = 0; j < NUMCOLUMNS; j++)
		{
			if(seats[i][j] == '*')
			{
			totalRevenue += price[i];
			}
		}
	}
	return totalRevenue;
	
}

int TicketManager::getSeatsSold()
{ int seatsSold = 0;
	for(int i = 0; i < NUMROWS; i++)
	{
		for(int j = 0; j < NUMROWS; j++)
		{
			if(seats[i][j] == '*')
			{
				seatsSold ++;
			}
		}
	}
	return seatsSold;
 
}

// Determines the ticket prices based on the row the user picked.
double TicketManager::getPrice(int row)
{
	return price[row-1];


	};

// Determines whether the seat is available based on rows and columns.
char TicketManager::getSeat(int row, int column)
{
	if( seats[row][column] == '*' || seats[row][column] == '#')
		return seats[row][column];
}

// Calculates total ticket prices and the change due, and displays tickets purchased, payment the user decides to make,
// change due, and total ticket prices.
void TicketManager::printTickets(int seatNum, int requestedRow, int startingSeatNum)
{
	double totalTicketPrice, numTickets;
	numTickets = seatNum;
	totalTicketPrice = numTickets * price[requestedRow];
	for(seatNum; seatNum >= startingSeatNum; seatNum--)
	{
		cout << "*************************************************************" << endl;
		cout << "* Gammage Theater *" << endl;
		cout << "Row " << requestedRow << " " << "Seat " << (seatNum- startingSeatNum +1) << endl;
		cout << "*Price: $ " << getPrice(requestedRow) << endl;
		cout << "*************************************************************" << endl;
	}
	
	cout << "Tickets purchased	: " << numTickets<< endl;
	cout << "Payment			: $ " << amountPaid << endl;
	cout << "Total ticket price : $ " << totalTicketPrice << endl;
	cout << "Change due			: $ " << (amountPaid - totalTicketPrice) << endl;

};

// Displays which seats have been sold and which are available.
void TicketManager::displaySeats()
{
	cout << "       "; 
	cout << "123456789012345678901234567890" << endl;
	for(int i = 0; i <= NUMROWS; i++)
	{	cout << endl;
		cout << "Row " << i;
		cout << setw(2) << right;
		for(int j = 0; j <= NUMCOLUMNS; j++)
		{
			seats[i][j] = '#'; 
			cout << seats[i][j];
		}
		
	}
	cout << endl;
	cout << " Legend: * = Sold " << endl;
	cout << "         # = Available " << endl;


};

// Displays seats sold, seats available, and the total revenue to date.
void TicketManager::displaySalesReport()
{
	int allSeats= 450;
	cout << "Gammage Sales Report " << endl;
	cout << endl;
	cout << "Seats sold: " << getSeatsSold() << endl;
	cout << "Seats available: " << (allSeats - getSeatsSold()) << endl;
	cout << "Total revenue to date: " << getTotalRevenue(); 
};

Here is the .txt files (2 of them)

seatAvailability.txt

##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################

and seatPrices.txt

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

Now, all of the code seems written correctly, but I when I input '2' to request tickets, and then input the number of seats desired, the row desired and the desired seating number, my program keeps going back to the menu. After fiddling with this for around 6 hours, I'm still at an impasse... my instincts are that the problem lies within the Assignmen8.cpp file, where somehow some number keeps screwing with the program, or it's not returning the correct boolean value for the requestTickets constructor.

Any suggestions? Thanks :)

Recommended Answers

All 2 Replies

Learn to use your compiler's debugger and you will find the problem a lot quicker than 6 hours! You should probably be able to find it in just a few minutes. Using the debugger you can execute the program one line at a time so that you can see exactly the order of execution and the value of all variables. It might take a while to learn the debugger but it's well worth the effort in the long run.

And if you don't know how to use the debugger, don't want to know how to use the debugger, or just prefer to do it yourself, then you can always through in debugging code that will be removed in the release version. If you don't see any output at all from TicketManager::requestTickets() which is where you should go after receiving user input for opotion 2, then that seems like a pretty good place to start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.