Very large negative value being returned to Main

The program below runs perfectly. However, the value being returned at the end of Main (the total Medications charge) is some extremely large negative number! If I insert a cout statement in the PatientAccount class for ttlMCharge, the correct number is displayed. For some reason, the ttlMCharge value is being changed when it is returned to Main, and I can't figure out why!?

NOTE: For now, just ignore the Hospital charges. I will take care of that once I figure out whats goin on with the Medications charges.


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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// MAIN
#include <iostream>
#include "PatientAccount.h"
#include "Pharmacy.h"
#include "Surgery.h"
#include <iomanip>
using namespace std;

int main()
{
	Surgery type;
	Pharmacy meds;
	
	PatientAccount charges;

	double mCharge = 0,			// Surgery charge;
		ttlMCharge;
	
	

	char sType,			// type of surgery chosen by user
		mType,			// types of medications chosen by user
		choice;			

	cout << "To obtain the total patient hospital charges, first choose the type of surgery," << endl
		<< "then choose the medications used by the patient. " << endl << endl
		<< "What is the type of surgery?" << endl
		<< "Enter (B) for back surgery" << endl
		<< "Enter (H) for hip surgery" << endl
		<< "Enter (S) for shoulder surgery" << endl
		<< "Enter (F) for foot surgery" << endl
		<< "Enter (X) for hand surgery" << endl;
	cin >> sType;

	type.chooseSurgery(sType);

	do
	{
		cout << endl << "Enter the medications used by the patient" << endl << endl
			<< "Enter (V) for Vicodin" << endl
			<< "Enter (O) for OxyContin" << endl
			<< "Enter (I) for Ibuprofen" << endl
			<< "Enter (P) for Percocet" << endl
			<< "Enter (C) for Celebrex" << endl;
		cin >> mType;

		meds.chooseMeds(mType);

		cout << endl << "Do you want to add another medication?" << endl
			<< "Enter (Y) for yes.  Enter (N) to check the patient out of the hospital and" << endl
			<< "see the total charges." << endl;
		cin >> choice;
	} while (choice == 'Y' || choice == 'y');


	cout << fixed << showpoint << setprecision(2)
		<< "The total charges is: $" << charges.ttlMedsCharges(mCharge, ttlMCharge) << endl;
	

	return 0;
}

===============================

// Surgery header
#ifndef SURGERY_H
#define SURGERY_H
#include "PatientAccount.h"


// Surgery class declaration
class Surgery
{
private:
	double sCharge,			// Surgery charge;
		ttlHCharge;
	int days,				// # of days a patient must stay in the hospital
		dailyRate = 200;	// Daily rate of hospital stay is $200/day

	PatientAccount charge;	// charge is an instance of the PatientAccount class
		
public:
	void chooseSurgery(char);
	void totalHospCharges(double, double, int, int);
};
#endif

=========================

// Surgery functions
#include <iostream>
#include <string>
#include "Surgery.h"
using namespace std;

void Surgery::chooseSurgery(char sType)

{
	// Process menu selection
	switch (sType)
	{
		// In-patient
	case 'b':
	case 'B':

		sCharge = 50000;		// Cost of back surgery is $50,000
		days = 4;				

		break;

	case 'h':
	case 'H':

		sCharge = 20000;		// Cost of hip surgery is $20,000
		days = 3;				

		break;

	case 's':
	case 'S':

		sCharge = 10000;		// Cost of shoulder surgery is $10,000
		days = 3;

		break;

	case 'f':
	case 'F':

		sCharge = 5000;			// Cost of foot surgery is $5,000
		days = 1;

		break;

	case 'x':
	case 'X':

		sCharge = 8000;			// Cost of hand surgery is $8,000
		days = 1;

		break;
	}

	totalHospCharges(sCharge, ttlHCharge, dailyRate, days);
}

void Surgery::totalHospCharges(double sCharge, double ttlHCharge, int dailyRate, int days)
{
	charge.ttlHospCharges(sCharge, ttlHCharge, dailyRate, days);
}

===========================

// Pharmacy header
#ifndef PHARMACY_H
#define PHARMACY_H
#include "PatientAccount.h"


// Pharmcy class declaration
class Pharmacy
{
private:
	double mCharge,			// Surgery charge;
		ttlMCharge = 0;

	PatientAccount charge;	// charge is an instance of the PatientAccount class

public:
	void chooseMeds(char);
	void totalMedsCharges(double, double&);
};
#endif

==========================

// Pharmacy functions
#include <iostream>
#include <string>
#include "Pharmacy.h"
using namespace std;

void Pharmacy::chooseMeds(char mType)

