fuction not working

after i was able to get my other program to run with everyone's input I decided to try a different one to get a better understanding of function programming. I current objective is to write a program that computes and displays the charges for a patient’s hospital stay.

The program design is:

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:

• The number of days spent in the hospital
• The daily rate
• Hospital medication charges
• Charges for hospital services (lab tests, etc.)

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

• Charges for hospital services (lab tests, etc.)
• Hospital medication charges 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 information. Both functions should return the total charges. Input Validation: Do not accept negative numbers for any data.

The issues I am having when I attempt to complie state:

too few arguments to function `double getInPatient(int, double, double, double)'

too few arguments to function `double getOutPatient(double, double)'

it also states varibles that are declared as undeclared.

ambiguates old declaration `double getInPatient(int, double, double, double)'
ambiguates old declaration `double getOutPatient(double, double)'

I do believe if I can get past the too few arguments to function issues the rest might be taken of. Thank you in advance to all who come to my aid.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// *****************************************************************************
// Program:  Overloaded Hospital 
// Purpose: program that computes and displays the charges for a patient's 
//          hospital stay. 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: The number of days spent in the hospital, The 
//          daily rate, Hospital medication charges, and Charges for hospital 
//          services (lab tests, etc.) ask for the following data if the patient
//          was an out-patient: Charges for hospital services (lab tests, etc.)
//          and Hospital medication charges. Input Validation: Do not accept 
//          negative numbers for any data.
// *****************************************************************************

# include <iostream>
using namespace std;

double getInPatient(int numberofDays, double dailyRate, double hospitalMedicationCharge, double chargesForHospitalServices);
double getOutPatient(double hospitalMedicationCharge, double chargesForHospitalServices);

int main()
{
    char patientStatus;
    
    cout << "Is the patient an In-patient or Out-patient: ";
    cin >> patientStatus;
    
    while (patientStatus !='I' || patientStatus !='i'
           || (patientStatus != 'O' || patientStatus != 'o'))
    {
        cout << "ERROR! Please enter one of the following (I,i O, o): ";
        cin >> patientStatus;
    }
    
    if (patientStatus == 'I' || patientStatus == 'i')
    {
        getInPatient();
    }
    else
    {
        getOutPatient();
    }

    cout << "Patient Status is: " << patientStatus << endl;
    
    if (patientStatus == 'I' || patientStatus == 'i')
    {
        cout << "Patient was in for " << numberofDays << " days.\n";
        cout << "Patient has accumulated " <<  dailyRate << " for the stay.\n";
        cout << "Patient has accumulated " <<  hospitalMedicationCharge 
             << " in medication charges for the stay.\n";
        cout << "Patient has accumulated " <<  chargesForHospitalServices 
             << " in charges for hospital related services for the stay.\n";
    }
    else
    {
        cout << "Patient has accumulated " <<  hospitalMedicationCharge 
             << " in medication charges for the stay.\n";
        cout << "Patient has accumulated " <<  chargesForHospitalServices 
             << " in charges for hospital related services for the stay.\n";
    }       
   system("pause");
   return 0;

}
    // Get the in-patient's data
    void getInPatient(int numberofDays, double dailyRate,
                     double hospitalMedicationCharge,
                     double chargesForHospitalServices)
    {
        
        cout << "Please enter the number of days spent in the hospital: ";
        cin >> numberofDays;
        
        while (numberofDays < 0)
        {
            cout << "ERROR! In-patients can not have a negative numbers of days"
                 << ". Try again: ";
            cin >> numberofDays;
        } 
        cout <<"\nPlease enter the daily rate of the in-patient: ";
        cin >> dailyRate;
        
        while (dailyRate < 0)
        {
            cout << "ERROR! In-patients can not have a negative daily rate"
                 << ". Try again: ";
            cin >> dailyRate;
        }
        
        cout << "\nPlease enter the Hospital medication charges of the" 
             <<" in-patient: ";
        cin >> hospitalMedicationCharge;
        
        while (hospitalMedicationCharge < 0)
        {
            cout << "ERROR! Hospital medication charges can not have a negative"
                 << " value. Try again: ";
            cin >> hospitalMedicationCharge;
        } 
        
        cout <<"\nPlease enter the Charges for hospital services "
             <<"(lab tests, etc.) of the in-patient: ";
        cin >> chargesForHospitalServices;
        
        while (chargesForHospitalServices < 0)
        {
            cout << "ERROR! In-patients can not have a negative charges for"
                 << " hospital services. Try again: ";
            cin >> chargesForHospitalServices;
        }         
    
    } 

    // Get the out-patient data
    void getOutPatient(double hospitalMedicationCharge,
                       double chargesForHospitalServices)
    {
                cout << "\nPlease enter the Hospital medication charges of the" 
             <<" in-patient: ";
        cin >> hospitalMedicationCharge;
        
        while (hospitalMedicationCharge < 0)
        {
            cout << "ERROR! Hospital medication charges can not have a negative"
                 << " value. Try again: ";
            cin >> hospitalMedicationCharge;
        } 
        
        cout <<"\nPlease enter the Charges for hospital services "
             <<"(lab tests, etc.) of the in-patient: ";
        cin >> chargesForHospitalServices;
        
        while (chargesForHospitalServices < 0)
        {
            cout << "ERROR! In-patients can not have a negative charges for"
                 << " hospital services. Try again: ";
            cin >> chargesForHospitalServices;
        }         

    } 


