hospital billing with overloaded functions

This is what the program requires. I'm having a little bit of trouble with understanding how functions and calling them works. I don't just want a solution I would also like some help to understand why

Write a C++ program which computes and displays the charges and average charges for the hospital stays of any number of patients. First, the program should ask the user if the patient was admitted as an inpatient or an out-patient.

If the patient was an in-patient, the following data should be entered:

-patients’ full name

-number of days spent in the hospital

-daily rate

-hospital medication charges

-charges for hospital services (lab tests, MRIs, etc.)

The program should ask for the following data if the patient was an out-patient:

-patient’s full name

-hospital medication charges

-charges for hospital services (lab tests, MRIs, etc.)

The program should use an overloaded function to calculate the total charges. One version of the function should accept arguments for the in-patient data and reference the total charges back to main via a reference variable. The other version should accept arguments for the out-patient data and return the total charges to main via the function’s return type.

The patient’s name and total charges should be displayed in main, and then another patient should be processed.

Also calculate and display in main the average in-patient charge, the average out-patient charge, and the overall average patient charge.

Validate that all numeric data entered is greater than zero.

Provide two screen prints with the following data:

In-patient: name Iam SoSick; 75 days; $2,500 per day; $110,110 med charges; hospital services $13,002.28.

Out-Patient: name Cary Meout; hospital services $21,678.45; $8,974 med charges..

[code]
Put the code you need help with here.
[#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
string patientName;
float bill, dailyRate, days, charges, medCharges, hospitalServ, total, perDay = 2500.00;
int x;


cout << "Enter patient name "; getline(cin, patientName);
cout << "Enter 1 for in Patient or 2 for out patient "; cin >> x;
switch (x)
{
case 1:
cout << "Enter days in hospital "; cin >> days;
cout << "Enter medical charges "; cin >> medCharges;
cout << "Enter hospital service charges "; cin >> hospitalServ;
patientBill(days, medCharges, hospitalServ, perDay, total);

break;
case 2:
{

}






system("pause");
return 0;

}
}

void patientBill(float total, float days, float medCharges, float hospitalServ, float perDay = 2500)
{
total = days * perDay;
total += (medCharges + hospitalServ);
return total;
}


]
Last edited on
You have the function for the case where they are an in-paptient but it returning a value, the bill, but the return type is void.

Once that done there a few ways of creating the bill for out patient.

Create a seperate function, can even use same name 'patientBill' taking in diffrent parameters as if they are an out-patient you wont need things such as float days. Also float days? do you want to record the number of part days or would it just be whole days, days may want to be an int.



edit-Reread your problem, I see you want to return the value by reference in 1 case, in your function you passing it back as return type. You need to pass the float total by reference.

Another problem is you calling the function and your arguments are a diffrent order, total first or last in argument list?

Last edited on
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.
Last edited on
Thank you both so much this really helped me understand functions and how to send/return values . Now I just have to add in the if statements for verification of the input.
Topic archived. No new replies allowed.