Question
Need help making a validity check to pass this code when user input an incorrect in_out option Test Case 2: Do you want to calculate
Need help making a validity check to pass this code when user input an incorrect in_out option
Test Case 2: Do you want to calculate the total hospital bill for (I) an In-Patient, or (O) an Out-Patient?A Enter I or O: A Enter I or O: B Enter I or O: C Enter I or O: o What were the charges for hospital services (lab tests, etc.)? $-30 Number must be a positive number : 30 What were the hospital medication charges? $300
14. Overloaded Hospital Write a program that computes and displays the charges for a patients hospital stay. First, the program should ask if the patient was admitted as an in-patient or an out-patient. Please keep asking the user until the user enters the valid choice. Please refer to the test cases for more details.
If the patient was an in-patient the following data should be entered: The number of days spent in the hospital The daily rate Charges for hospital services (lab tests, etc.) Hospital medication charges. If the patient was an out-patient the following data should be entered: Charges for hospital services (lab tests, etc.) Hospital medication charges. Use a single, separate function to validate that no input is less than zero. If it is, it should be re-entered before being returned. Keep validating the data until the user entersvalid value.
Once the required data has been input and validated, 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 out- patient data. Both functions should return the total charges.
#include
#include
using namespace std;
// Function prototype
double calcData(int, double, double, double);
double calcData(double, double);
int main()
{
char in_out;
int numDays;
double dailyRate,
servicesCharge,
medicationCharge,
totalCharges;
cout << "Do you want to calculate the total hospital bill for " << "(I) an In-Patient, or " << "(O) an Out-Patient? ";
cin >> in_out;
// if (in_out != 'I' && in_out != "O")
// cout << "Enter I or O: ";
// cin >> in_out;
switch(in_out)
{
case 'i' :
case 'I' : do
{
cout << "How many days were spent in the hospital? ";
cin >> numDays;
if (numDays < 0)
{
cout << "Number must be a positive number : " << endl;
}
} while(numDays < 0);
do
{
cout << "What is the hospital daily rate? $";
cin >> dailyRate;
if (dailyRate < 0)
{
cout << "Number must be a positive number : " << endl;
}
} while(dailyRate < 0);
case 'o' :
case 'O' : do
{
cout << "What were the charges for hospital services (lab tests, etc.)? $";
cin >> servicesCharge;
if (servicesCharge < 0)
{
cout << "Number must be a positive number : " << endl;
}
} while (servicesCharge < 0);
do
{
cout << "What were the hospital medication charges? $";
cin >> medicationCharge;
if (medicationCharge < 0)
{
cout << "Number must be a positive number : " << endl;
}
} while(medicationCharge < 0);
}
cout << right << fixed << showpoint << setprecision(2);
switch(in_out)
{
case 'i' :
case 'I' : totalCharges = calcData(numDays, dailyRate, servicesCharge, medicationCharge);
break;
case 'o' :
case 'O' : totalCharges = calcData(servicesCharge, medicationCharge);
}
cout << " Total Hospital Charges :$" << totalCharges << setw(10) ;
}
// accepts the arguments for in-patient data, calculates and returns the total charges.
double calcData(int numDays, double dailyRate, double servicesCharge,
double medicationCharge)
{
return (numDays * dailyRate) + calcData(servicesCharge, medicationCharge);
}
// function accepts the arguments for out-patient data, calculates and returns the total charges. *
double calcData(double servicesCharge, double medicationCharge)
{
return servicesCharge + medicationCharge;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started