The first errors are because when you call your functions you don't pass any parameters.

The second error is because when you write your function definitions, you're writing them as void function, not double returning function. This is why there's ambiguity in your program; effectively two different functions with the same name.
Thank you iHutch105,
I was able to solve the issues you pointed out or at least I think I did. I am hopeful the the returns will work correctly at this point. I have two issues left to work out and I sure exacty what more to include to get the correct number arguments to the functions prototypes.

too few arguments to function `double getInPatient(int, double, double, double)'
too few arguments to function `double getOutPatient(double, double)'

because of not having enough arguments for it the complier kicks out. I would appreciate any advice you can give me improve my level of understanding how to setup the prototypes correctly. I have also reposted my code so you can view and make your recommendations .
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// *****************************************************************************
// Program:  Overloaded Hospital part 1
// Purpose: program that computes and displays the charges for a patient's 
//          hospital stay. 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: The number of days spent in the hospital, The 
//          daily rate, Hospital medication charges, and Charges for hospital 
//          services (lab tests, etc.) ask for the following data if the patient
//          was an out-patient: Charges for hospital services (lab tests, etc.)
//          and Hospital medication charges. Input Validation: Do not accept 
//          negative numbers for any data.
// *****************************************************************************

# include <iostream>
using namespace std;

double getInPatient(int numberofDays, double dailyRate, double hospitalMedicationCharge, double chargesForHospitalServices);
double getOutPatient(double hospitalMedicationCharge, double chargesForHospitalServices);

int main()
{
    char patientStatus;
    int numberofDays =0;
    double dailyRate =0,
           hospitalMedicationCharge =0,
           chargesForHospitalServices =0;
           
    cout << "Is the patient an In-patient or Out-patient: ";
    cin >> patientStatus;
    
    while (patientStatus !='I' || patientStatus !='i'
           || (patientStatus != 'O' || patientStatus != 'o'))
    {
        cout << "ERROR! Please enter one of the following (I,i O, o): ";
        cin >> patientStatus;
    }
    
    if (patientStatus == 'I' || patientStatus == 'i')
    {
        getInPatient();
    }
    else
    {
        getOutPatient();
    }

    cout << "Patient Status is: " << patientStatus << endl;
    
    if (patientStatus == 'I' || patientStatus == 'i')
    {
        cout << "Patient was in for " << numberofDays << " days.\n";
        cout << "Patient has accumulated " <<  dailyRate << " for the stay.\n";
        cout << "Patient has accumulated " <<  hospitalMedicationCharge 
             << " in medication charges for the stay.\n";
        cout << "Patient has accumulated " <<  chargesForHospitalServices 
             << " in charges for hospital related services for the stay.\n";
    }
    else
    {
        cout << "Patient has accumulated " <<  hospitalMedicationCharge 
             << " in medication charges for the stay.\n";
        cout << "Patient has accumulated " <<  chargesForHospitalServices 
             << " in charges for hospital related services for the stay.\n";
    }       
   system("pause");
   return 0;

}
    // Get the in-patient's data
    double getInPatient(int numberofDays, double dailyRate,
                     double hospitalMedicationCharge,
                     double chargesForHospitalServices)
    {
        
        cout << "Please enter the number of days spent in the hospital: ";
        cin >> numberofDays;
        
        while (numberofDays < 0)
        {
            cout << "ERROR! In-patients can not have a negative numbers of days"
                 << ". Try again: ";
            cin >> numberofDays;
        } 
        cout <<"\nPlease enter the daily rate of the in-patient: ";
        cin >> dailyRate;
        
        while (dailyRate < 0)
        {
            cout << "ERROR! In-patients can not have a negative daily rate"
                 << ". Try again: ";
            cin >> dailyRate;
        }
        
        cout << "\nPlease enter the Hospital medication charges of the" 
             <<" in-patient: ";
        cin >> hospitalMedicationCharge;
        
        while (hospitalMedicationCharge < 0)
        {
            cout << "ERROR! Hospital medication charges can not have a negative"
                 << " value. Try again: ";
            cin >> hospitalMedicationCharge;
        } 
        
        cout <<"\nPlease enter the Charges for hospital services "
             <<"(lab tests, etc.) of the in-patient: ";
        cin >> chargesForHospitalServices;
        
        while (chargesForHospitalServices < 0)
        {
            cout << "ERROR! In-patients can not have a negative charges for"
                 << " hospital services. Try again: ";
            cin >> chargesForHospitalServices;
        }         
        return (numberofDays,dailyRate,hospitalMedicationCharge,chargesForHospitalServices);
    } 

    // Get the out-patient data
    double getOutPatient(double hospitalMedicationCharge,
                       double chargesForHospitalServices)
    {
                cout << "\nPlease enter the Hospital medication charges of the" 
             <<" in-patient: ";
        cin >> hospitalMedicationCharge;
        
        while (hospitalMedicationCharge < 0)
        {
            cout << "ERROR! Hospital medication charges can not have a negative"
                 << " value. Try again: ";
            cin >> hospitalMedicationCharge;
        } 
        
        cout <<"\nPlease enter the Charges for hospital services "
             <<"(lab tests, etc.) of the in-patient: ";
        cin >> chargesForHospitalServices;
        
        while (chargesForHospitalServices < 0)
        {
            cout << "ERROR! In-patients can not have a negative charges for"
                 << " hospital services. Try again: ";
            cin >> chargesForHospitalServices;
        }         
        return(hospitalMedicationCharge,chargesForHospitalServices);
    } 
