In: Computer Science
Lab 10-2:You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays the cost of a customer’s yogurt purchase. Use a validation loop to prompt for and get from the user the number of yogurts purchased in the range 1-9. Then use a validation loop to prompt for and get from the user the coupon discount in the range 0-20%. Calculate the following:
● Subtotal using a cost of $3.50 per yogurt. ● Subtotal after discount
● Sale tax using rate of 6%. ● Total
Use formatted output manipulators (setw, left/right) to print the following rows:
● Yogurts purchased ● Yogurt cost ($) ● Discount (%)
● Subtotal ($) ● Subtotal after discount ($) ● Tax ($)
● Total ($)
And two columns:
● A left-justified label (including units)
● A right-justified value.
Define constants for the yogurt cost, sales tax rate, and column widths. Format all real numbers to two decimal places. Run the program with invalid and valid inputs. The output should look like this:
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std;
#define t 6.0
#define Yogurt_cost 3.50
int main()
{
float Yogurts_purchased,Discount,Subtotal,after_discount,tax,total;
cout<<"Welcome to Yogurt Yummies";
cout<<"\n-------------------------\n";
while (1>0)
{
cout<<"Enter the number of yogurts purchased (1-9):";
// input the number of yougurts purchased
cin>>Yogurts_purchased;
//checking number of yougurts purchased is valid
if(Yogurts_purchased>=1 && Yogurts_purchased<=9)
break;
else
cout<<"\nError:'"<<Yogurts_purchased<<"'is an invalid number of yogurts.\n ";
}
while (1>0)
{
// input the percentage discount
cout<<"Enter the percentage discount (0-20): :";
cin>>Discount;
// checking percentage discount is valid
if(Discount>=0 && Discount<=20)
break;
else
cout<<"Error:'"<<Discount<<"' is an invalid percentage discount.\n ";
}
//evaluating the Subtotal
Subtotal=Yogurts_purchased*Yogurt_cost;
//evaluating the after discount
after_discount=Subtotal-(Subtotal*(Discount/100));
//evaluating the tax
tax=after_discount*(t/100.0);
//evaluating the total
total=tax+after_discount;
//show the values
cout<<"\n Yogurts: "<<setw(26)<<Yogurts_purchased;
cout<<"\n Yogurt cost($):"<<setw(20)<< fixed << setprecision(2)<<Yogurt_cost;
cout<<"\n Discount (%):"<<setw(22)<< fixed << setprecision(2)<<Discount;
cout<<"\n Subtotal ($):"<<setw(22)<< fixed << setprecision(2)<<Subtotal;
cout<<"\n Total after discount ($):"<<setw(10)<< fixed << setprecision(2)<<after_discount;
cout<<"\n Tax ($): :"<<setw(25)<< fixed << setprecision(2)<<tax;
cout<<"\n Total ($):"<<setw(25)<< fixed << setprecision(2)<<total;
cout<<"\n\nEnd of Yogurt Yummies";
return 0;
}
Remember that great app you wrote for Yogurt Yummies (Lab 10-2). They want you to enhance the C++ console application that calculates and displays the cost of a customer’s yogurt purchase. Now they need to process multiple sales. Do not change any logic for handling a single sale. Make the following enhancements:
● Add "v2" to the application output header and close.
● Declare and initialize overall totals including:
Number of sales.
Overall number of yogurts sold.
Overall sale amount after discount.
Overall tax paid.
Overall sale total.
● Enclose the logic for a single sale with a sentinel loop that continues to process sales until the user enters 'n'.
● After calculating sale totals, update overall totals.
● When the user enters the sentinel value ('n'), print overall totals using formatted output manipulators (setw, left/right). Run the program with invalid and valid inputs, and at least three sales. The output should look like this:
Welcome to Yogurt Yummies, v2
-----------------------------
Enter another yogurt purchase (y/n)? y
Sale 1
----------------------------------------
Enter the number of yogurts purchased (1-9): 11
Error: '11' is an invalid number of yogurts.
Enter the number of yogurts purchased (1-9): 2
Enter the percentage discount (0-20): 22
Error: '22.00' is an invalid percentage discount.
Enter the percentage discount (0-20): 4
Yogurts: 2
Yogurt cost ($): 3.50
Discount (%): 4.00
Subtotal ($): 7.00
Total after discount ($): 6.72
Tax ($): 0.40
Total ($): 7.12
Enter another yogurt purchase (y/n)? y
Sale 2
----------------------------------------
Enter the number of yogurts purchased (1-9): 5
Enter the percentage discount (0-20): 10
Yogurts: 5
Yogurt cost ($): 3.50
Discount (%): 10.00
Subtotal ($): 17.50
Total after discount ($): 15.75
Tax ($): 0.94
Total ($): 16.70
Enter another yogurt purchase (y/n)? y
Sale 3
----------------------------------------
Enter the number of yogurts purchased (1-9): 7
Enter the percentage discount (0-20): 20
Yogurts: 7
Yogurt cost ($): 3.50
Discount (%): 20.00
Subtotal ($): 24.50
Total after discount ($): 19.60
Tax ($): 1.18
Total ($): 20.78
Enter another yogurt purchase (y/n)? n
Overall totals
========================================
Sales: 3
Yogurts: 14
Total after discount ($): 42.07
Tax ($): 2.52
Total ($): 44.59
End of Yogurt Yummies, v2
#include <iostream>
#include <iomanip>
#define COST 3.50
#define TAX 0.06
using namespace std;
// Function to accept number of yogurts from the user and
validates
// returns the number of yogurts by using pass by reference
void acceptNumberOfYogurts(int &numberOfYogurts)
{
// Loops till valid number of yogurts entered by the user
do
{
// Accepts number of yogurts
cout<<"\n Enter the number of yogurts purchased (1-9):
";
cin>>numberOfYogurts;
// Checks if number of yogurts is greater than or equals to
1
// and number of yogurts is less than or equals to 9
if(numberOfYogurts>= 1 && numberOfYogurts <= 9)
// Come out of the loop for valid data
break;
// Otherwise invalid data display error message
else
cout<<"\n Error: \'"<<numberOfYogurts<<"\' is an
invalid number of yogurts.";
}while(1);// End of do - whil loop
}// End of function
// Function to accept percentage discount from the user and
validates
// returns the percentage discount by using pass by reference
void acceptPercentageDiscount(double &percentageDiscount)
{
// Loops till valid percentage discount entered by the user
do
{
// Accepts percentage discount
cout<<"\n Enter the percentage discount (0 - 20): ";
cin>>percentageDiscount;
// Checks if percentage discount is greater than or equals to
0
// and percentage discount is less than or equals to 20
if(percentageDiscount >= 0 && percentageDiscount <=
20)
// Come out of the loop for valid data
break;
// Otherwise invalid data display error message
else
cout<<"\n Error: \'"<<percentageDiscount<<"\' is
an invalid percentage discount.";
}while(1);// End of do - whil loop
}// End of function
// main function definition
int main()
{
int numberOfYogurtsPurchased;
double percentageDiscount;
int countSales = 0;
int totalYogurts = 0;
double subTotal = 0.0;
double afterDiscount = 0.0;
double tax = 0.0;
double total = 0.0;
double totalAfterDiscount = 0.0;
double totalTax = 0.0;
double overalTotal = 0.0;
char choice;
cout<<"\n ----------------------------- Welcome to Yogurt Yummies, v2 -----------------------------\n";
// Loops till user choice is not equals to 'n' or 'N'
do
{
// Accepts user choice
cout<<"\n Enter another yogurt purchase (y/n)? ";
cin>>choice;
// Checks if user choice is equals to 'n' or 'N' then come out
of the loop
if(choice == 'n' || choice == 'N')
break;
// Displays sales counter
cout<<"\n Sales: "<<++countSales;
cout<<"\n ----------------------------------------";
// Calls the function to accept number of yogurts purchased
acceptNumberOfYogurts(numberOfYogurtsPurchased);
// Calls the function to accept percentage discount
acceptPercentageDiscount(percentageDiscount);
// Calculates to total number of yogurts purchased
totalYogurts += numberOfYogurtsPurchased;
// Calculates sub total by multiplying number of yogurts purchased
with cost per of yogurts purchased
subTotal = numberOfYogurtsPurchased * COST;
// Calculate amount after discount
afterDiscount = subTotal - (subTotal * percentageDiscount) /
100.0;
// Calculates tax
tax = (afterDiscount * TAX);
// Calculates total
total = afterDiscount + tax;
// Displays current purchase report
cout<<left<<setw(28)<<"\n Yogurts:
"<<right<<numberOfYogurtsPurchased;
cout<<left<<setw(28)<<"\n Yogurt cost ($):
"<<right<<fixed<<setprecision(2)<<COST;
cout<<left<<setw(28)<<"\n Discount (%):
"<<right<<percentageDiscount;
cout<<left<<setw(28)<<"\n Subtotal ($):
"<<right<<subTotal;
cout<<left<<setw(28)<<"\n Total after discount
($): "<<right<<afterDiscount;
cout<<left<<setw(28)<<"\n Tax ($):
"<<right<<tax;
cout<<left<<setw(28)<<"\n Total ($):
"<<right<<total;
// Calculates total of after discount
totalAfterDiscount += afterDiscount;
// Calculates total tax
totalTax += tax;
// Calculates overall total
overalTotal += total;
}while(1);// End of do - while loop
// Displays overall report
cout<<"\n Overall totals";
cout<<"\n ========================================";
cout<<left<<setw(28)<<"\n Sales:
"<<right<<countSales;
cout<<left<<setw(28)<<"\n Yogurts:
"<<right<<totalYogurts;
cout<<left<<setw(28)<<"\n Total after discount
($): "<<right<<totalAfterDiscount;
cout<<left<<setw(28)<<"\n Tax ($):
"<<right<<setprecision(2)<<totalTax;
cout<<left<<setw(28)<<"\n Total ($):
"<<right<<setprecision(2)<<overalTotal;
return 0;
}// End of function
Sample Output: