In: Computer Science
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:
Welcome to Yogurt Yummies
-------------------------
Enter the number of yogurts purchased (1-9): 12
Error: '12' is an invalid number of yogurts.
Enter the number of yogurts purchased (1-9): 4
Enter the percentage discount (0-20): 30
Error: '30.00' is an invalid percentage discount.
Enter the percentage discount (0-20): 10
Yogurts: 4
Yogurt cost ($): 3.50
Discount (%): 10.00
Subtotal ($): 14.00
Total after discount ($): 12.60
Tax ($): 0.76
Total ($): 13.36
End of Yogurt Yummies
main.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
double discount, cost=3.50, tax=6;
cout<<"Welcome to Yogurt Yummies"<<endl;
cout<<"-------------------------"<<endl;
do{
cout<<"Enter the number of yogurts purchased (1-9): ";
cin>>n;
if(n<1 || n>9)
cout<<"Error: '"<<n<<"' is an invalid number of
yogurts."<<endl;
}while(n<1 || n>9);
do{
cout<<"Enter the percentage discount (0-20): ";
cin>>discount;
if(discount<0 || discount>20)
cout<<"Error: '"<<discount<<"' is an invalid
percentage discount."<<endl;
}while(discount<0 || discount>20);
double subtotal = n*cost;
double total_after_discount = subtotal -
(subtotal*discount/100);
double total_tax = total_after_discount*tax/100;
double total = total_after_discount+total_tax;
cout<<fixed<<setprecision(2);
cout<<"Yogurts: \t"<<n<<endl;
cout<<"Yogurt cost ($): \t"<<cost<<endl;
cout<<"Discount (%): \t"<<discount<<endl;
cout<<"Subtotal ($): \t"<<subtotal<<endl;
cout<<"Total after discount ($):
\t"<<total_after_discount<<endl;
cout<<"Tax ($): \t"<<total_tax<<endl;
cout<<"Total ($): \t"<<total<<endl;
cout<<"End of Yogurt Yummies"<<endl;
return 0;
}
Code Snippet (For Indentation):
Output: