Issue with program comprising several classes.

Hello everybody!

So I'm having a bit of an issue with a simple exercise program I'm working on.

I have to write a program for a hospital to calculate a patient fee according
to the number of days stayed in the hospital, the surgery undertaken and the
medication given to the patient.

Here is the first class:

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
//PATIENTACCOUNT CLASS
#ifndef PATIENTACCOUNT_H
#define PATIENTACCOUNT_H
#include <string>

using namespace std;


class PatientAccount
{
	private:
		string name;
		double totalCharges;
		int numDays;
		double hospitalDailyRate;
		double extraCharges;
	
	public:
		PatientAccount()
			{
				name = " ";
				totalCharges = 0;
				numDays = 0;
				hospitalDailyRate = 25.98;
				extraCharges = 0;
			}
		
		void getInformation()
			{
				cout << "\n\n Patient's name: ";
				getline(cin, name);
				cout << " Numbers of days spend in hospital: ";
				cin >> numDays;
			}
			
		setHospitalDailyRate(double d)
			{ hospitalDailyRate = d; }
			
		void calculateTotalCharges()
			{ totalCharges = (numDays * hospitalDailyRate + extraCharges); }
		
		void addToTotalCharges(double d)
			{ extraCharges += d; }
			
		getExtraCharges() const
			{ return extraCharges; }
		
		string getName() const
			{ return name; }
		
		double getTotalCharges() const
			{ return totalCharges; }
			
		getNumDays() const
			{ return numDays; }
		
		double getHospitalDailyRate() const
			{ return hospitalDailyRate; }
};

#endif 


The PatientAccount class holds the patient's name, the number of days he stayed
in the hospital, the daily rate of the hospital, and the amount he will be charged
when he checks out.


Here is the second class:
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
//SURGERY CLASS
#ifndef SURGERY_H
#define SURGERY_H
#include <string>
#include "PatientAccount.h"

using namespace std;


class Surgery
{
	private:
		string s1,
			   s2,
			   s3,
			   s4,
			   s5,
			   chosenSurgery;
		double cost1,
			   cost2,
			   cost3,
			   cost4,
			   cost5,
			   chargedAmount;
			   
	public:
		Surgery()
			{
				s1 = "Brain Surgery";
				cost1 = 548.32;
				s2 = "Kidney Implant";
				cost2 = 420.63;
				s3 = "Broken nose repair";
				cost3 = 320.36;
				s4 = "Heart transplant";
				cost4 = 520.87;
				s5 = "First aid";
				cost5 = 50.27;
				chargedAmount = 0;
			}
		
		string getSurgery1() const
			{ return s1; }
			
		string getSurgery2() const
			{ return s2; }
			
		string getSurgery3() const
			{ return s3; }
			
		string getSurgery4() const
			{ return s4; }
			
		string getSurgery5() const
			{ return s5; }
			
		string getChosenSurgery() const
			{ return chosenSurgery; }
			
		double getCost1() const
			{ return cost1; }
			
		double getCost2() const
			{ return cost2; }
		
		double getCost3() const
			{ return cost3; }
			
		double getCost4() const
			{ return cost4; }
			
		double getCost5() const
			{ return cost5; }
			
		double getChargedAmount() const
			{ return chargedAmount; }
			
