Best Stable Matching program

I am trying to write a program to ask the user to enter in two data files that contains the most preferred matches in order of 0 to 4. I am getting this error Unhandled exception thrown: read access violation._Pnext was 0xCCCCCCD0. I know is something with my read in function i am including the data set examples or what is suppose to be the output.

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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

//define the max likes and final matches
#define MAXL 50 
#define MAXFM 10

using namespace std;

//********************************************Struct Match****************************************************
struct Match {
	string name;   //user or client
	string nMatches[MAXL]; //max possible matches
	int mNum = 0; //this number keeps track of where the best match left off search
	int aMatches = 0; //records the actual number of matches
	int tMatches;
	int sMatches = 1; //max number of stable matches per user
	int asMatches = 0;
	string fMatch[MAXFM]; //max number of final matches
	bool isOpen = true;
	int matchRating = 0;
};
//************************************************************************************************************

//returns the fmatch num
int findFmatchNum(struct Match &m, string &xName) {
	int i = 0;
	while (i < MAXFM - 1) {
		if (m.fMatch[i] == xName) {
			return i;
		}
	}
}

// Makes isOpen to false
void notFree(struct Match &m) {
	m.isOpen = false;
}

// Makes isOpen to true
void makeFree(struct Match &m) {
	m.isOpen = true;
}

//test method
bool isFree(struct Match &m) {
	if (m.aMatches < m.sMatches) {
		makeFree(m);
		return m.isOpen;
	}
	else {
		notFree(m);
	}
}

//finds the next pick
string findNextPick(struct Match &p) {
	int i = 0;
	while (i < MAXL - 1) {
		if (!(p.nMatches[i] == "")) {
			return p.nMatches[i];
		}
		i++;
	}
}

//returns the pos of a person in arry
int findPerson(struct Match * list, string n) {
	int i = 0;
	while (i < MAXL) {
		if (list[i].name == n) {
			return i;
		}
	}
}
//returns the string name of a person in arry
string findPersonName(struct Match * list, string n) {
	int i = 0;
	while (i < MAXL) {
		if (list[i].name == n) {
			return list[i].name;
		}
	}
}

string returnfNamel1(struct Match &m) {
	return m.fMatch[0];
}

// remove a match from fmatch
void removeMatch(struct Match &m, string &xName) {
	int i = findFmatchNum(m, xName);
	m.fMatch[i] = "\0";
	m.asMatches--;
}

//finds rating 0 being best
int findRating(struct Match &person, string &n) {
	int i = 0;
	while (i < MAXL) {
		if (person.nMatches[i] == n) {
			return i;
		}
	}
}

// adds a match to fmatches
void addMatch(struct Match &person, string &mname) {
	int i = 0;
	if (!person.fMatch[i].empty()) {
		i++;
	}
	person.fMatch[i] = mname;
	person.asMatches++;
	person.matchRating = findRating(person, mname);
	//removes name from list of matches
	person.nMatches[person.matchRating] = "\0";
}

bool checkIfbetter(struct Match person, string nperson) {
	if (person.matchRating > findRating(person, nperson)) {
		return true;
	}
	return false;
}

//Finds the best possible match for the each client - main matching algorithm
// list1 = woman
// list2 = men
void findMatchs(struct Match * list1, Match * list2) {
	int i = 0, k = 0, temp = 0, temp2 = 0;
	string nMatch, tempN;
	while (list2[i].isOpen) {
		nMatch = findNextPick(list2[i]);
		k = findPerson(list1, nMatch);
		if (isFree(list1[k])) {
			addMatch(list2[i], nMatch);
			addMatch(list1[k], list2[i].name);
		}
		else if ((!isFree(list1[k]) && (checkIfbetter(list1[k], list2[i].name)))) {
			tempN = returnfNamel1(list1[k]);
			removeMatch(list1[k], tempN);
			temp2 = findPerson(list2, tempN);
			removeMatch(list2[temp2], list1[k].name);
			addMatch(list2[i], nMatch);
			addMatch(list1[k], list2[i].name);
		}
		else {
			removeMatch(list2[i], nMatch);
			removeMatch(list1[k], list2[i].name);
		}
		i++;
	}
}

void printMatches(struct Match * m){
	int i = 0;
	int maxNum = m[0].tMatches;
	while (i < maxNum) {
		cout << m[i].fMatch;
		i++;
	}
}