{
	// Process menu selection
	switch (mType)
	{
	case 'v':
	case 'V':

		mCharge = 200;		// Cost of 30 day supply Vicodin is $200

		break;

	case 'o':
	case 'O':

		mCharge = 300;		// Cost of 30 day supply OxyContin is $300

		break;

	case 'i':
	case 'I':

		mCharge = 10;		// Cost of 30 day supply Ibuprofen is $10

		break;

	case 'p':
	case 'P':

		mCharge = 450;		// Cost of 30 day supply Percocet is $450

		break;

	case 'c':
	case 'C':

		mCharge = 250;		// Cost of 30 day supply Celebrex is $250

		break;
	}

	totalMedsCharges(mCharge, ttlMCharge);
}

void Pharmacy::totalMedsCharges(double mCharge, double &ttlMCharge)
{
	charge.ttlMedsCharges(mCharge, ttlMCharge);
}

==================================

// Patient account header
#ifndef PATIENTACCOUNT_H
#define PATIENTACCOUNT_H

// PatientAccount class declaration
class PatientAccount
{
private:
	double sCharge,			// total of patient charges
		ttlHCharge,
		mCharge,
		ttlMCharge,
		ttlPCharge;
	int days,
		dailyRate;
	
public:
	
	void ttlHospCharges(double, double, int, int);
	double ttlMedsCharges(double, double&);

};
#endif

=======================

// Patient Account functions
#include <iostream>
#include <string>
#include "PatientAccount.h"
using namespace std;

void PatientAccount::ttlHospCharges(double sCharge, double ttlHCharge, int dailyRate, int days)

{
	ttlHCharge = sCharge + (days * dailyRate);
	
}

double PatientAccount::ttlMedsCharges(double mCharge, double &ttlMCharge)

{
	ttlMCharge = ttlMCharge + mCharge;
	return ttlMCharge;
}
When you give a class method a function with a parameter that has the same name as a data member, the parameter overshadows the data member with the same name. Since you only feed the function an uninitialized variable as an argument for that parameter, you get garbage.

Garbage in, garbage out.

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
#include <iostream>
 
struct A {
	double member = 0.0;
 
	void func(double& member) {
		std::cout << "Address of parameter: " << &member;
		std::cout << "\nAddress of data member: " << &(this->member) << '\n' ;
 
		member = 4.0;
		this->member = 10.0;
	}
};
 
 
int main() {
	A a;
 
	double value;
 
	a.func(value);
 
	std::cout << "value: " << value << '\n' ;
	std::cout << "a.member: " << a.member << '\n' ;
}
Address of parameter: 0x7ffd1fd82a58
Address of data member: 0x7ffd1fd82a50
value: 4
a.member: 10
Thanks for the reply. I'm trying to understand your coding but a having a little difficulty/confusion understanding.

Since my multi-Class program is a bit more involved than the Struct example you gave, I wanna go back to my code and just stepping thru just following the ttlMCharge variable:

1) start in Main - double ttlMCharge declared
2) meds.chooseMeds(mType) is called from Pharmacy function
3) In Pharmacy header, ttlMCharge is initialized to 0
4) totalMedsCharges(double mCharge, double &ttlMCharge) is called in PatientAccount function; &ttlMcharge passed by reference
5) In PatientAccount, ttlMCharge is declared, but not initialized
6) ttlMedsCharges(double mCharge, double &ttlMCharge) - ttlMCharge is calculated as part of running total
7) ttlMcharge is returned to Main

So....
Are you telling me that ttlMCharge should be initialized AGAIN in #5 above? and where exactly should I be changing the parameter names? Just fyi, I did actually try changing the parameter names in the diffrent classes. That yielded no changes.
Since my multi-Class program is a bit more involved than the Struct example you gave, I wanna go back to my code and just stepping thru just following the ttlMCharge variable:


If you must have it in terms of the original code:

Start in main - ttlMCharge is defined as an uninitialized variable of type double.

There is a function invoked on an object towards the end of main which takes as one of it's parameters, a reference to the uninitialized variable ttlMCharge defined in main.
charges.ttlMedsCharges(mCharge, ttlMCharge)

The charges object is of type PatientAccount. And if we look at that function in the class we see that it takes a reference (with the same name as a data member of PatientAccount) to the variable with the uninitialized value in main, adds it to the other value fed to it and returns the resulting value.

An uninitialized variable has some unspecified value. Some unspecified value plus some other value, leaves us with an unpredictable result.

Variables with the same names that are defined in different scopes are not the same variable.

See the code I presented above for clarification.
Topic archived. No new replies allowed.