		void menu()
			{
				int choice;
				
				cout << "\n\n\t\t____SURGERY MENU____\n"
					 << "\n Chose in the surgeries below:\n"
					 << "\n 1. " << s1
					 << "\n 2. " << s2
					 << "\n 3. " << s3
					 << "\n 4. " << s4
					 << "\n 5. " << s5
					 << "\n\n Your choice: ";
				cin >> choice;
				
				switch(choice)
				{
					case 1:
						chosenSurgery = s1;
						chargedAmount = cost1;
						cout << "\n Chosen surgery : " << chosenSurgery
							 << "\n Surgery Cost : $ " << chargedAmount;
						break;
					case 2:
						chosenSurgery = s2;
						chargedAmount = cost2;
						cout << "\n Chosen surgery : " << chosenSurgery
							 << "\n Surgery Cost : $ " << chargedAmount;
						break;
					case 3:
						chosenSurgery = s3;
						chargedAmount = cost3;
						cout << "\n Chosen surgery : " << chosenSurgery
							 << "\n Surgery Cost : $ " << chargedAmount;
						break;
					case 4:
						chosenSurgery = s4;
						chargedAmount = cost4;
						cout << "\n Chosen surgery : " << chosenSurgery
							 << "\n Surgery Cost : $ " << chargedAmount;
						break;
					case 5:
						chosenSurgery = s5;
						chargedAmount = cost5;
						cout << "\n Chosen surgery : " << chosenSurgery
							 << "\n Surgery Cost : $ " << chargedAmount;
						break;
					default:
						break;
				}
				
				cout << "\n\n Data saved...\n";
			}
		
		updateCost(PatientAccount p)
			{ p.addToTotalCharges(chargedAmount); }
};

#endif 


This class holds the name and cost of 5 surgeries, displays a menu so that the user may chose one
and calls the function updateCost(PatientAccount p) { p.addToTotalCharges(chargedAmount);}
And the third class:
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
//MEDICATION CLASS
#ifndef MEDICATION_H
#define MEDICATION_H
#include <string>
#include "PatientAccount.h"

using namespace std;


class Medication
{
	private:
		string m1,
			   m2,
			   m3,
			   m4,
			   m5,
			   chosenMedication;
		double cost1,
			   cost2,
			   cost3,
			   cost4,
			   cost5,
			   chargedAmount;
			   
	public:
		Medication()
			{
				m1 = "Canabis";
				cost1 = 60.25;
				m2 = "Xanax";
				cost2 = 123.87;
				m3 = "Bandages";
				cost3 = 12.30;
				m4 = "Eye drops";
				cost4 = 30.20;
				m5 = "Aspirine";
				cost5 = 50.27;
				chargedAmount = 0;
			}
		
		string getMed1() const
			{ return m1; }
			
		string getMed2() const
			{ return m2; }
			
		string getMed3() const
			{ return m3; }
			
		string getMed4() const
			{ return m4; }
			
		string getMed5() const
			{ return m5; }
			
		string getChosenMed() const
			{ return chosenMedication; }
			
		double getCost1() const
			{ return cost1; }
			
		double getCost2() const
			{ return cost2; }
		
		double getCost3() const
			{ return cost3; }
			
		double getCost4() const
			{ return cost4; }
			
		double getCost5() const
			{ return cost5; }
			
		double getChargedAmount() const
			{ return chargedAmount; }
			
		void menu()
			{
				int choice;
				
				cout << "\n\n\t\t____MEDICATION MENU____\n"
					 << "\n Chose in the medications below:\n"
					 << "\n 1. " << m1
					 << "\n 2. " << m2
					 << "\n 3. " << m3
					 << "\n 4. " << m4
					 << "\n 5. " << m5
					 << "\n\n Your choice: ";
				cin >> choice;
				
				switch(choice)
				{
					case 1:
						chosenMedication = m1;
						chargedAmount = cost1;
						cout << "\n Chosen medication : " << chosenMedication
							 << "\n Medication Cost : $ " << chargedAmount;
						break;
					case 2:
						chosenMedication = m2;
						chargedAmount = cost2;
						cout << "\n Chosen medication : " << chosenMedication
							 << "\n Medication Cost : $ " << chargedAmount;
						break;
					case 3:
						chosenMedication = m3;
						chargedAmount = cost3;
						cout << "\n Chosen medication : " << chosenMedication
							 << "\n Medication Cost : $ " << chargedAmount;
						break;
					case 4:
						chosenMedication = m4;
						chargedAmount = cost4;
						cout << "\n Chosen medication : " << chosenMedication
							 << "\n Medication Cost : $ " << chargedAmount;
						break;
					case 5:
						chosenMedication = m5;
						chargedAmount = cost5;
						cout << "\n Chosen medication : " << chosenMedication
							 << "\n Medication Cost : $ " << chargedAmount;
						break;
					default:
						break;
				}
				
				cout << "\n\n Data saved...\n";
			}
		
