fork download
  1. // Elaine Torrez CS1A Chapter 6, P.373, #4
  2. // *****************************************************************************************
  3. // * OVERLOADED HOSPITAL *
  4. // *--------------------------------------------------------------------------------------- *
  5. // * This program computes and displays the total hospital charges for a patient based on *
  6. // * whether they were admitted as an in-patient or an out-patient. *
  7. // * The program uses two overloaded functions to calculate the total charges: *
  8. // * 1) For in-patients: accepts number of days, daily rate, medication, and service fees. *
  9. // * 2) For out-patients: accepts medication and service fees only. *
  10. // *--------------------------------------------------------------------------------------- *
  11. // * INPUT *
  12. // * patientType : 'I' or 'O' to indicate in-patient or out-patient *
  13. // * days : number of days spent in hospital (for in-patient only, >= 0) *
  14. // * dailyRate : rate per day (for in-patient only, >= 0) *
  15. // * medCharges : medication charges (>= 0) *
  16. // * servCharges : service/lab test charges (>= 0) *
  17. // *--------------------------------------------------------------------------------------- *
  18. // * OUTPUT *
  19. // * totalCharges : total cost of hospital stay *
  20. // *****************************************************************************************
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <limits> // for input validation
  25. #include <cctype> // for toupper
  26. using namespace std;
  27.  
  28. // ---------------- Function Prototypes ----------------
  29. double calcCharges(int days, double dailyRate, double medCharges, double servCharges); // In-patient
  30. double calcCharges(double medCharges, double servCharges); // Out-patient
  31.  
  32. // ---------------------- MAIN -------------------------
  33. int main()
  34. {
  35. char patientType;
  36. int days;
  37. double dailyRate, medCharges, servCharges, totalCharges;
  38.  
  39. cout << fixed << setprecision(2);
  40.  
  41. // ---------------------- INPUT ----------------------
  42. cout << "Was the patient admitted as an in-patient or out-patient? (I/O): ";
  43. cin >> patientType;
  44. patientType = toupper(patientType);
  45.  
  46. while (patientType != 'I' && patientType != 'O')
  47. {
  48. cout << "ERROR: Please enter 'I' for in-patient or 'O' for out-patient: ";
  49. cin >> patientType;
  50. patientType = toupper(patientType);
  51. }
  52.  
  53. if (patientType == 'I')
  54. {
  55. cout << "Enter number of days spent in the hospital: ";
  56. cin >> days;
  57. while (cin.fail() || days < 0)
  58. {
  59. cin.clear();
  60. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  61. cout << "ERROR: Days cannot be negative. Re-enter: ";
  62. cin >> days;
  63. }
  64.  
  65. cout << "Enter daily rate: $";
  66. cin >> dailyRate;
  67. while (cin.fail() || dailyRate < 0)
  68. {
  69. cin.clear();
  70. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  71. cout << "ERROR: Daily rate cannot be negative. Re-enter: $";
  72. cin >> dailyRate;
  73. }
  74.  
  75. cout << "Enter hospital medication charges: $";
  76. cin >> medCharges;
  77. while (cin.fail() || medCharges < 0)
  78. {
  79. cin.clear();
  80. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  81. cout << "ERROR: Medication charges cannot be negative. Re-enter: $";
  82. cin >> medCharges;
  83. }
  84.  
  85. cout << "Enter charges for hospital services (lab tests, etc.): $";
  86. cin >> servCharges;
  87. while (cin.fail() || servCharges < 0)
  88. {
  89. cin.clear();
  90. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  91. cout << "ERROR: Service charges cannot be negative. Re-enter: $";
  92. cin >> servCharges;
  93. }
  94.  
  95. // -------------------- PROCESS --------------------
  96. totalCharges = calcCharges(days, dailyRate, medCharges, servCharges);
  97. }
  98. else
  99. {
  100. cout << "Enter charges for hospital services (lab tests, etc.): $";
  101. cin >> servCharges;
  102. while (cin.fail() || servCharges < 0)
  103. {
  104. cin.clear();
  105. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  106. cout << "ERROR: Service charges cannot be negative. Re-enter: $";
  107. cin >> servCharges;
  108. }
  109.  
  110. cout << "Enter hospital medication charges: $";
  111. cin >> medCharges;
  112. while (cin.fail() || medCharges < 0)
  113. {
  114. cin.clear();
  115. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  116. cout << "ERROR: Medication charges cannot be negative. Re-enter: $";
  117. cin >> medCharges;
  118. }
  119.  
  120. // -------------------- PROCESS --------------------
  121. totalCharges = calcCharges(medCharges, servCharges);
  122. }
  123.  
  124. // ---------------------- OUTPUT ----------------------
  125. cout << "\nTotal hospital charges: $" << totalCharges << endl;
  126.  
  127. return 0;
  128. }
  129.  
  130. // ---------------- Function Definitions ----------------
  131.  
  132. // For in-patients
  133. double calcCharges(int days, double dailyRate, double medCharges, double servCharges)
  134. {
  135. return (days * dailyRate) + medCharges + servCharges;
  136. }
  137.  
  138. // For out-patients
  139. double calcCharges(double medCharges, double servCharges)
  140. {
  141. return medCharges + servCharges;
  142. }
  143.  
Success #stdin #stdout 0.01s 5276KB
stdin
I
3
500
150
250
stdout
Was the patient admitted as an in-patient or out-patient? (I/O): Enter number of days spent in the hospital: Enter daily rate: $Enter hospital medication charges: $Enter charges for hospital services (lab tests, etc.): $
Total hospital charges: $1900.00