In: Computer Science
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions.
CODE:
#include
using namespace std;
void printWelcome(string movieName, string rating, int
startHour, int startMinute, char ampm); //menu function
//constants
const double TICKET_PRICE = 9.95;
const double TAX_RATE = 0.095;
const bool AVAILABLE = true;
const bool UNAVAILABLE = false;
int subTotal,taxAdded, totalCost;
//
bool checkAvailability( int tickets, int& seatingLeft){
//checks ticket availability
while(tickets > 0 || tickets <= 200) //valid ticket
entry
cout<< AVAILABLE< while(tickets < 0) //invalid number
of tickets entered
cout<< UNAVAILABLE << "Invalid number of tickets: "<
while(tickets > 200)//not enough tickets availible;
cout<< UNAVAILABLE<<"Could not process your request.
There are only x tickets left.";
while(tickets == 0)
cout< }
void calcAndPrintReceipt(int tickets){ //does price
claculations
subTotal = tickets * TICKET_PRICE;
taxAdded = subTotal * TAX_RATE;
totalCost = subTotal + taxAdded;
cout<<"SUBTOTAL: $"< cout<<"TAXES: $"<
cout<<"TOTAL: $"< }
int main() {
int seatingLeft = 300; // number of empty seats available for
purchase
string movieName = "The best movie Ever"; //movie title
string rating = "Rated: PG-13"; // Motion Picture Assoc. of America
rating
int startHour = 2; // start time in 12-hour clock
int startMinute = 30;
char ampm = 'P';
int tickets; // how mamy tickets user wants to buy
//
cout< cout << "How many tickets (0 to end)? ";
cin >> tickets;
while (tickets != 0 ) {
if (checkAvailability(tickets, seatingLeft ) ) {
calcAndPrintReceipt(tickets);
}
cout << "How many tickets (0 to end)? ";
cin >> tickets;
}
cout << "Enjoy the movie!\n";
return 0;
}
INSTRUCTIONS:
This application allows users to purchase movie tickets like a simplified version of Fandango™. Imagine you are creating a program to be used at a small movie theatre that only has one movie showing once a day. This program will be used at a kiosk to sell tickets to customers.
Detailed Information
Your program MUST use the variable and constant declarations below, Note the inline comments for the variables – these are examples of good inline comments. Use them, also. You may initialize the movie title, the rating, the start hour and minute and the ampm variable to different values if you like.
Declare and use the following global constants. Do not change the values of these constants.
const double TICKET_PRICE = 9.95;
const double TAX_RATE = 0.095;
const bool AVAILABLE = true;
const bool UNAVAILABLE = false;
Use the following main function. You may change the values of the movie name, the rating and the start time. DO NOT change anything else in the main function.
int main() { int seatingLeft = 300; // number of empty seats available for purchase // movie title string movieName = "The Best Movie Ever"; string rating = "PG-13"; // Motion Picture Assoc. of America rating int startHour = 4; // start time in 12-hour clock int startMinute = 30; char ampm = 'P'; int tickets; // how mamy tickets user wants to buy
printWelcome( movieName, rating, startHour, startMinute, ampm ); cout << "How many tickets (0 to end)? "; cin >> tickets; while (tickets != 0 ) { if ( checkAvailability( tickets, seatingLeft ) ) { calcAndPrintReceipt( tickets ); } cout << "How many tickets (0 to end)? "; cin >> tickets; } cout << "Enjoy the movie!\n"; return 0; } |
Write the following functions that are called by the main function.
Sample Executions
Sample Execution 1
Welcome to My Theater’s Ticketing Program.
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? 2
Tickets purchased: 2
Subtotal: $19.90
Tax: $1.89
Total: $21.79
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? 5
Tickets purchased: 5
Subtotal: $49.75
Tax: $4.73
Total: $54.48
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? 0
Thank you for using My Theatre’s Ticketing Program.
Sample Execution 2
Welcome to My Theater’s Ticketing Program.
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? -1
Invalid number of tickets: -1
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? 400
Could not process your request. There are only 300 tickets left.
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? 300
Tickets purchased: 300
Subtotal: $2985.00
Tax: $283.57
Total: $3268.57
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? 1
I'm sorry. We're SOLD OUT.
Today we are showing: The Best Movie Ever rated PG-13 at 4:30 PM
How many tickets (0 to end)? 0
Thank you for using My Theatre’s Ticketing Program.
Relevance Questions
This lab requires the use of selection statements in order to decide whether to print one of the messages above. It also requires the use of loops to make it do something over and over again. Most programs of any significant size use loops because they need to do things more than once.
Rubric
#include<iostream>
using namespace std;
//constants
const double TICKET_PRICE = 9.95;
const double TAX_RATE = 0.095;
const bool AVAILABLE = true;
const bool UNAVAILABLE = false;
void printWelcome( string movieName, string rating, int startHour, int startMinute, char ampm ){
cout<<"Welcome to My Theater’s Ticketing Program.\n";
cout<<"Today we are showing: "<<movieName<<" rated "<<rating<<" at "<<startHour<<":"<<startMinute<<" "<<ampm<<"M\n";
}
bool checkAvailability( int tickets, int& seatingLeft ){
//handling invalid number of tickets
if(tickets <= 0){
cout<<"Invalid number of tickets "<<tickets<<"\n";
return UNAVAILABLE;
}
//Checking SOLD OUT condition
if(seatingLeft == 0){
cout<<"I’m sorry. We’re SOLD OUT.\n";
return UNAVAILABLE;
}
//Checking if required amount of seats are left
if(tickets <= seatingLeft){
seatingLeft -= tickets;
return AVAILABLE;
}
//if request cannot be statisfied
cout<<"Could not process your request. There are only "<<seatingLeft<<" tickets left.\n";
return UNAVAILABLE;
}
void calcAndPrintReceipt( int tickets ){
float subTotal = tickets * TICKET_PRICE; //calculating subtotal
float taxAdded = subTotal * TAX_RATE; //calculating total tax based on subtotal amount
float totalCost = subTotal + taxAdded; //calculating total cost including tax
printf("Subtotal: $%.2f\n", subTotal);
printf("Tax: $%.2f\n", taxAdded);
printf("Total: $%.2f\n", totalCost);
}
int main() {
int seatingLeft = 10; // number of empty seats available for purchase
string movieName = "The Best Movie Ever"; // movie title
string rating = "PG-13"; // Motion Picture Assoc. of America rating
int startHour = 4; // start time in 12-hour clock
int startMinute = 30;
char ampm = 'P';
int tickets; // how mamy tickets user wants to buy
printWelcome( movieName, rating, startHour, startMinute, ampm );
cout << "How many tickets (0 to end)? ";
cin >> tickets;
while (tickets != 0 ) {
if ( checkAvailability( tickets, seatingLeft ) ) {
calcAndPrintReceipt( tickets );
}
cout << "How many tickets (0 to end)? ";
cin >> tickets;
}
cout << "Enjoy the movie!\n";
return 0;
}
To compile: g++ <filename>
To execute: ./a.out
Sample output is given below:
Note:
This print statement Thank you for using My Theatre’s Ticketing Program is missing in main method provided above and since modifying main method is not allowed. Please ask your instructor about this.
Also tickets > 0 || tickets <= 200 condition was present in your code where you are limiting the max number of tickets to 200 which is not mentioned in the problem, so I have not kept this in the code.
If you require any further help let me know.