fork download
  1. //Zachary Abdollahi CS1A Chapter 6, P. 373, #14
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE HOSPITAL CHARGES
  6.  *
  7.  * This program computes and displays the total charges for a patient's
  8.  * hospital stay. It handles both in-patients and out-patients using
  9.  * overloaded functions.
  10.  *
  11.  * Computations:
  12.  * In-patient = (days * dailyRate) + medication + services
  13.  * Out-patient = medication + services
  14.  *
  15.  * INPUT
  16.  * patientType : Choice between in-patient (1) or out-patient (2)
  17.  * days : Number of days spent in the hospital
  18.  * dailyRate : Daily room rate
  19.  * medCharges : Hospital medication charges
  20.  * servCharges : Charges for hospital services (labs, etc.)
  21.  *
  22.  * OUTPUT
  23.  * totalCharges : Total calculated charges for patient
  24.  *
  25.  ******************************************************************************/
  26.  
  27. #include <iostream>
  28. #include <iomanip>
  29. using namespace std;
  30.  
  31. // Function Prototypes
  32. double calcTotalCharges(int days, double dailyRate, double medCharges,
  33. double servCharges);
  34. double calcTotalCharges(double medCharges, double servCharges);
  35. double getValidNumber(string prompt);
  36. int getValidInt(string prompt);
  37.  
  38. int main()
  39. {
  40. int patientType;
  41. int days;
  42. double dailyRate;
  43. double medCharges;
  44. double servCharges;
  45. double totalCharges;
  46.  
  47. // Ask if patient was in-patient or out-patient
  48. cout << "Hospital Billing System\n";
  49. cout << "1. In-Patient\n";
  50. cout << "2. Out-Patient\n";
  51. cout << "Enter patient type (1 or 2): ";
  52. cin >> patientType;
  53.  
  54. // Validate patient type input
  55. while (patientType != 1 && patientType != 2)
  56. {
  57. cout << "Invalid choice. Please enter 1 for In-Patient or 2 for Out-Patient: ";
  58. cin >> patientType;
  59. }
  60. // Process based on patient type
  61. if (patientType == 1)
  62. {
  63. // Get in-patient data with input validation
  64. days = getValidInt("Enter number of days spent in the hospital: ");
  65. dailyRate = getValidNumber("Enter the daily rate ($): ");
  66. medCharges = getValidNumber("Enter hospital medication charges ($): ");
  67. servCharges = getValidNumber("Enter charges for hospital services ($): ");
  68.  
  69. //Call overloaded function for in-patient
  70. totalCharges = calcTotalCharges(days, dailyRate, medCharges, servCharges);
  71. }
  72. else
  73. {
  74. // Get out-patient data with input validation
  75. servCharges = getValidNumber("Enter charges for hospital services ($): ");
  76. medCharges = getValidNumber("Enter hospital medication charges ($): ");
  77.  
  78. // Call overloaded function for out-patient
  79. totalCharges = calcTotalCharges(medCharges, servCharges);
  80. }
  81.  
  82. // Display formatted output
  83. cout << fixed << showpoint << setprecision(2);
  84. cout << "\n-------------------------------" << endl;
  85. cout << "Total Hospital Charges: $" << totalCharges << endl;
  86. cout << "-------------------------------" << endl;
  87.  
  88. return 0;
  89. }
  90.  
  91. /******************************************************************************
  92.   * calcTotalCharges (In-Patient) *
  93.   * Overloaded function that calculates total charges for an in-patient stay. *
  94.   *****************************************************************************/
  95. double calcTotalCharges(int days, double dailyRate, double medCharges,
  96. double servCharges)
  97. {
  98. return (days * dailyRate) + medCharges + servCharges;
  99. }
  100.  
  101. /******************************************************************************
  102.   * calcTotalCharges (Out-Patient) *
  103.   * Overloaded function that calculates total charges for an out-patient visit.*
  104.   *****************************************************************************/
  105. double calcTotalCharges(double medCharges, double servCharges)
  106. {
  107. return medCharges + servCharges;
  108. }
  109.  
  110. /*****************************************************************************
  111.   * getValidNumber *
  112.   * Prompts the user for a double value and rejects negative numbers *
  113.   ****************************************************************************/
  114. double getValidNumber(string prompt)
  115. {
  116. double value;
  117. cout << prompt;
  118. cin >> value;
  119.  
  120. while (value < 0)
  121. {
  122. cout << "Error: Value cannot be negative. " << prompt;
  123. cin >> value;
  124. }
  125. return value;
  126. }
  127.  
  128. /****************************************************************************
  129.   * getValidInt *
  130.   * Prompts the user for an integer value and rejects negative numbers *
  131.   ***************************************************************************/
  132. int getValidInt(string prompt)
  133. {
  134. int value;
  135. cout << prompt;
  136. cin >> value;
  137.  
  138. while (value <0)
  139. {
  140. cout << "Error: Value cannot be negative " << prompt;
  141. cin >> value;
  142. }
  143. return value;
  144. }
Success #stdin #stdout 0s 5316KB
stdin
1
4
350
125.50
85.25
stdout
Hospital Billing System
1. In-Patient
2. Out-Patient
Enter patient type (1 or 2): Enter number of days spent in the hospital: Enter the daily rate ($): Enter hospital medication charges ($): Enter charges for hospital services ($): 
-------------------------------
Total Hospital Charges: $1610.75
-------------------------------