Question

In: Computer Science

This is an assignment for a C++ introduction class. This module included if statements. If you...

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.

}

Solutions

Expert Solution

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:


Related Solutions

0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
Module 07 Written Assignment - C-Diff Your written assignment for this module should be a 1-2...
Module 07 Written Assignment - C-Diff Your written assignment for this module should be a 1-2 page paper (not including title page and reference page) that describes the following: -You are caring for a patient with c-diff as part of your workload assignment. Discuss what c-diff is and how it is transmitted (how you can get it)? -What actions will you take as a nurse to protect yourself and the other patients on the unit when taking care of your...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
Prior to class, and after reading/viewing the module assignment, select one of the following and conduct...
Prior to class, and after reading/viewing the module assignment, select one of the following and conduct an assessment. Musculoskeletal System Neurological System You may conduct the assessment on a fellow student, friend, or family member. Remember to secure their permission. Collect both subjective and objective data using the process described in the textbook.
Prior to class, and after reading/viewing the module assignment, select one of the following and conduct...
Prior to class, and after reading/viewing the module assignment, select one of the following and conduct an assessment. Heart and Neck Vessels Peripheral Vascular System You may conduct the assessment on a fellow student, friend, or family member. Remember to secure their permission. Collect both subjective and objective data using the process described in the textbook. Then, document your findings and bring them to class.
Prior to class, and after reading/viewing the module assignment, select one of the following and conduct...
Prior to class, and after reading/viewing the module assignment, select one of the following and conduct an assessment. Musculoskeletal System Neurological System You may conduct the assessment on a fellow student, friend, or family member. Remember to secure their permission. Collect both subjective and objective data using the process described in the textbook. Then, document your findings and bring them to class.
C++ In this assignment you will use your Card class to simulate a deck of cards....
C++ In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows: The constructor will create a full 52-card deck of cards. 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace for each suit: Clubs, Diamonds, Hearts, Spades Each card will be created on the heap and the deck will be stored using an STL vector of Card pointers. The destructor will free the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT