fork download
  1. //Diego Martinez CSC5 Chapter 4, P.226,#25
  2. /*******************************************************************************
  3. * VALIDATING INTERNET SERVICE HOURS
  4. * ______________________________________________________________________________
  5. * This program enhances input validation by asking the user to enter the month
  6. * and ensures that the number of hours entered does not exceed the maximum
  7. * possible hours for that specific month.
  8. *
  9. *
  10. * Computation is based on the Formula:
  11. * maxHours : 744 : if month has 31 days
  12. * : 720 : if months has 30 days
  13. * : 672 : if month is February (28 days)
  14. *______________________________________________________________________________
  15. * INPUT
  16. * Package Type
  17. * Month name
  18. * Hours used
  19. *
  20. * OUTPUT
  21. * Final bill amount
  22. *******************************************************************************/
  23. #include <iostream>
  24. #include <string>
  25. #include <algorithm>
  26. using namespace std;
  27.  
  28. // Function to convert string to lowercase
  29. string toLower(string str) {
  30. transform(str.begin(), str.end(), str.begin(), ::tolower);
  31. return str;
  32. }
  33.  
  34. // Function to get max hours based on month
  35. int getMaxHours(string month) {
  36. month = toLower(month);
  37.  
  38. if (month == "january" || month == "march" || month == "may" ||
  39. month == "july" || month == "august" || month == "october" ||
  40. month == "december")
  41. return 744;
  42. else if (month == "april" || month == "june" ||
  43. month == "september" || month == "november")
  44. return 720;
  45. else if (month == "february")
  46. return 672;
  47. else
  48. return -1; // invalid month
  49. }
  50.  
  51. int main() {
  52. char package;
  53. int hours;
  54. string month;
  55. double bill = 0.0;
  56.  
  57. // Input package
  58. cout << "Enter your package (A, B, or C): ";
  59. cin >> package;
  60. package = toupper(package);
  61.  
  62. if (package != 'A' && package != 'B' && package != 'C') {
  63. cout << "Invalid package selection.\n";
  64. return 1;
  65. }
  66.  
  67. // Input month
  68. cout << "Enter the month name: ";
  69. cin >> month;
  70.  
  71. int maxHours = getMaxHours(month);
  72.  
  73. if (maxHours == -1) {
  74. cout << "Invalid month entered.\n";
  75. return 1;
  76. }
  77.  
  78. //Input hours used
  79. cout << "Enter number of hours used: ";
  80. cin >> hours;
  81.  
  82. if (hours < 0 || hours > maxHours) {
  83. cout << "Invalid number of hours for " << month
  84. << ". Maximum allowed is " << maxHours << " hours.\n";
  85. return 1;
  86. }
  87.  
  88. // Calculate Bill
  89. switch (package) {
  90. case 'A':
  91. bill = 9.95;
  92. if (hours > 10)
  93. bill += (hours -10) * 2.0;
  94. break;
  95.  
  96. case 'B':
  97. bill = 14.95;
  98. if (hours > 20)
  99. bill += (hours - 20) * 1.0;
  100. break;
  101.  
  102. case 'C':
  103. bill = 19.95;
  104. break;
  105. }
  106.  
  107. // Output result
  108. cout << "Your total bill for " << month << " is: $" << bill << endl;
  109.  
  110. return 0;
  111. }
Success #stdin #stdout 0s 5324KB
stdin
B
february
672
stdout
Enter your package (A, B, or C): Enter the month name: Enter number of hours used: Your total bill for february is: $666.95