fork download
  1. //Sam Partovi CS1A Chapter 4, P. 221, #10
  2. //
  3. /*******************************************************************************
  4. *
  5. * COMPUTE QUANTITY-BASED DISCOUNT
  6. * ____________________________________________________________
  7. * This program computes discount percentage and total cost based on a given
  8. * item quantity, with varying discounts for multiple quantity ranges.
  9. *
  10. * Discounts are determined by the following table:
  11. * 0 - 9 units = 0% discount 10 - 19 units = 20% discount
  12. * 20 - 49 units = 30% discount 50 - 99 units = 40% discount
  13. * 100+ units = 50% discount
  14. *
  15. * The total purchase price is calculated with the following formula:
  16. * totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal)
  17. *
  18. * The total savings are calulated with the following formula:
  19. * totalSavings = (unitsSold * itemPrice * discountDecimal)
  20. * ____________________________________________________________
  21. * INPUT
  22. * unitsSold : Quantity of units to be sold
  23. * discountDecimal : Discount expressed as a decimal
  24. * itemPrice : Price per item
  25. *
  26. * OUTPUT
  27. * discountPercentage : Discount expressed as a percentage
  28. * totalCost : Total purchase price after realizing discount
  29. * totalSavings : Total savings after realizing discount
  30. *
  31. ******************************************************************************/
  32. #include <iostream>
  33. #include <iomanip>
  34. using namespace std;
  35. int main ()
  36. {
  37.  
  38. int unitsSold; //INPUT - Quantity of units to be sold
  39. float discountDecimal; //INPUT - Discount expressed as a decimal
  40. float discountPercentage; //OUTPUT - Discount expressed as a percentage
  41. float totalCost; //OUTPUT - Total purchase price after realizing discount
  42. float totalSavings; //OUTPUT - Total savings after realizing discount
  43.  
  44. //
  45. //Initialize program variables
  46. const float itemPrice = 99.00;
  47. discountDecimal = 0.00; //Before receiving user input, discount is 0%.
  48.  
  49. //
  50. //Prompt user to input unitsSold
  51. cout << "Enter the quantity of software packages to purchase: \n";
  52. cin >> unitsSold;
  53.  
  54. //
  55. //Compute discount, savings, and total for 0-9 units
  56. if (unitsSold > 0 and unitsSold < 10) {
  57. totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
  58. totalSavings = (unitsSold * itemPrice * discountDecimal);
  59.  
  60. cout << "Your purchase of " << unitsSold <<
  61. " unit(s) does not qualify for a discount.\n";
  62. cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".\n";
  63. }
  64.  
  65. //
  66. //Compute discount, savings, and total for 10-19 units
  67. else if (unitsSold >= 10 and unitsSold <= 19) {
  68. discountDecimal += 0.20;
  69. discountPercentage = discountDecimal * 100;
  70. totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
  71. totalSavings = (unitsSold * itemPrice * discountDecimal);
  72.  
  73. cout << "You are receiving a " << discountPercentage << "% discount, saving $"
  74. << fixed << setprecision(2) << totalSavings << ".\n";
  75. cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
  76.  
  77. }
  78.  
  79. //
  80. //Compute discount, savings, and total for 20-49 units
  81. else if (unitsSold > 19 and unitsSold <= 49) {
  82. discountDecimal += 0.30;
  83. discountPercentage = discountDecimal * 100;
  84. totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
  85. totalSavings = (unitsSold * itemPrice * discountDecimal);
  86.  
  87.  
  88. cout << "You are receiving a " << discountPercentage << "% discount, saving $"
  89. << fixed << setprecision(2) << totalSavings << ".\n";
  90. cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
  91.  
  92. }
  93.  
  94. //
  95. //Compute discount, savings, and total for 49-99 units
  96. else if (unitsSold > 49 and unitsSold <= 99) {
  97. discountDecimal += 0.40;
  98. discountPercentage = discountDecimal * 100;
  99. totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
  100. totalSavings = (unitsSold * itemPrice * discountDecimal);
  101.  
  102.  
  103. cout << "You are receiving a " << discountPercentage << "% discount, saving $"
  104. << fixed << setprecision(2) << totalSavings << ".\n";
  105. cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
  106.  
  107. }
  108.  
  109. //
  110. //Compute discount, savings, and total for 99+ units
  111. else if (unitsSold > 99) {
  112. discountDecimal += 0.50;
  113. discountPercentage = discountDecimal * 100;
  114. totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
  115. totalSavings = (unitsSold * itemPrice * discountDecimal);
  116.  
  117.  
  118. cout << "You are receiving a " << discountPercentage << "% discount, saving $"
  119. << fixed << setprecision(2) << totalSavings << ".\n";
  120. cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
  121.  
  122. }
  123.  
  124. //
  125. //Input validation: Display error if quantity < 0
  126. else if (unitsSold <= 0) {
  127. cout << "\nError! Your purchase must be greater than 0 units.";
  128. }
  129.  
  130. return 0;
  131. }
  132.  
  133.  
Success #stdin #stdout 0.01s 5272KB
stdin
12
stdout
Enter the quantity of software packages to purchase: 
You are receiving a 20% discount, saving $237.60.
Your total is $950.40.