Maybe something along the lines of this would be a way for you to do some of it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <string>
// #include <iomanip> // you're not using this yet
using namespace std;
const int PERDAY=2500;
// Func.1: get total by reference (note the ampersand preceeding "total")
void patientBill(float &total, int days, float medCharges, float hospitalServ)
{
total = (days * PERDAY) + (medCharges + hospitalServ);
}
// Func.2: return total via the function's return type
float patientBill(float medCharges, float hospitalServ)
{
float total = (medCharges + hospitalServ);
return total;
}
int main()
{
string patientName;
float medCharges, hospitalServ, total;
cout << "Enter patient name:\t";
getline(cin, patientName);
int x;
cout << "In/Out-patient (1/2):\t";
cin >> x;
switch (x)
{
case 1: {
int days; // probably best as an integer unless you need part-days
cout << "Enter days in hospital:\t"; cin >> days;
cout << "Enter medical charges:\t"; cin >> medCharges;
cout << "Hospital charges:\t"; cin >> hospitalServ;
patientBill(total, days, medCharges, hospitalServ); // uses Function 1
cout << "Total In-patient cost:\t" << total << endl;
break;
}
case 2: {
cout << "Enter medical charges:\t"; cin >> medCharges;
cout << "Hospital charges:\t"; cin >> hospitalServ;
total = patientBill(medCharges, hospitalServ); // uses Function 2
cout << "Total Out-patient cost:\t" << total << endl;
break;
}
default: {
cout << "Something went wrong ... I'm outa here." << endl;
}
} // end switch
return 0;
}
|
Enter patient name: Anonymous
In/Out-patient (1/2): 1
Enter days in hospital: 2
Enter medical charges: 20
Hospital charges: 44
Total In-patient cost: 5064
|
I'm sure there are also many other ways to do it. You'll still need to modify the code to satisfy all requirements of the problem you have. According to the question you also need to validate the input, and possibly you need to format the output more prettily too.
Note the overloaded functions. Each, of course, has the same name, but different parameters. Which function is used will be determined by the arguments passed to it (by the type, and by the number of parameters).
As you can see, the return type on the first function is void (you're modifying the actual "total" variable itself, as it's passed by reference, so no need to return anything); the second function, with the same name, has a return type of float, and returns "total", which is a float, as its result.
In fact, if you wanted to, in case-2 (out-patient), you could simply do this if you wished to:
1 2 3
|
// total = patientBill(medCharges, hospitalServ); // uses Function 2
// cout << "Total Out-patient cost:\t" << total << endl;
cout << "Total Out-patient cost:\t" << patientBill(medCharges, hospitalServ) << endl;
|
FYI. The return type is not one of the considerations in determining which overloaded function is selected; only the number and the type of its parameters determine that (actually, and their order). So long as the parameters are different, you can effectively overload the
patientBill function as many times as you want.
Hope that helps.