fork download
  1. //Diego Martinez CSC5 Chapter 4, P.226,#24
  2. /*******************************************************************************
  3. * Determining Best Internet Package
  4. * ______________________________________________________________________________
  5. * This program calculates a customer's monthly Internet bill based on the subsc-
  6. * ription package they selected and the number of hours they used during the
  7. * month. This program also determines whether the customer could save money by
  8. * switching to a different package.
  9. *
  10. * Computation is based on the Formula:
  11. * Package A: Base fee includes 10 hours, extra hours are charged
  12. * Package B: Base fee includes 20 hours, extra hours are charged
  13. * Package C: Flat rate with unlimited access (no extra charges)
  14. *______________________________________________________________________________
  15. * INPUT
  16. * Package Type
  17. * Hours used
  18. *
  19. * OUTPUT
  20. * Total monthly bill
  21. * Any potential savings from switching packages (if applicable)
  22. *******************************************************************************/
  23. #include <iostream>
  24. #include <cctype>
  25. using namespace std;
  26.  
  27. // Function to calculate bill based on package and hours
  28. double calculateBill(char package, int hours) {
  29. double bill = 0.0;
  30.  
  31. switch (package) {
  32. case 'A':
  33. bill = 9.95;
  34. if (hours > 10)
  35. bill += (hours - 10) * 2.0;
  36. break;
  37.  
  38. case 'B':
  39. bill = 14.95;
  40. if (hours > 20)
  41. bill += (hours - 20) * 1.0;
  42. break;
  43.  
  44. case 'C':
  45. bill = 19.95;
  46. break;
  47. }
  48.  
  49. return bill;
  50. }
  51.  
  52. int main() {
  53. char package;
  54. int hours;
  55.  
  56. // Input package
  57. cout << "Enter your package (A, B, or C): ";
  58. cin >> package;
  59. package = toupper(package);
  60.  
  61. // Validate package
  62. if (package != 'A' && package != 'B' && package != 'C') {
  63. cout << "Invalid package selection.\n";
  64. return 1;
  65. }
  66.  
  67. // Input hours
  68. cout << "Enter number of hours used: ";
  69. cin >> hours;
  70.  
  71. // Validate hours
  72. if (hours < 0 || hours > 744) {
  73. cout << "Invalid number of hours. Must be between 0 and 744.\n";
  74. return 1;
  75. }
  76.  
  77. // Calculate current bill
  78. double currentBill = calculateBill(package, hours);
  79.  
  80. // Output current bill
  81. cout << "Your total monthly bill: $" << currentBill << endl;
  82.  
  83. // Calculate and display savings
  84. if (package == 'A') {
  85. double costB = calculateBill('B', hours);
  86. double costC = calculateBill('C', hours);
  87.  
  88. if (costB < currentBill) {
  89. cout << "You would save $" << currentBill - costC
  90. << " by switching to Package C.\n";
  91. }
  92. }
  93. else if (package == 'B') {
  94. double costC = calculateBill('C', hours);
  95.  
  96. if (costC < currentBill) {
  97. cout << "You would save $" << currentBill - costC
  98. << " by switching to Package C.\n";
  99. }
  100. }
  101.  
  102. return 0;
  103. }
Success #stdin #stdout 0s 5316KB
stdin
A
700
stdout
Enter your package (A, B, or C): Enter number of hours used: Your total monthly bill: $1389.95
You would save $1370 by switching to Package C.