Having trouble with adding/sorting/searching to an array of a class.

Hi, I am having some trouble with this project that requires me to display a menu and then allow the user to select if they would like to add to the patient list, print the list, sort by last name, sort by ID, and search by last name and ID.

Since this array is of class patientType I am having some trouble bubble sorting and binary searching. I created a function to return the patients last name and ID, and now I need to be able to search by ID and last name, as well as sort by ID and last name.

Unfortunately my code isn't compiling (I'm getting a bunch of overloaded-function errors which I am not familiar with.)

How can I fix these overload errors to at least get my code to compile?

I realize there are also many issues with my select item function, as i cant figure out how to make it run as a switch function and not in my main code.

How can I run these functions in a switch?

I know this is a lot of code...if anyone helps it would be absolutely appreciated.

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

//header


#ifndef patientType_H
#define patientType_H

#include <iostream> 
#include <string>
using namespace std;

class patientType{
public:

	patientType(string fn = "default",
		string ln = "default", string id = "12345");
	void setData(string newF, string newL,
		string newId);
	void printData();
	string getLastName();
	string getPatientID();
	
private:
	string fname;
	string lname;
	string patientID;

};

#endif





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

//implementation

#include "patientType.h"

patientType::patientType(string fn,
	string ln, string id){

	fname = fn;
	lname = ln;
	patientID = id;
}


void patientType::printData()
{
	cout << "first name: " << fname << "\n"
		<< "last name: " << lname << "\n"
		<< "id: " << patientID << endl;

}

void patientType::setData(string newF, string newL,
	string newId)
{
	fname = newF;
	lname = newL;
	patientID = newId;
}

string patientType::getLastName()
{
	return lname;
}

string patientType::getPatientID()
{
	return patientID;
}




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


//main



#include <iostream> 
#include <string>
#include "patientType.h"

using namespace std;


const int SIZE = 10;

void showMenu();
void selectItem(int);

int addPatient(patientType patients[], int& length);

void printPatients(patientType patients[], int& length);

void bubblesortLname(patientType patients[], int length);

void bubblesortPatientID(patientType patients[], int length);


int main()
{

	patientType patients[SIZE];
	int length = 0;
//	int userSelection;

	showMenu();

	//addPatient(patients, length);


	//cin >> userSelection;

	//selectItem(userSelection);

	system("pause");
	return 0;
}

void showMenu()
{
	cout << "Please choose a number from 1-7 : " << endl;
	cout << endl;
	cout << "1: Print the existing patients." << endl;
	cout << "2: Add a new patient." << endl;
	cout << "3: Sort the patients by last name." << endl;
	cout << "4: Sort the patients by ID." << endl;
	cout << "5: Search for patient by last name. " << endl;
	cout << "6: Search for patient by ID." << endl;
	cout << "7: Exit the program. " << endl;
}

void selectItem(int newSelection)
{
	switch (newSelection) {

	case 1:

	
	

		printPatients();

		showMenu();

		break;

	case 2:

		addPatient();
	

		showMenu();

		break;

	case 3:

		bubblesortLname();


		showMenu();

		break;


	case 4:

		bubblesortPatientID();


		showMenu();

		break;


	case 5:

		binarySearchLname();


		showMenu();

		break;


	case 6:


		showMenu();

		break;
	case 7:

		system("pause");

		break;

	}
}


int addPatient(patientType patients[], int& length)
{
	string newname;
	string newlast;
	string newid;

	cout << "Enter first name of Patient: " << endl;
	cin >> newname;
	cout << "Enter last name of Patient: " << endl;
	cin >> newlast;
	cout << "Enter ID of Patient: " << endl;
	cin >> newid;

	patients[length].setData(newname, newlast, newid);

	return length;

}


void printPatients(patientType patients[], int& length)
{
	for (int i = 0; i < length; i++)
	{
		patients[i].printData();
	}
}


void bubblesortLname(patientType patients[], int length)
{
	bool sorted = false;
	for (int i = 0; i < length - 1 && !sorted; i++)
	{
		sorted = true;
		for (int j = 0; j < (length - 1 - i); j++)
		{
			if (patients[j].getLastName() > patients[j + 1].getLastName())
			{
				swap(patients[j], patients[j + 1]);
				sorted = false;
			}
		}

	}
}

void bubblesortPatientID(patientType patients[], int length)
{
	bool sorted = false;
	for (int i = 0; i < length - 1 && !sorted; i++)
	{
		sorted = true;
		for (int j = 0; j < (length - 1 - i); j++)
		{
			if (patients[j].getPatientID() > patients[j + 1].getPatientID())
			{
				swap(patients[j], patients[j + 1]);
				sorted = false;
			}
		}

	}
}

int binarySearchLname(patientType patients[], int length)
{
	int first = 0;
	int last = length - 1;
	int mid;
	string lname;

	bool found = false;

	do
	{
		cout << "Please enter the Patient's last name: " << endl;
		cin >> lname;
	}

	while (!found && first <= last && lname != "END");
	{
		mid = (first + last) / 2;
		if (patients[mid].getLastName == lname)
		{
			found = true;
			cout << "The Patients Last Name was found, info as follows: " << endl;

			cout << patients[mid].printData << endl;
		}
		else if (patients[mid].getLastName > lname)
			last = mid - 1;
		else
			first = mid + 1;
	}

	if (found)
		return mid;
	else
		return -1;
}





The answer is pretty simple:

This is your function decleration:
int addPatient(patientType patients[], int& length);
That's how you call the function:
addPatient();

When calling a function you need to provide the parameters.
Topic archived. No new replies allowed.