In: Computer Science
Overview
For this program, add code to program 2 that will (potentially) allow for a discount to be applied to the cost of the tickets to the movie theater.
Basic Program Logic
The program should start the same as program 2 by asking the user for the number of tickets to purchase for adult and children.
The program should then ask the user if they have a discount coupon. This is a string value. A value of "Y" indicates the user does have a coupon. Any other value indicates the user does not have a coupon.
If the user does have a discount coupon, ask if the coupon is for a free adult ticket or free child ticket. This is also a string value. A value of "A" is for a free adult ticket which means that a discount of 11.25 should be applied to total purchase. A value of "C" is for a free child ticket which means that a discount of 4.50 should be applied to the total purchase. Any other value is invalid which means that no discount should be applied to the total purchase. If an invalid discount type is selected, display a message to the user that their discount type is not valid and that no discount will be applied.
Calculate the user's total purchase amount, making sure to apply the appropriate discount amount (11.25, 4.50, or 0.00).
Finally, display the results similar to program 2: the number of adult tickets that were purchased, the number of child tickets that were purchased, the discount amount (if a discount was applied to the purchase), and the total purchase amount. The discount amount should ONLY be displayed if the user indicated they had a discount coupon. As with program 2, use setw to line up the last digit of the displayed values and setprecision to display 2 digits after the decimal point.
Program Requirements
At the top of the C++ source code, include a documentation box that resembles the one from programs 1 and 2.
The dollar amounts should all be displayed with exactly 2 digits after the decimal point, including zeros.
The numeric values read in from the user should all be integer values. The discount values read in from the user should all be string values. Use meaningful variable names.
Make sure and test the program with values other than those in the sample output.
Output
A few runs of the program should produce the following results:
Run 1
Enter the number of adult tickets that are being purchased: 2 Enter the number of child tickets that are being purchased: 5 Do you have a discount coupon (Y for yes)? Y Is the discount for an adult or child's ticket (A for adult, C for child)? C ************************************ Theater Sale ************************************ Number of adult tickets: 2 Number of child tickets: 5 Discount: 4.50 Total purchase: 40.50
Run 2
Enter the number of adult tickets that are being purchased: 1 Enter the number of child tickets that are being purchased: 2 Do you have a discount coupon (Y for yes)? n ************************************ Theater Sale ************************************ Number of adult tickets: 1 Number of child tickets: 2 Total purchase: 20.25
Run 3
Enter the number of adult tickets that are being purchased: 2 Enter the number of child tickets that are being purchased: 2 Do you have a discount coupon (Y for yes)? Y Is the discount for an adult or child's ticket (A for adult, C for child)? S Error: S is not a valid discount type. No discount will be applied ************************************ Theater Sale ************************************ Number of adult tickets: 2 Number of child tickets: 2 Discount: 0.00 Total purchase: 31.50
Run 4
Enter the number of adult tickets that are being purchased: 4 Enter the number of child tickets that are being purchased: 2 Do you have a discount coupon (Y for yes)? Y Is the discount for an adult or child's ticket (A for adult, C for child)? A ************************************ Theater Sale ************************************ Number of adult tickets: 4 Number of child tickets: 2 Discount: 11.25 Total purchase: 42.75
Answer :
please upvote if its upto your expectation or comment for any query.
Program plan :
1. run a while loop to continuously asking for input
2. get input no of adult ticket ,child ticket and store in variables
3. get discount input and check for discount.
4. check discount type A for adult C for child else display error
5. calculate total purchase amount and display
Program :
#include <iostream> //Header File for Basic I/O
#include <iomanip> //Header file for setw()
using namespace std;
const double ChildTicketAmount=4.50; //ticket amount
const double AdultTicketAmount=11.25;
const double InvalidDiscountAmount=0.00;
int main()
{
while(1)
{
int adultTicket=0,childTicket=0; //variable for number of
ticket
double totalPurchase=0.0;
char Discount='N',DiscountType='X'; //string input for
discount
bool DiscountError=false; //store error of discount type
/*
Program 1 of C++ for getting input of Theatre ticket
here we store adult ticket ,child ticket ,discount type in
variable
to calculate in program 2
*/
cout<<"Enter the number of adult tickets that are being
purchased:"; //enter no. of adult ticket
cin>>adultTicket;
cout<<"Enter the number of child tickets that are being
purchased:"; //enter no. of child ticket
cin>>childTicket;
cout<<"Do you have a discount coupon (Y for yes)?"; //enter
discount y for yes and n for no both cases supported
cin>>Discount;
if(Discount=='Y' || Discount=='y') //both cases supported
{
cout<<"Is the discount for an adult or child's ticket (A
for adult, C for child)?"; //get discount type
cin>>DiscountType;
switch(DiscountType) //switch for type of discount
{
case 'A':
DiscountError=false;
break;
case 'a':
DiscountError=false;
break;
case 'c':
DiscountError=false;
break;
case 'C':
DiscountError=false;
break;
default:
cout<<"Error: "<<DiscountType << "is not a valid
discount type. No discount will be applied"<<endl;
DiscountError=true;
break;
}
}
/*
Program 2
Calculate total ticket amount using variable we get in Program 1
and deduct appropriate discount amount
display no of ticket for adult ,child
display discount amount if have
display total purchase of ticket
*/
cout<<"************************************\n\tTheater
Sale\n************************************";
cout <<setprecision(2) << fixed; //set precision for 2
decimal point
totalPurchase=(childTicket*ChildTicketAmount)+(adultTicket*AdultTicketAmount);
//calculate total purchase
cout<<endl<<"Number of adult
tickets:"<<setw(5)<<adultTicket; //output on
screen
cout<<endl<<"Number of child
tickets:"<<setw(5)<<childTicket;
if(DiscountType=='C' || DiscountType=='c') //to deduct discount
amount
{
cout<<endl<<"Discount
Amount:"<<setw(17)<<ChildTicketAmount;
totalPurchase-=ChildTicketAmount; //subtract 4.50
}
else if(DiscountType=='A' || DiscountType=='a') //subtract
11.25
{
cout<<endl<<"Discount
Amount:"<<setw(17)<<AdultTicketAmount;
totalPurchase-=AdultTicketAmount;
}
if(DiscountError)
{
cout<<endl<<"Discount
Amount:"<<setw(17)<<InvalidDiscountAmount;
}
cout<<endl<<"Total Purchase
:"<<setw(17)<<totalPurchase<<endl<<endl<<endl;
// display total purchase
}
return 0;
}
Output Images :
Run1
run2
run3
run4