//Zachary Abdollahi CS1A Chapter 6, P. 373, #14
//
/*******************************************************************************
*
* COMPUTE HOSPITAL CHARGES
*
* This program computes and displays the total charges for a patient's
* hospital stay. It handles both in-patients and out-patients using
* overloaded functions.
*
* Computations:
* In-patient = (days * dailyRate) + medication + services
* Out-patient = medication + services
*
* INPUT
* patientType : Choice between in-patient (1) or out-patient (2)
* days : Number of days spent in the hospital
* dailyRate : Daily room rate
* medCharges : Hospital medication charges
* servCharges : Charges for hospital services (labs, etc.)
*
* OUTPUT
* totalCharges : Total calculated charges for patient
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
double calcTotalCharges(int days, double dailyRate, double medCharges,
double servCharges);
double calcTotalCharges(double medCharges, double servCharges);
double getValidNumber(string prompt);
int getValidInt(string prompt);
int main()
{
int patientType;
int days;
double dailyRate;
double medCharges;
double servCharges;
double totalCharges;
// Ask if patient was in-patient or out-patient
cout << "Hospital Billing System\n";
cout << "1. In-Patient\n";
cout << "2. Out-Patient\n";
cout << "Enter patient type (1 or 2): ";
cin >> patientType;
// Validate patient type input
while (patientType != 1 && patientType != 2)
{
cout << "Invalid choice. Please enter 1 for In-Patient or 2 for Out-Patient: ";
cin >> patientType;
}
// Process based on patient type
if (patientType == 1)
{
// Get in-patient data with input validation
days = getValidInt("Enter number of days spent in the hospital: ");
dailyRate = getValidNumber("Enter the daily rate ($): ");
medCharges = getValidNumber("Enter hospital medication charges ($): ");
servCharges = getValidNumber("Enter charges for hospital services ($): ");
//Call overloaded function for in-patient
totalCharges = calcTotalCharges(days, dailyRate, medCharges, servCharges);
}
else
{
// Get out-patient data with input validation
servCharges = getValidNumber("Enter charges for hospital services ($): ");
medCharges = getValidNumber("Enter hospital medication charges ($): ");
// Call overloaded function for out-patient
totalCharges = calcTotalCharges(medCharges, servCharges);
}
// Display formatted output
cout << fixed << showpoint << setprecision(2);
cout << "\n-------------------------------" << endl;
cout << "Total Hospital Charges: $" << totalCharges << endl;
cout << "-------------------------------" << endl;
return 0;
}
/******************************************************************************
* calcTotalCharges (In-Patient) *
* Overloaded function that calculates total charges for an in-patient stay. *
*****************************************************************************/
double calcTotalCharges(int days, double dailyRate, double medCharges,
double servCharges)
{
return (days * dailyRate) + medCharges + servCharges;
}
/******************************************************************************
* calcTotalCharges (Out-Patient) *
* Overloaded function that calculates total charges for an out-patient visit.*
*****************************************************************************/
double calcTotalCharges(double medCharges, double servCharges)
{
return medCharges + servCharges;
}
/*****************************************************************************
* getValidNumber *
* Prompts the user for a double value and rejects negative numbers *
****************************************************************************/
double getValidNumber(string prompt)
{
double value;
cout << prompt;
cin >> value;
while (value < 0)
{
cout << "Error: Value cannot be negative. " << prompt;
cin >> value;
}
return value;
}
/****************************************************************************
* getValidInt *
* Prompts the user for an integer value and rejects negative numbers *
***************************************************************************/
int getValidInt(string prompt)
{
int value;
cout << prompt;
cin >> value;
while (value <0)
{
cout << "Error: Value cannot be negative " << prompt;
cin >> value;
}
return value;
}