void loadMatchesf2(ifstream &data, struct Match * matchesArr, int &tnum) {

	struct Match newMatch; // new match struct
	string newName = "";   //user or client
	string nmTemp[MAXL]; //max possible matches
	int newaMatches = 0; //records the actual number of matches
	int newsMatches = 1; //requested number of stable matches per user
	string newfMatch[MAXFM]; //max number of final matches
	int newtMatches = 0;
	bool newisOpen = true;

	

	data >> newName;

	while (!data.eof()) {
		//reads in the data
		data >> ws;
		data >> newsMatches;
		data >> ws;
		string str;
		getline(data, str);
		int found, i = 0;
		while (str != "\0") {
			found = str.find(" ");
			for (int k = 0; k < found; k++) {
				nmTemp[i] += str[k];
			}
			str.erase(0, found + 1);
			i++;
			newaMatches++;
			newtMatches++;
		}
		//assign data to new item
		newMatch.name = newName;
		newMatch.sMatches = newsMatches;
		newMatch.tMatches = newtMatches;
		//copys the array of favored matches
		for (int j = 0; j < newtMatches; j ++) {
			newMatch.nMatches[j] = nmTemp[j];
		}
		// Add new profile to the list
		matchesArr[tnum] = newMatch;
		newName = "";
		//updates the total number of people
		tnum++;
		data >> newName;
	}
	return;
}

void loadMatchesf1(ifstream &data, struct Match * matchesArr, int &tnum) {

	struct Match newMatch; // new match struct
	string newName = "";   //user or client
	string nmTemp[MAXL]; //max possible matches
	int newaMatches = 0; //records the actual number of matches
	string newfMatch[MAXFM]; //max number of final matches
	int newtMatches = 0;
	bool newisOpen = true;

	data >> newName;

	while (!data.eof()) {
		//reads in the data
		data >> ws;
		string str;
		getline(data, str);
		int found = 0, i = 0;
		while (str != "\0") {
			found = str.find(" ");
			for (int k = 0; k < found; k++) {
				nmTemp[i] += str[k];
			}
			str.erase(0, found + 1);
			i++;
			newaMatches++;
			newtMatches++;
		}
		//assign data to new item
		newMatch.name = newName;
		newMatch.aMatches = newaMatches;
		newMatch.tMatches = newtMatches;
		//copys the array of favored matches
		for (int j = 0; j < newtMatches; j++) {
			newMatch.nMatches[j] = nmTemp[j];
		}
		// Add new profile to the list
		matchesArr[tnum] = newMatch;
		newName = "";
		//updates the total number of people
		tnum++;
		data >> newName;
	}
	return;
}

//******************************************* MAIN ***************************************************
int main() {

	string file1 = "";
	string file2 = "";
	struct Match matches1[MAXL];
	int m1count = 0;
	struct Match matches2[MAXL];
	int m2count = 0;
	string input = "";

	cout << "Enter the first file name:\n>";
	getline(cin, file1);
	cout << "Enter the second file name:\n>";
	getline(cin, file2);

	ifstream inputFile1(file1, ios::in);
	ifstream inputFile2(file2, ios::in);

	inputFile1.open(file1);
	inputFile2.open(file2);

	loadMatchesf1(inputFile1, matches1, m1count);
	loadMatchesf2(inputFile2, matches2, m2count);

	inputFile1.close();
	inputFile2.close();

	findMatchs(matches1, matches2);

	printMatches(matches2);


	cout << "Enter Q to quit";
	cin >> input;
	if ((input == "q") || (input == "q")) {
		return 0;
	}
}


Suppose the input fileset1.residentshas the following contents:
Black Barnes Albany
Blythe Albany Barnes
Johnson Albany Barnes
Malfoy Albany Barnes
Potter Barnes Albany

and the input fileset1.hospitalshas the following contents:
Albany 2 Blythe Potter Johnson Black Malfoy
Barnes 3 Black Johnson Blythe Potter Malfoy

Then the following is a potential execution of the program:
Enter residents filename:set1.residents
Enter hospitals filename:set1.hospitals

Matches are:
Albany: Blythe Johnson
Barnes: Black Potter Malfoy
Last edited on
The code as you've posted it is unreadable. Use code tags.

A couple comments:

1. MSVC fills stack memory with 0xCC, so if you allocate a 32-bit value on the stack and never initialize it, it will have the value 0xCCCCCCCC. An attempt to read 0xCCCCCCD0 suggest that you tried to dereference an uninitialized pointer to a structure and then access 4 bytes after the start of the structure. For example,
1
2
3
4
5
6
7
struct Foo{
    int a;
    int b;
}

Foo *p; //uninitialized
p->b = 0; //will attempt to write to 0xCCCCCCD0 

2. In a lot of places you're passing Match instances around by copy, when it looks like you meant to pass them by reference. For example notFree() makes no sense the way you've written it.
Topic archived. No new replies allowed.