Write the following c++ program. Write a program that computes and displays the
ID: 3675426 • Letter: W
Question
Write the following c++ program.
Write a 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 in-patient or out-patient. Data entered for in-patient: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) Data entered for out-patients: Hospital medication charges Charges for hospital services (lab tests, etc.) The program should use two overloaded functions to calculate the total charges. One of the functions should accept arguments for the in-patient data, while the other function accepts arguments for the out-patient information. Both functions should return the total charges. The main function prints the total charges. The program will continue to process patient data for as long as the user indicates his/her intent to continue. Input Validation: Do not accept negative numbers for any data. Please pay attention to the input validation.Explanation / Answer
#include<iostream>
using namespace std;
//function to calculate total charge for in-patient
double calculateCharge(int days, double dailyRate, double medicationCharge, double serviceCharge){
double totalCharge = 0;
totalCharge = (days*dailyRate) + medicationCharge + serviceCharge;
return totalCharge;
}
//function to calculate total charge for out-patient
double calculateCharge(double medicationCharge, double serviceCharge){
double totalCharge = 0;
totalCharge = medicationCharge + serviceCharge;
return totalCharge;
}
int main(){
char c;
int days;
char typeOfPatient;
double dailyRate;
double medicationCharge;
double serviceCharge;
double totalCharge;
do{
cout<<"Enter type of patient [ I - for in-patient, O - for outpatient]"<<endl;
cin>>typeOfPatient;
totalCharge = 0;
switch(typeOfPatient){
//in-patient
case 'I':
// reading number of days
while(true){
cout<<"Enter number of days : ";
cin>>days;
if(days < 0)
cout<<"Days can not be negative ";
else
break;
}
// reading daily rate
while(true){
cout<<"Enter daily rate : ";
cin>>dailyRate;
if(dailyRate < 0)
cout<<"Daily rate can not be negative ";
else
break;
}
// reading medicationCharge
while(true){
cout<<"Enter medication charge : ";
cin>>medicationCharge;
if(medicationCharge < 0)
cout<<"Medication charge can not be negative ";
else
break;
}
// reading serviceCharge
while(true){
cout<<"Enter hospital service charge : ";
cin>>serviceCharge;
if(serviceCharge < 0)
cout<<"Meditation charge can not be negative ";
else
break;
}
// getting total charge
totalCharge = calculateCharge(days, dailyRate, medicationCharge, serviceCharge);
cout<<"Total charge : "<<totalCharge<<endl;
break;
// out-patient
case 'O':
// reading medicationCharge
while(true){
cout<<"Enter medication charge : ";
cin>>medicationCharge;
if(medicationCharge < 0)
cout<<"Medication charge can not be negative ";
else
break;
}
// reading serviceCharge
while(true){
cout<<"Enter hospital service charge : ";
cin>>serviceCharge;
if(serviceCharge < 0)
cout<<"Meditation charge can not be negative ";
else
break;
}
// getting total charge
totalCharge = calculateCharge(medicationCharge, serviceCharge);
cout<<"Total charge : "<<totalCharge<<endl;
break;
default:
cout<<"invalid type of patient ";
}
cout<<"Do you want to continue, press any key: (E for exit):";
cin>>c;
}while(c != 'E');
return 0;
}
/*
output:
Enter type of patient [ I - for in-patient, O - for outpatient]
I
Enter number of days : 4
Enter daily rate : 890
Enter medication charge : 324.56
Enter hospital service charge : 500
Total charge : 4384.56
Do you want to continue, press any key: (E for exit):d
Enter type of patient [ I - for in-patient, O - for outpatient]
O
Enter medication charge : 890
Enter hospital service charge : 456.50
Total charge : 1346.5
Do you want to continue, press any key: (E for exit):E
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.