In: Computer Science
This is an assignment for a C++ introduction class. This module included if statements. If you have any questions please feel free to let me know!
{
In this module you learned about making decisions in C++ and how to combine decision making with the material from the first few modules to solve problems.
For this assignment, write a program that calculates a discount for buying certain quantities of coffee. Consider the following scenario:
A coffee company sells a pound of coffee for $12.99. Quantity discounts are given according to the table below.
Quantity | Discount |
5-9 | 5% |
10-19 | 10% |
20-29 | 15% |
30 or more | 20% |
Write a program that asks for the number of pounds purchased and computes the total cost of the purchase. Make sure the output formatting is appropriate for the values provided. Be sure to include comments throughout your code where appropriate.
Input validation: Decide how the program should handle an input of less than 0.
}
Code:
#include<iostream>
using namespace std;
int main()
{
// this variable is used to store the number
of coffee's ordered
int coffee_quantity;
// prompting the user to enter the coffee
quantity
cout<<"Enter the number of coffee's
needed:";
// reading the quantity
cin>>coffee_quantity;
// calculating the total price
double coffee_price = coffee_quantity * 12.99;
// if the coffee quantity is greater than or
equal to 5 and less than or equal to 9, discount of 5 percent is
applied on total price
if(coffee_quantity >= 5 && coffee_quantity
<=9)
{
coffee_price -= (coffee_price *
0.05);
}
// if the coffee quantity is greater than or
equal to 10 and less than or equal to 19, discount of 10 percent is
applied on total price
else if(coffee_quantity >= 10 &&
coffee_quantity <=19)
{
coffee_price -= (coffee_price *
0.10);
}
// if the coffee quantity is greater than or
equal to 20 and less than or equal to 29, discount of 15 percent is
applied on total price
else if(coffee_quantity >= 20 &&
coffee_quantity <=29)
{
coffee_price -= (coffee_price *
0.15);
}
// if the coffee quantity is greater than or
equal to 30, discount of 20 percent is applied on total
price
else if(coffee_quantity >= 30)
{
coffee_price -= (coffee_price *
0.20);
}
// If the entered coffee quantity is less than
or equal to zero, let the user know that quantity must be
positive
if(coffee_quantity <= 0)
{
cout<<"The quantity of
coffee's to be ordered must be greater than 0\n";
}
// Else print the coffee quantity and the
total price
else
{
printf("Number of coffee's
ordered:%d\n",coffee_quantity);
printf("Total cost of the purchase:
$%.2f\n",coffee_price);
}
return 0;
}
Sample Output: