Funcions problems in c++

Cant get the catch out of this program, maybe someone can lend me a hand.

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. If the patient was an in-patient the following data should be entered:

-Number of Days spent in a Hospital
-The daily rate
-Charges for the hospital services(lab test, etc)
-Hospital medication charges

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

-Charges for Hospital services(lab test, etc)
-Hospital medication charges

The program should use two 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.


I dont know why but the result always gives me 0???
This is what I have, thank you for your time.

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
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;

double in_patient(int Numdays, double dailyRate, double Charge_hospServices, double HospMed_Charge);
double out_patient(double Outcharge_Service, double OuthospMed_Charge);

int main()
{

double dailyRate, Charge_hospServices,HospMed_Charge,Outcharge_Service,OuthospMed_Charge;

int Numdays;

char response;

cout<<"Enter (Y) if you are an in patient or (N0) if you are an out patient:";
cin>>response;

if (response == 'Y')
{
    cout<<"Number of days spent in a hospital:";
    cin>>Numdays;

    cout<<"The daily rate:";
    cin>>dailyRate;

    cout<<"Charges for the hospital services:";
    cin>>Charge_hospServices;

    cout<<"Hospital Medication Charges:";
    cin>>HospMed_Charge;
}

else if (response == 'N')
{
    cout<<"Charges for the hospital service:";
    cin>>Outcharge_Service;

    cout<<"Hospital medication Charges:";
    cin>>OuthospMed_Charge;
}

if (Numdays<0 || dailyRate<0 || Charge_hospServices<0 || HospMed_Charge<0 || Outcharge_Service<0 || OuthospMed_Charge<0){

    cout<<"Please try again:";
}
}

double in_patient(int Numdays, double dailyRate, double Charge_hospServices, double HospMed_Charge)
{

    int totalCharge;

    totalCharge = (Numdays * dailyRate) + Charge_hospServices + HospMed_Charge;

    return totalCharge;
}

double out_patient(double Outcharge_Service, double OuthospMed_Charge)
{
    int totalCharge;

    totalCharge = (Outcharge_Service+OuthospMed_Charge);

    return totalCharge;

}
if i choose 'Y', then you dont ask for a value for 'Outcharge_Service' or 'OuthospMed_Charge', so line 43 blows up for me.

also int totalCharge; should be defined as double in both of your functions.


wait a minute.. you can't have problems with your functions as you never call them!
Last edited on
Hey buddy, call those functions in main() for execution.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    if (Numdays<0 || dailyRate<0 || Charge_hospServices<0 || HospMed_Charge<0 || Outcharge_Service<0 || OuthospMed_Charge<0){

        cout<<"Please try again:";
    }
//from line47
    double totalCharges = 0.0;
    if (response == 'Y')
    {
        totalCharges = in_patient(Numdays, dailyRate, Charge_hospServices,     HospMed_Charge)
    }
    else if (response == "N")
    {
        totalCharges = out_patient(Outcharge_Service, OuthospMed_Charge)
    }
    cout << "Total Charges: " <<  totalCharges;
    return 0;//end of int main()
)


EDIT: Added return 0;
Last edited on
upX86,

Oohh Wow it works! Thank you soooo much!!!

Can you please explain me to me what I did wrong. Because I thought the functions were supposed to be called after the int main and kindof mix it with the arithmetic operations.

Thank You for your time
Functions don't get called spontaneously. You have to actually write a call to the function in your code, when you want the function to be called.

main() is an exception - it's the entry point to your program, and it's called when you run the program.
Topic archived. No new replies allowed.