In: Computer Science
C++ program
Overloaded
Hospital
Write a c++ program that computes and displays the charges for a
patient’s hospital stay. First, the program should ask if the
patient was admitted as an inpatient or an outpatient. If the
patient was an inpatient, the following data should be entered:
The program should ask for the following data if the patient was an
outpatient:
The program should use two overloaded functions to calculate the
total charges. One of the functions should accept arguments for the
inpatient data, while the other function accepts arguments for
outpatient information. Both functions should return the total
charges.
Input Validation: Do not accept
negative numbers for any data.
#include <iostream>
using namespace std;
float total_charge(int n,float daily_rate,float med_charge,float
hosp_service) // function to compute total charges
{
return n * daily_rate + med_charge + hosp_service;
}
float total_charge(float med_charge,float hosp_service) //
overloading the above function with two arguments
{
return med_charge + hosp_service;
}
int main()
{
char patient_type; // it stores patient type
int n; // number of days spent in hospital
float daily_rate, med_charge, hosp_service;
cout << "Patient was admitted as an inpatient or an
outpatient ? " << endl;
cout << "Press I for inpatient or O for outpatient : ";
cin >> patient_type;
if (patient_type == 'I' || patient_type == 'i') // if patient is
inpatient
{
while (1){
cout << "Enter number of days spent in the hospital :
";
cin >> n;
if (n < 0)
cout << "Invalid Input! try again..." << endl; // if
input is negative then ask user again to enter value
else // else if input is correct then go out of loop
break;
}
while (1){
cout << "Enter daily rate : ";
cin >> daily_rate;
if (daily_rate < 0)
cout << "Invalid Input! try again..." << endl;
else
break;
}
while (1){
cout << "Enter hospital medication charges : ";
cin >> med_charge;
if (med_charge < 0)
cout << "Invalid Input! try again..." << endl;
else
break;
}
while (1){
cout << "Enter charges for hospital services (lab tests,
etc.) : ";
cin >> hosp_service;
if (hosp_service < 0)
cout << "Invalid Input! try again..." << endl;
else
break;
}
cout << "Total charges : " <<
total_charge(n,daily_rate,med_charge,hosp_service); // call
function to compute total charge
}
else if(patient_type == 'O' || patient_type == 'o') // if patient
is outpatient
{
while (1){
cout << "Enter charges for hospital services (lab tests,
etc.) : ";
cin >> hosp_service;
if (hosp_service < 0)
cout << "Invalid Input! try again..." << endl;
else
break;
}
while (1){
cout << "Enter hospital medication charges : ";
cin >> med_charge;
if (med_charge < 0)
cout << "Invalid Input! try again..." << endl;
else
break;
}
cout << "Total charges : " <<
total_charge(med_charge,hosp_service); // call overloaded function
to compute total charge
}
return 0;
}
output 1 :
output 2:
output 3: