#include "stdafx.h"
#include <iostream>
#include <iomanip>
usingnamespace std;
int bill(int);
double bill(double);
int main()
{
// declare and initialize variables
char choice;
int inpatient;
double outpatient;
double bill = 0;
double total = 0;
cout << fixed << showpoint << setprecision(2);
// displaying the menu and get a selection
cout << "Do you want to calculate the charges for a patient's hospital stay?" << endl;
cout << "(I) an in-patient, or ";
cout << "(O) a out-patient? ";
cin >> choice;
while ( choice != 'I' && choice != 'i' &&
choice != 'O' && choice != 'o' )
{
cout << "Please enter I or O: ";
cin >> choice;
}
bill(inpatient);
bill(outpatient);
// return total for in-patient
cout << "Total is: " << bill(inpatient);
// return total for out-patient
cout << "Total is: " << bill(outpatient);
return 0;
}
// defintion of overloaded function bill for in-patient
int bill(int inpatient)
{
double days;
double rate;
double medicine;
double services;
double total = 0;
cout << "How many days did the patient spent at the hospital? ";
cin >> days;
while ( days <= 0 )
{
cout << "Please enter a number greater than ZERO: ";
cin >> days;
}
cout << "What is the daily rate for a night at the hospital? ";
cin >> rate;
while ( rate <= 0 )
{
cout << "Please enter a number greater than ZERO: ";
cin >> rate;
}
cout << "How much is for the medication charges? ";
cin >> medicine;
while ( medicine <= 0 )
{
cout << "Please enter a number greater than ZERO: ";
cin >> medicine;
}
cout << "What is the charge for hospital services? ";
cin >> services;
while ( services <= 0 )
{
cout << "Please enter a number greater than ZERO: ";
cin >> services;
}
total = (days * rate) + medicine + services;
return total;
}
// definition of overloaded function bill for out-patient
double bill(double outpatient)
{
double services;
double medicine;
double total = 0;
cout << "What is the charge for hospital services? ";
cin >> services;
while ( services <= 0 )
{
cout << "Please enter a number greater than ZERO: ";
cin >> services;
}
cout << "How much is for the medication charges? ";
cin >> medicine;
while ( medicine <= 0 )
{
cout << "Please enter a number greater than ZERO: ";
cin >> medicine;
}
total = (services + medicine);
}
return total;
Trying to get both Function to return their total at the same time..somehow it not working
In line 33 you get the user's choice but you do not act upon it.
There is no if-then to decide which function to call.
In line 36 and 37 you are calling the functions which return the total.
The only problem is that you do not assign the value which the function returns to any variable.
This leads to the fact that the function gets executed but the return value is simply lost.
And then you call the functions again. So, the user has to go through inputting the data four times instead of only once.