|
// include directives- we have a file that we read in from, so we need fstream #include <iostream> #include <fstream> #include <cstdlib> using namespace std;
// define some global constants to simplify the program; these will be used in the seating // chart array and loops const char FULL = '*'; const char OPEN = '#'; const int ROWS = 15; const int SEATS = 30;
// class definition- need private variables to store changing data and public member // functions to manipulate and display data- simplifies program class Theater { public: void buytickets(); void todayprofit(); void todaysales(); void rowshow(); void seatshow(); void seatingchart(); void readprice(char& seats, ifstream& fin); void filltheater(); void initialize(); int ticketsSold; void initializerc(); private: int rowcount; int seatnum; char seats[ROWS][SEATS]; double prices[ROWS]; double profit; double cost; int seatsavail[15]; int rc; };
// main function body int main() { char seats; ifstream fin; int rc = ROWS;
// call to initialize variables, fill seating chart, and read in the row prices from // the file containing the price array Theater seatnow; seatnow.filltheater(); seatnow.initialize(); seatnow.readprice(seats, fin); seatnow.initializerc();
// switch statement makes the main menu so that the user can select their action // from a variety of options int choice; do { // explanation of options cout << "Hello, and welcome to our movie theater!\n" << "Press 1 to purchase tickets.\n" << "Press 2 to display the total dollar value of all tickets sold.\n" << "Press 3 to display the total number of tickets sold.\n" << "Press 4 to display the number of seats available in each row.\n" << "Press 5 to display the number of seats available in the theater.\n" << "Press 6 to display the current seating chart.\n" << "Press 7 to exit this program." << endl; cin >> choice;
// switch statement, each option calls a function that is defined below int main() // total of 6 function calls in this section // contains a default in case they enter something invalid switch(choice) { case 1: seatnow.buytickets(); break; case 2: seatnow.todayprofit(); break; case 3: seatnow.todaysales(); break; case 4: seatnow.rowshow(); break; case 5: seatnow.seatshow(); break; case 6: seatnow.seatingchart(); break; case 7: cout << "Thank you and please come again!" << endl; break; default: cout << "That is not a valid choice!!\n" << "Choose again." << endl; } } while (choice != 7);
return 0; }
// function definitions void Theater::buytickets() { // initialize variables to zero to essentially reset the option to buy seats int tickcount(0), rowcount(0), seatnum(0), sold(0); int seatsavail[ROWS];
// explantion of what will happen if something invalid is answered // ask how many tickets will be purchased cout << "If a nonexistant seat is entered, your total will be $0.00." << endl; cout << "In that case, please retry." << endl; cout << "How many tickets would you like to purchase?" << endl; cin >> tickcount; // beginning of a do-while loop--> asks for seat information as input, checks to see // if the seat has been sold. // will display an appropriate message if the seat is taken // if the seat is empty, the seat will be sold do { cout << "In what row would you like to sit?" << endl; cin >> rowcount; cout << "What seat number you would like?" << endl; cin >> seatnum;
double cost(0);
if (seats[rowcount-1][seatnum-1] == FULL) { cout << "You can't purchase those tickets-- those seats are full!!" << endl; } else { seats[rowcount-1][seatnum-1] = FULL; sold += 1; int rc = rowcount; seatsavail[rc-1]--; } } while (sold < tickcount);
// BOOK-KEEPING SECTION: // calculates the cost for the individual user // keeps track of the total number of tickets successfully sold // keeps track of the total profit for the theater and displays it as well // properly formats everything so that it is displayed with two decimals int temp = tickcount; cost = temp * prices[rowcount - 1]; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "Your total is $" << cost << endl; ticketsSold += temp; profit += prices[rowcount-1] * temp; cout << "Our profit for today is $" << profit << endl; cost = 0; } // displays the total profit of the theater using the information from above void Theater::todayprofit() { cout << "Today's total profit is $" << profit << endl; } // displays the number of tickets sold for the movie showing void Theater::todaysales() { cout << "There will be " << ticketsSold << " people watching the show.\n"; } // displays the number of available seats in each row void Theater::rowshow() { cout << "The following are the number of seats available in each row:" << endl; for (int rc = 0; rc < 15; ++rc) cout << seatsavail[rc] << endl; cout << endl; } // displays the number of tickets available for purchase --> subtracts the tickets sold // from the total number of seats void Theater::seatshow() { cout << "There are "<< 450 - ticketsSold << " seats available for purchase." << endl; } // displays the array in all its updated seating-chart glory void Theater::seatingchart() { cout << "This is the current seating chart: " << endl; for (int r = 0; r < 15; ++r) { for(int s = 0; s < 30; ++s) cout << seats[r][s]; cout << endl; } } // opens the price file, reads in the prices and stores them, closes the file void Theater::readprice(char& seats, ifstream& fin) { fin.open("seats.txt"); if (fin.fail()) { cout << "I cannot find the prices of the seats- missing input file" << endl; exit(1); }
int tmp = ROWS; for (int i=0; i<ROWS; i++) { fin >> prices[i]; } fin.close(); } // initializes every seat to empty void Theater::filltheater() { for (char ROWS = 0; ROWS < 15; ++ROWS) { for(int SEATS = 0; SEATS < 30; ++SEATS) seats[ROWS][SEATS] = OPEN; } for (char ROWS = 0; ROWS < 15; ++ROWS) { for(int SEATS = 0; SEATS < 30; ++SEATS) cout << seats[ROWS][SEATS]; cout << endl; } } // sets the profit of the theater and the number of tickets sold to zero void Theater::initialize() { profit = 0; ticketsSold = 0; } // initializes the rowcounter void Theater::initializerc() { for (int rc = 0; rc < 15; ++rc) { int seatsavail[rc] = {30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; } }
|