		void updateCost(PatientAccount p)
			{ p.addToTotalCharges(chargedAmount); }
};

#endif 


This class works the same as the Surgery class except it holds names and costs of medications instead of surgeries.


Here is my program:
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
#include <iostream>
#include <string>
#include <iomanip>
#include "PatientAccount.h"
#include "Surgery.h"
#include "Medication.h"

using namespace std;


void pause();


int main()
{
	PatientAccount patient;
	Surgery surgery;
	Medication medication;
	
	int choice;
	
	double totalCharges,
		   extraCharges;
	
	string name;
	
	cout << showpoint << fixed << setprecision(2);
	
	do
	{
		extraCharges = patient.getExtraCharges();
		totalCharges = patient.getTotalCharges();
		cout << "\n Total charges: $ " << totalCharges
			 << "\n Extra Charges: $ " << extraCharges
			 <<  "\n\n";
		
		
		cout << "\n\t\t__________HOSPITAL MENU__________\n"
			 << "\n 1. Enter Patient's Infrmation."
			 << "\n 2. Enter Surgery Information."
			 << "\n 3. Enter Medication Information."
			 << "\n 4. Patient is Checking Out."
			 << "\n 5. Exit Program."
			 << "\n"
			 << "\n User's Input: ";
		cin >> choice;
		cout << "\n\n";
		cin.sync();
		
		switch(choice)
		{
			case 1:
				patient.getInformation();
				patient.calculateTotalCharges();
				break;
			case 2:
				surgery.menu();
				surgery.updateCost(patient);
				break;
			case 3:
				medication.menu();
				medication.updateCost(patient);
				break;
			case 4:
				name = patient.getName();
				totalCharges = patient.getTotalCharges();
				
				cout << "\n\tCHECK OUT\n"
					 << "\n Name: " << name
					 << "\n Total charges: $ " << totalCharges;
				break;
			case 5:
				cout << "\n\n Exiting Program...";
				choice = -1;
				break;
			default:
				cout << "\n\n Exiting Program...";
				choice = -1;
				break;
		};
		
		cout << "\n\n\tDATA HAS BEEN SAVED...\n\n\n";
	}
	while (choice > 0);
	
	
	pause();
	
	
	
	
	return(0);
}

void pause()
{
	cout << "\n\n\n Press ENTER to continue...";
	cin.sync();
	cin.get();
	cout << "\n\n";
	
	return;
}



My problem is: Somehow I dont manage to send the values held by double chargedAmount in both
the Surgery and Medication class to the extraCharges double in the PatientAccount class.

I do think the the problem lies within the relation between the following functions:
in PatientAccount.h
1
2
void addToTotalCharges(double d)
	{ extraCharges += d; }

in Surgery.h and Medication.h
1
2
updateCost(PatientAccount p)
	{ p.addToTotalCharges(chargedAmount); }


I've tried messing around with the two functions but I haven't managed to solve my problem.

When I run the program and run the first case of the switch(choice) function, the totalCharges
are calculated however the extraCharges remain equal to 0 even after I run the other cases of the
switch function.


Any suggestion/advice would be much appreciated.

Thank you in advance,

Have a great day!
You're passing your "PatientAccount" by copy: the original one will remain unaffected.
You probably want to pass it by reference:

void Surgery::updateCost(PatientAccount& p) { p.addToTotalCharges(chargedAmount); }

and:

void Medication::updateCost(PatientAccount& p) { p.addToTotalCharges(chargedAmount); }
You're passing your "PatientAccount" by copy: the original one will remain unaffected.
You probably want to pass it by reference:

void Surgery::updateCost(PatientAccount& p) { p.addToTotalCharges(chargedAmount); }

and:

void Medication::updateCost(PatientAccount& p) { p.addToTotalCharges(chargedAmount); }


Waw so little change yet it fixed everything.

Thanks a lot to you Enoizat, have a great day!
Topic archived. No new replies allowed.