Commit 10e321d4 authored by Jared Sexton's avatar Jared Sexton

Upload previous file I/O example

parent 5e88c095
# CPTR 141: Project #3
## Problem Overview
In this project, you will write a C++ program that can be used by
small theaters to sell tickets for performances. Your program will
allow a theater to enter the number of rows and columns of seating
that they have and the price for each row of seats. It will then
allow the theater staff to sell individual seats or groups of seats,
give the total for each order, and keep track of which seats are still
available.
![Theater
Seating](http://www.preferred-seating.com/images/Phoenix-Elite-36-Front.jpg)
Here is what your program should do:
* The first time your program is run, it should prompt the user for
the number of rows and columns in the theater and for the price for
seats row of seating (up to a pre-defined maximum number). Store that
information in a configuration file that you can then read the next
time the program is run.
* Display a main menu to the user with the following options:
* Display a seating chart
* Sell one or more tickets
* Display statistics such as the number of tickets sold, the
number of seats still available, the total revenue from ticket sales,
etc.
* Reset the program and re-enter the theater seating and pricing
information
* The seating chart should be displayed in a fashion similar to the
one below (which is for a theater with 10 rows and 30 columns of
seating) where taken seats are represented by a * symbol and available
seats are represented by a # symbol.
```
Seats
0 1 2 3
123456789012345678901234567890
Row 1 ***###***###*########*****####
Row 2 ####*************####*******##
Row 3 **###**********########****###
Row 4 **######**************##******
Row 5 ********#####*********########
Row 6 ##############************####
Row 7 #######************###########
Row 8 ************##****############
Row 9 #########*****############****
Row 10 #####*************############
```
* When selling tickets, the program should allow the user to enter
individual seats (by row and seat number) and/or ranges of seats. You
must validate user input so that you do not accept a row or seat
number that does not exist and you do not sell a seat that is already
taken. When the group of seats has been reserved, display the total
price for the transaction.
* Extra Feature: Although not required, you may wish to have your
program save the seating chart to a configuration file so that it can
be read back in when the program is restarted.
## Solution Specification
Your solution should strive to meet the standards specified below as
they form the basis on which it will be graded.
1. You must use a two-dimensional array to keep track of your seating
chart. You may wish to use other arrays to keep track of additional
information.
2. Your program must be divided into functions which perform
well-defined and logical sub-tasks for the problem. Before you go too
far on your development, check with your professor or TA about your
choice of functions and the parameters and/or return types that they
will require.
2. The file `theater.txt` must be used to save the theater
configuration so that when the program starts again, the information
can be read from the file instead of being re-entered by the user.
When your program starts, check to see if this file exists and if it
does not, create it.
3. You must validate all user input and handle errors gracefully.
5. Since this is your final project in the class, show off as much of
what you've learned as possible. In particular, you are expected to
make use of the following concepts somewhere in your program.
* Appropriate branching and looping statements
* Console input/output and file output using streams
* Functions with various return and parameter types
(pass-by-reference, default values, etc)
* Global constants (likely for your maximum array dimensions)
* Arrays and passing arrays to functions
## Code Review and Grading
Before you can turn in your project, you must participate in a [code
review](https://en.wikipedia.org/wiki/Code_review) with your
instructor or TA. This [walk-through](https://en.wikipedia.org/wiki/)
style review is a guided-tour of your source code in which you
describe how you implemented the various features, explain why you
made the choices you did, and solicit constructive input which might
help improve your final product.
Once your final product has been turned in, it will be graded
according to the following rubric.
\ No newline at end of file
/****************************************************************************
*
* Proj.03: A program for small theaters to manage seating.
*
* File Name: theater.cpp
* Name: Jared Sexton
* Course: CPTR 141
* Date: November 29, 2017
*
*/
// To Compile: g++ theater.cpp theater_functions.cpp -o theater.o
#include <iostream>
#include <fstream>
#include "theater_functions.h"
using namespace std;
int main() {
//Define variables
int rows, columns;
double revenue;
double price[ROW_LIMIT];
char seats [ROW_LIMIT] [COL_LIMIT];
bool runagain = true;
initialize(seats, rows, columns, price);
//Run program
do {
int decision = displayMenu();
switch (decision) {
case 1: // Display chart
displayChart(seats, rows, columns);
break;
case 2: // Sell Tickets
revenue += sellTickets(seats, rows, columns, price);
break;
case 3: // Display Statistics
displayStats(seats, rows, columns, revenue);
break;
case 4: // Reset the program (initialize it again)
initializeOut();
initializeIn(seats, rows, columns, price);
break;
case 5: // Exit the program
//exit(0);
runagain = false;
break;
default: // Just in case
cerr << "Invalid entry. Please try again.\n";
}
} while (runagain == true);
return 0;
}
10
35
100 75 75 50 50 50 50 40 40 30
\ No newline at end of file
/****************************************************************************
*
* Proj.03: A program for small theaters to manage seating.
*
* File Name: theater.cpp
* Name: Jared Sexton
* Course: CPTR 141
* Date: November 29, 2017
*
*/
#include <iostream>
#include <fstream>
using namespace std;
ofstream fout;
ifstream fin;
//Global constants
const int ROW_LIMIT = 100, COL_LIMIT = 100;
//Function definitions
void getRowData(int& rows) {
bool valid;
do {
valid = true;
cout << "Please enter the number of rows in your theater (up to " << ROW_LIMIT << "): ";
if (!(cin >> rows) || (rows > ROW_LIMIT) || (rows <= 0)) {
cerr << "Invalid entry. Please try again.\n";
cin.clear();
cin.ignore(256, '\n');
valid = false;
}
} while (valid == false);
}
void getColData(int& columns) {
bool valid;
do {
valid = true;
cout << "Please enter the number of seats in each row (up to " << COL_LIMIT << "): ";
if (!(cin >> columns) || (columns > COL_LIMIT) || (columns <= 0)) {
cerr << "Invalid entry. Please try again.\n";
cin.clear();
cin.ignore(256, '\n');
valid = false;
}
} while (valid == false);
}
void getPriceData(double price [], int rows) {
bool valid;
do {
valid = true;
for (int i = 0; i < rows; i++) {
cout << "Please enter the seat price for Row " << i + 1 << ": $";
if (!(cin >> price[i]) || (price[i] < 0)) {
cerr << "Invalid entry. Please try again.\n";
cin.clear();
cin.ignore(256, '\n');
valid = false;
}
}
} while (valid == false);
}
void initializeOut() {
fout.open("theater.txt");
if( !fout )
cerr << "can't open file\n";
else {
int rows, columns;
double price [ROW_LIMIT];
//Collect user data
getRowData(rows);
getColData(columns);
getPriceData(price, rows);
//Save values to configuration file
fout << rows << endl << columns << endl;
for (int i = 0; i < rows; i++) {
fout << price[i] << " ";
}
}
fout.close();
}
void initializeIn(char seats [] [COL_LIMIT], int& rows, int& columns, double price []) {
fin.open("theater.txt");
if( !fin )
cerr << "can't open file\n";
else {
fin >> rows >> columns;
for (int i = 0; i < rows; i++) {
fin >> price[i];
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
seats [r] [c] = '#';
}
}
}
fin.close();
}
void initialize(char seats [] [COL_LIMIT], int& rows, int& columns, double price []) {
fin.open("theater.txt");
if( !fin )
cerr << "can't open file\n";
else {
int check;
if (!(fin >> check))
initializeOut();
}
fin.close();
initializeIn(seats, rows, columns, price);
}
int displayMenu() {
int choice;
bool valid;
cout << "\nWelcome to T-TAP, the Theater Ticket Assist Program.\n";
do {
valid = true;
cout << "You may select: \n"
<< " (1) Display a seating chart\n"
<< " (2) Sell tickets\n"
<< " (3) Display statistics about ticket sales and remaining seats\n"
<< " (4) Reset the program\n"
<< " (5) Exit the program\n"
<< "What would you like to do?\n";
if (!(cin >> choice) || (!(choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5))) {
cerr << "Invalid entry. Please try again.\n";
cin.clear();
cin.ignore(256, '\n');
valid = false;
}
} while (valid == false);
cout << endl;
return choice;
}
void displayChart(char seats [] [COL_LIMIT], int rows, int columns) {
cout << " 0 ";
for (int i = 1; i <= columns / 10; i++) {
cout << i << " ";
}
cout << endl << " ";
for (int i = 1; i <= columns; i++) {
cout << i % 10;
}
cout << endl;
for (int r = 0; r < rows; r++) {
if (r + 1 < 10)
cout << "Row " << r + 1 << " ";
else
cout << "Row " << r + 1 << " ";
for (int c = 0; c < columns; c++) {
cout << seats [r] [c];
}
cout << endl;
}
}
bool seatsFree(char seats [] [COL_LIMIT], int rowChoice, int colChoice, int range) {
bool available = true;
for (int c = colChoice - 1; c < colChoice - 1 + range; c++) {
if (seats [rowChoice - 1] [c] == '*')
available = false;
}
return available;
}
double purchaseSeats(char seats [] [COL_LIMIT], int rowChoice, int colChoice, double price [], int range) {
for (int c = colChoice - 1; c < colChoice - 1 + range; c++) {
seats [rowChoice - 1] [c] = '*';
}
return price [rowChoice - 1] * range;
}
double sellTickets(char seats [] [COL_LIMIT], int rows, int columns, double price[]) {
// Define variables
char purchaseType, trash;
bool valid;
int rowChoice, colChoice, rangeChoice;
double revenue;
// Determine purchase type
do {
valid = true;
cout << "Would you like to buy a (S)ingle seat or a (R)ange? ";
if (!(cin >> purchaseType) || (!(purchaseType == 'S' || purchaseType == 'R'))) {
cerr << "Invalid entry. Please try again.\n";
cin.clear();
cin.ignore(256, '\n');
valid = false;
}
} while (valid == false);
// Determine the number of tickets to be purchased
if (purchaseType == 'S') {
rangeChoice = 1;
}
else {
do {
valid = true;
cout << "How many seats would you like to purchase? ";
if (!(cin >> rangeChoice) || (rangeChoice <= 0)) {
cerr << "Invalid entry. Please try again.\n";
cin.clear();
cin.ignore(256, '\n');
valid = false;
}
} while (valid == false);
}
// Determine which seats are being purchased
displayChart(seats, rows, columns);
do {
valid = true;
if (purchaseType == 'S')
cout << "Please enter the row, and the seat number of the seat being purchased.\n";
else
cout << "Please enter the row, and the seat number of the first seat in the range being purchased.\n";
cout << "Please use the form: row, seat # ";
if (!(cin >> rowChoice >> trash >> colChoice)
|| !((rowChoice - 1 < rows) && (rowChoice > 0)) || !((colChoice + rangeChoice - 1 < columns) && (colChoice > 0))
|| (!seatsFree(seats, rowChoice, colChoice, rangeChoice))) {
cerr << "Invalid entry. Please try again.\n";
cin.clear();
cin.ignore(256, '\n');
valid = false;
}
} while (valid == false);
// Display final bill
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
revenue = purchaseSeats(seats, rowChoice, colChoice, price, rangeChoice);
cout << "You owe $" << revenue << endl;
return revenue;
}
void displayStats(char seats [] [COL_LIMIT], int rows, int columns, double revenue) {
//Display total revenue
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Total ticket sales: $" << revenue << endl;
//Display total number of tickets sold
int sold = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
if (seats [r] [c] == '*')
sold++;
}
}
cout << "Total tickets sold: " << sold << endl;
//Display total number of seats available
int available = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
if (seats [r] [c] == '#')
available++;
}
}
cout << "Total seats still available: " << available << endl;
}
\ No newline at end of file
/****************************************************************************
*
* Proj.03: A program for small theaters to manage seating.
*
* File Name: theater.cpp
* Name: Jared Sexton
* Course: CPTR 141
* Date: November 29, 2017
*
*/
//Global constants
const int ROW_LIMIT = 100, COL_LIMIT = 100;
//Function prototypes
/* Collects the number of rows for initialization.*/
void getRowData(int& rows);
/* Collects the number of columns for initialization.*/
void getColData(int& columns);
/* Collects the price per row for initialization.*/
void getPriceData(double price [], int rows);
/* References initializeOut if there is not currently data in the configuration file
and then references initializeIn to fill the necessary variables and arrays.*/
void initialize(char seats [] [COL_LIMIT], int& rows, int& columns, double price []);
/* Collects user input about the size of the theater
and the seat price per row.*/
void initializeOut();
/* Saves the information from the configuration file
to the variables and arrays needed for the program.*/
void initializeIn(char seats [] [COL_LIMIT], int& rows, int& columns, double price []);
/* Displays the menu of options that the user has
and returns an integer representing their choice.*/
int displayMenu();
/* Displays the current state of the seating chart.*/
void displayChart(char seats [] [COL_LIMIT], int rows, int columns);
/* check if all seats are free within a given range.
returns false, if not.*/
bool seatsFree(char seats [] [COL_LIMIT], int rowChoice, int colChoice, int range);
/* Purchases the seats in the specified range
returns the total revenue from this sale*/
double purchaseSeats(char seats [] [COL_LIMIT], int rowChoice, int colChoice, double price [], int range);
/* Sells tickets in either singles or in a range
and returns the total revenue from that sale.*/
double sellTickets(char seats [] [COL_LIMIT], int rows, int columns, double price[]);
/* Displays relevent statistics such as
total revenue, total seats sold and total seats available.*/
void displayStats(char seats [] [COL_LIMIT], int rows, int columns, double revenue);
......@@ -10,7 +10,8 @@ using namespace std;
int main() {
Questions questionList;
questionList.countingQuestion(2);
cout << questionList.countingQuestion(2);
return 0;
}
\ No newline at end of file
......@@ -4,6 +4,7 @@
============================================================================
*/
#include "Questions.h"
#include <iostream>
#include <cstdlib>
using namespace std;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment