In: Computer Science
C++ please show the error mesg if user enter others letter insated of I and O and also numbers must be greater than 0 if user input -, show the error mesg untill user out the correct...thanks
please Overloaded Hospital
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 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 enters valid 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.
my code
#include <iostream>
#include <iomanip>
using namespace std;
void In_Patient_Total(int days, double rate,double service,
double medication);
void Out_Patient_Total(double service,double medication);
int main()
{
char Choise;
int days;
double rate, service, medication;
cout << "Do you want to calculate the total
hospital bill for \n(I)an In - Patient, or \n(O)an Out - Patient ?
";
cin >> Choise;
while (Choise != 'I' && Choise != 'i'
&& Choise != 'o' && Choise != 'O')
{
cout << "Enter I or O:
";
cin >> Choise;
}
if (Choise == 'I' || Choise == 'i') {
cout << "How many
days were spent in the hospital? \n";
cin >> days;
while (days < 0);
cout << "Number must be a positive number : ";
cin >> days;
cout << "What is
the hospital daily rate? $\n";
cin >> rate;
while (rate < 0);
cout << "Number must be a positive number : ";
cin >> rate;
cout << "What were
the charges for hospital services (lab tests, etc.)? $\n";
cin >>
service;
while (service < 0);
cout << "Number must be a positive number : ";
cin >> service;
cout << "What were
the hospital medication charges? $\n";
cin >>
medication;
while (medication < 0);
cout << "Number must be a positive number : ";
cin >> days;
In_Patient_Total(int days, double rate, double service, double
medication);
}
if (Choise == 'O' || Choise == 'o') {
cout << "What were
the charges for hospital services (lab tests, etc.)? $\n";
cin >>
medication;
while (medication < 0);
cout << "Number must be a positive number : ";
cin >> days;
cout << "What were
the hospital medication charges? $\n";
cin >>
service;
while (service < 0);
cout << "Number must be a positive number : ";
cin >> service;
}
}
void In_Patient_Total(int days, double rate, double service, double
medication)
{
return (days * rate) + (service,
medication);
}
void Out_Patient_Total(double service, double medication)
{
return (service + medication);
}
Made some changes to your code but works alright.
#include <iostream>
#include <iomanip>
using namespace std;
double calcData(int, double, double, double); //changed your declarations to a simple form
double calcData(double, double);
int main()
{
char InOut;
int NumOfDays;
double DailyRate,
ServiceCharges,
MedicationCharges,
TotalCharges;
cout << "This program computes and displays a patient’s hospital charges\n" //all regular information displayed to the user
<< "Was patient admitted as an in-patient or an out-patient?\n"
<< "Enter (I) for in-patient\nEnter (O) for out-patient\n";
cin >> InOut; //changed Choise to InOut
switch(InOut) //For better use if conditions are replaced to switch condition
{
case 'i' : //whether the input is i or I
case 'I' : do
{
cout << "How many days were spent in the hospital? ";
cin >> NumOfDays;
if (NumOfDays < 0) //minimum value should be greater than zero
{
cout << "Number of days must be greater than 0.\n";
}
} while(NumOfDays < 0);
do
{
cout << "What was the daily rate? ";
cin >> DailyRate; //entering the daily rate of hospital daily
if (DailyRate < 0)
{
cout << "Daily rate must be greater than 0.\n";
}
} while(DailyRate < 0);
case 'o' : //for case 'O' or 'o'
case 'O' : do
{
cout << "Enter the charges for hospital services "
<< "(lab tests, etc.): ";
cin >> ServiceCharges;
if (ServiceCharges < 0)
{
cout << "Charges for hospital services must be "
"greater than 0.\n";
}
} while (ServiceCharges < 0);
do
{
cout << "Enter the hospital medication charges: ";
cin >> MedicationCharges;
if (MedicationCharges < 0)
{
cout << "Hospital medication charges must be greater"
<< " than 0.\n";
}
} while(MedicationCharges < 0);
}
cout << "\n Patient hospital stay report\n";
cout << right << fixed << showpoint << setprecision(2);
switch(InOut)
{
case 'i' :
case 'I' : TotalCharges = calcData(NumOfDays, DailyRate, ServiceCharges, MedicationCharges);
cout << "Number of days spent in the hospital : "
<< setw(11) << NumOfDays << endl;
cout << "Daily rate : $"
<< setw(10) << DailyRate << endl;
break;
case 'o' :
case 'O' : TotalCharges = calcData(ServiceCharges, MedicationCharges);
}
cout << "Charges for hospital services : $"
<< setw(10) << ServiceCharges << endl;
cout << "Hospital medication charges : $"
<< setw(10) << MedicationCharges << endl;
cout << "Total charges : $"
<< setw(10) << TotalCharges << endl;
}
// calculation of total charges for the given user input
double calcData(int NumOfDays, double DailyRate, double ServiceCharges,
double MedicationCharges)
{
return (NumOfDays * DailyRate) + calcData(ServiceCharges, MedicationCharges);
}
double calcData(double ServiceCharges, double MedicationCharges)
{
return ServiceCharges + MedicationCharges;
}