fork download
  1. //Devin Scheu CS1A Chapter 6, P. 173, #14
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE HOSPITAL CHARGES
  6. * ____________________________________________________________
  7. * This program determines the total charges for a patient s
  8. * hospital stay based on in-patient or out-patient status.
  9. * ____________________________________________________________
  10. * INPUT
  11. * patientType : The type of patient (in-patient or out-patient)
  12. * daysInHospital : The number of days for in-patient
  13. * dailyRate : The daily rate for in-patient
  14. * medicationCharges : Hospital medication charges
  15. * serviceCharges : Charges for hospital services
  16. *
  17. * OUTPUT
  18. * totalCharges : The total charges for the patient s stay
  19. *
  20. **************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <string>
  25.  
  26. using namespace std;
  27.  
  28. // Overloaded function for in-patient charges
  29. double calculateCharges(int days, double dailyRate, double medicationCharges, double serviceCharges) {
  30. return (days * dailyRate) + medicationCharges + serviceCharges;
  31. }
  32.  
  33. // Overloaded function for out-patient charges
  34. double calculateCharges(double medicationCharges, double serviceCharges) {
  35. return medicationCharges + serviceCharges;
  36. }
  37.  
  38. int main () {
  39.  
  40. //Variable Declarations
  41. string patientType; //INPUT - The type of patient (in-patient or out-patient)
  42. int daysInHospital; //INPUT - The number of days for in-patient
  43. double dailyRate; //INPUT - The daily rate for in-patient
  44. double medicationCharges; //INPUT - Hospital medication charges
  45. double serviceCharges; //INPUT - Charges for hospital services
  46. double totalCharges; //OUTPUT - The total charges for the patient s stay
  47.  
  48. //Prompt for Patient Type and Echo
  49. cout << "Enter patient type (in-patient or out-patient): ";
  50. cin >> patientType;
  51. cout << patientType << endl;
  52.  
  53. //Input Validation and Data Collection
  54. if (patientType == "in-patient" || patientType == "out-patient") {
  55. if (patientType == "in-patient") {
  56. cout << "Enter the number of days in hospital: ";
  57. cin >> daysInHospital;
  58. while (daysInHospital < 0) {
  59. cout << "\nError: Please enter a non-negative number: ";
  60. cin >> daysInHospital;
  61. }
  62. cout << daysInHospital << endl;
  63.  
  64. cout << "Enter the daily rate: $";
  65. cin >> dailyRate;
  66. while (dailyRate < 0) {
  67. cout << "\nError: Please enter a non-negative amount: $";
  68. cin >> dailyRate;
  69. }
  70. cout << dailyRate << endl;
  71. }
  72.  
  73. cout << "Enter hospital medication charges: $";
  74. cin >> medicationCharges;
  75. while (medicationCharges < 0) {
  76. cout << "\nError: Please enter a non-negative amount: $";
  77. cin >> medicationCharges;
  78. }
  79. cout << medicationCharges << endl;
  80.  
  81. cout << "Enter charges for hospital services: $";
  82. cin >> serviceCharges;
  83. while (serviceCharges < 0) {
  84. cout << "\nError: Please enter a non-negative amount: $";
  85. cin >> serviceCharges;
  86. }
  87. cout << serviceCharges << endl;
  88. } else {
  89. cout << "\nError: Please enter 'in-patient' or 'out-patient': ";
  90. cin >> patientType;
  91. cout << patientType << endl;
  92. // Recheck patient type after error
  93. while (patientType != "in-patient" && patientType != "out-patient") {
  94. cout << "\nError: Please enter 'in-patient' or 'out-patient': ";
  95. cin >> patientType;
  96. cout << patientType << endl;
  97. }
  98. // Recollect data if type was corrected
  99. if (patientType == "in-patient") {
  100. cout << "Enter the number of days in hospital: ";
  101. cin >> daysInHospital;
  102. while (daysInHospital < 0) {
  103. cout << "\nError: Please enter a non-negative number: ";
  104. cin >> daysInHospital;
  105. }
  106. cout << daysInHospital << endl;
  107.  
  108. cout << "Enter the daily rate: $";
  109. cin >> dailyRate;
  110. while (dailyRate < 0) {
  111. cout << "\nError: Please enter a non-negative amount: $";
  112. cin >> dailyRate;
  113. }
  114. cout << dailyRate << endl;
  115. }
  116.  
  117. cout << "Enter hospital medication charges: $";
  118. cin >> medicationCharges;
  119. while (medicationCharges < 0) {
  120. cout << "\nError: Please enter a non-negative amount: $";
  121. cin >> medicationCharges;
  122. }
  123. cout << medicationCharges << endl;
  124.  
  125. cout << "Enter charges for hospital services: $";
  126. cin >> serviceCharges;
  127. while (serviceCharges < 0) {
  128. cout << "\nError: Please enter a non-negative amount: $";
  129. cin >> serviceCharges;
  130. }
  131. cout << serviceCharges << endl;
  132. }
  133.  
  134. //Calculate Charges
  135. if (patientType == "in-patient") {
  136. totalCharges = calculateCharges(daysInHospital, dailyRate, medicationCharges, serviceCharges);
  137. } else {
  138. totalCharges = calculateCharges(medicationCharges, serviceCharges);
  139. }
  140.  
  141. //Separator and Output Section
  142. cout << "-------------------------------------------------------" << endl;
  143. cout << "OUTPUT:" << endl;
  144.  
  145. //Output Result
  146. cout << fixed << setprecision(2);
  147. if (patientType == "in-patient") {
  148. cout << left << setw(25) << "Days in Hospital:" << right << setw(15) << daysInHospital << endl;
  149. cout << left << setw(25) << "Daily Rate:" << right << setw(15) << "$" << dailyRate << endl;
  150. }
  151. cout << left << setw(25) << "Medication Charges:" << right << setw(15) << "$" << medicationCharges << endl;
  152. cout << left << setw(25) << "Service Charges:" << right << setw(15) << "$" << serviceCharges << endl;
  153. cout << left << setw(25) << "Total Charges:" << right << setw(15) << "$" << totalCharges << endl;
  154.  
  155. } //end of main()
Success #stdin #stdout 0.01s 5292KB
stdin
in-patient
7
400
700
300
stdout
Enter patient type (in-patient or out-patient): in-patient
Enter the number of days in hospital: 7
Enter the daily rate: $400
Enter hospital medication charges: $700
Enter charges for hospital services: $300
-------------------------------------------------------
OUTPUT:
Days in Hospital:                      7
Daily Rate:                            $400.00
Medication Charges:                    $700.00
Service Charges:                       $300.00
Total Charges:                         $3800.00