A prototype is just a forward declaration of a function. It basically lets your program know that the function exists, thus allowing it to be called without writing the definition somewhere above the call (remember, C++ is a sequential language).

Your functions and prototypes match up fine here.

The issue is with the calls to your functions and the return values from those functions. Your functions take parameters, values that get passed into them for use within the functions, which are declared in the function declarations and definitions. In your case, getInPatient takes an integer and three doubles and getOutPatient takes two doubles. So to call these functions, you need to pass to them what they're expecting:

1
2
3
4
5
6
7
8
// Example values
int a = 1;
double b = 1;
double c = 2;
double d = 3;
double result;

result = getInPatient(a,b,c,d);


That code there will take whatever is returned by the function and store it in the result variable. Which brings us to the second issue; your function returns.

You're returning a bunch of variables in the return statement of your function. Functions can only return one value this way. Your compiler may allow it but all your going to get returned is the last value in the list you're returning.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int MyFunc(int a, int b)
{
   int result_1 = a+b;
   int result_2 = a-b;

   return(result_1, result_2);  // The only value returned will be result_2
}

// This is a more viable set of functions
int Add(int a, int b)
{
   return a + b;
}

int Subtract(int a, int b)
{
   return a - b;
}


If you want to change more than one value in a function, you need to pass by reference. When you pass by reference, you're passing the address of an object, rather than the value of the object. The difference is that the function uses the object located at that address, rather than a copy of that object. Run this example to see what I mean:

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
#include <iostream>

using namespace std;

void ByValue(int a) // Parameter passed by value.
{
   a += 10;  // Add 10 to 'a'
}

void ByRef(int &a) // Parameter passed by reference.  Note the & operator.
{
   a += 10; // Add 10 to 'a'
}

int main()
{
   int x = 1;
   cout << x << endl;  // Outputs 1

   ByValue(x);  // Call ByValue
   cout << x << endl;  // Output x after ByValue (it's still 1)

   ByRef(x);  // Call ByRef
   cout << x << endl; // Output x after ByRef (it's now 11)

   return 0;
}


When a parameter is passed by value, the function essentially creates a copy of it, so the parameter passed in isn't actually changed. As I mentioned earlier, passing by reference uses the address of the variable, meaning the changes are made to the variable passed in and a copy is not created.

If you're looking to alter multiple values within your function, I would recommend passing by reference. If you're just looking to perform some functionality then return a result, stick with pass by value but make sure you're only returning one value.

Hope this helps.
I thought this line declared the variables to be used
1
2
3
    double getInPatient(int numberofDays, double dailyRate,
                     double hospitalMedicationCharge,
                     double chargesForHospitalServices)


how should i code it to pass all variables data back to the main for displaying?
Oi, Moschops. You've undermined my beautifully written, stunningly articulated and immaculately formatted pass by reference example above.

The shame.
I am now in a self-destructive shame spiral that will result in me drinking an entire bottle of red tonight.

DISninjaTASTIC :(
Swap the red for Jack and I'll join ya!
Topic archived. No new replies allowed.