Question

In: Computer Science

Lab 10-2:You've been hired by Yogurt Yummies to write a C++ console application that calculates and...

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

Solutions

Expert Solution

#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:


Related Solutions

You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays...
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...
You've been hired by Avuncular Addresses to write a C++ console application that analyzes and checks...
You've been hired by Avuncular Addresses to write a C++ console application that analyzes and checks a postal address. Prompt for and get from the user an address. Use function getline so that the address can contain spaces. Loop through the address and count the following types of characters:         ● Digits (0-9)         ● Alphabetic (A-Z, a-z)         ● Other Use function length to control the loop. Use functions isdigit and isalpha to determine the character types. Use formatted...
You've been hired by Water Wonders to write a C++ console application that analyzes lake level...
You've been hired by Water Wonders to write a C++ console application that analyzes lake level data. MichiganHuronLakeLevels.txt. Place the input file in a folder where your development tool can locate it (on Visual Studio, in folder \). The input file may be placed in any folder but a path must be specified to locate it. MichiganHuronLakeLevels.txt Down below: Lake Michigan and Lake Huron - Average lake levels - 1860-2015 Year    Average level (meters) 1860    177.3351667 1861    177.3318333 1862    177.316...
You've been hired by Water Wonders to write a C++ console application that analyzes lake level...
You've been hired by Water Wonders to write a C++ console application that analyzes lake level data. Place the input file in a folder where your development tool can locate it (on Visual Studio, in folder <project-name>\<project-name>). The input file may be placed in any folder but a path must be specified to locate it. The input file has 158 lines and looks like this: Lake Michigan and Lake Huron - Average lake levels - 1860-2015 Year    Average level (meters)...
write a c++ code for following application You have been hired by XYZ Car Rental to...
write a c++ code for following application You have been hired by XYZ Car Rental to develop a software system for their business. Your program will have two unordered lists, one of Cars and one of Reservations. The Car class will have the following data: string plateNumber (this is the key) string make string model enum vehicleType (VehicleType Enumeration of options: sedan, suv, exotic) double pricePerDay bool isAvailable isAvailable should be set to true on initialization, and a public setter...
write a c# console application app that reads all the services in the task manager and...
write a c# console application app that reads all the services in the task manager and automatically saves what was read in a Notepad txt file.  Please make sure that this program runs, also add some comments
Loops Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance....
Loops Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance. Upon execution the program will request ether 1 Series or 2 Parallel then the number of resisters. The program will then request each resistor value. Upon entering the last resistor the program will print out the equivalent resistance. The program will ask if another run is requested otherwise it will exit.
How can we write an application that calculates connascence of an application?
How can we write an application that calculates connascence of an application?
Write a C++ console application that allows your user to enter the total rainfall for each...
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT