Need Help with Linked List Dating Service

Hi all I have been struggling with this program for quite awhile. I can't seem to get the PRINTFREE and PRINTMATCH commands to work right. I seen a similar problem on here but I am still grasping at straws. Any help would be greatly appreciated. Here is the scenario:

Programming Assignment 2

Your assignment is to write a program for a computer dating service. Each client gives you his or her

name, phone number, and a list of interests. It is your job to maintain lists of men and women using

the service and to match up the compatible couples. You will find more details about the assignment,

including the inputs, outputs, and deliverables in the course environment.

Input

Data about current clients should be on file "Clients.mf". For each current client, the file contains the

following information:

Sex 1 character, 'M' or 'F'

Name up to 20 characters, followed by comma

Phone Number 8 characters

Number of Interests an integer

List of Interests 10 characters each separated by commas with a period after the

Match up to 20 characters, followed by end-of-line.

From the keyboard: The user may enter on of the following commands.

Command Processing

NEWCLIENT <sex> <name>

<phone number><number of

interests> <interests>

final interest.

Add the client to the appropriate list by storing the appropriate

information. Attempt to match this new client with a member of the

opposite sex. A match occurs when the clients have three or more

of the same interests. (You only have to find the first match for each

new client.) Make sure you then designate both persons as matched,

as described in the section on data structures above. Print the name

of the new client, the name of his or her match, and both phone

numbers. If no match is found, print an appropriate message.

<name> from the matched person.

currently matched.

UNMATCH <name> Unmatch this name with his or her current match by removing

PRINTMATCH Print a list of all matched pairs.

PRINTFREE Print the names and phone numbers of clients who are not

QUIT Stop processing

Output

Echo print all screen input and output on file "Dates.out."

Data Structures

The problem requires you to maintain two lists of clients, one for men and one for women. When a

new client is added to the list, he or she is added to the end of the appropriate list.

Each list element must include all the information for one client: name, phone number, number of

interests (maximum number is 10), interests, and the name of this client's current match (empty

string if not matched). You must use one of the list classes developed in Chapter 3.

Deliverables

• Your design.

• A listing of your program

• A listing of your test plan as input to the program

• A listing of the output file
Here is my codes:

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
#include "Client.h"

	Client :: Client()
	{numInterest = 0;}

	void Client :: setGender(char gender)
	{sex = gender;}

	void Client :: setName(string n)
	{name = n;}

	void Client :: setPhone(string p)
	{phoneNum = p;}

	void Client :: setInterest(string i)
	{
		for (int index = 0; index < 10; index++)
			interest[index] = i;
	}

	bool Client :: isMatchingClient (Client& otherClient)
	{ 
		int index = 0, length = 10;
		bool found = false;

		while (index < length && ! found)
		{
			if(interest[index] == otherClient.interest[index])
			{
				found = true;
				match = otherClient.getName();
				numInterest++;
				otherClient.match = name;
				otherClient.numInterest++;
			}
			index++;
		}
		return found;
	}

	void Client :: setMatch(string& m)
	{match = m;}

	relationType  Client :: compareTo(Client otherClient)
	{
		if (name < otherClient.name)
			return LESS;
		else if (name > otherClient.name)
			return GREATER;
		else 
			return EQUAL;
	}

	char Client :: getGender() const
	{return sex;}

	string Client :: getName() const
	{return name;}

	string Client :: getPhone() const
	{return phoneNum;}

	string Client :: getClientDescription() const
	{
		string desc;

		desc = name + " " + sex + " " + phoneNum;

		return desc;
	}

	int Client :: getNumInterest() const
	{return numInterest;}

	string Client :: getMatch() const
	{return match;}


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
#include "ClientList.h"

	ClientList :: ClientList()
	{
		length = 0;
		listData = NULL;	
	}

	ClientList:: ~ClientList()

	{
		ClientNode* tempPtr;

		while (listData != NULL)
		{
			tempPtr = listData;
			listData = listData->next;
			delete tempPtr;
		}
	}

	void ClientList :: makeEmpty()
	{
		ClientNode* tempPtr;

		while (listData != NULL)
		{
			tempPtr = listData;
			listData = listData->next;
			delete tempPtr;
		}
	    
		length = 0;
	}

	bool ClientList :: isFull()
	{
		ClientNode* location;
		
		try
		{
			location = new ClientNode;
			delete location;
			return false;
		}
		catch (std :: bad_alloc exception)
		{
			return true;
		}	
	
	}

	int ClientList :: getLength()
	{
		return length;
	}

	Client ClientList :: getClient(Client person)
	{
		bool moreToSearch;
		ClientNode* location;

		location = listData;
		bool found = false;
		moreToSearch = (location != NULL);

		while (moreToSearch && !found)
		{
			switch(person.compareTo(location->info))
			{
			case LESS:
			case GREATER:	location = location->next;
							moreToSearch = (location != NULL);
							break;

			case EQUAL:		found = true; 
							person = location->info;
							break;
			}
		}

		return person;
	
	}

	void ClientList :: newClient(Client person)
	{
		ClientNode* location;
		ClientNode* newNode;
		newNode = new ClientNode;
		newNode->info = person;
		newNode->next = NULL;

		if (!listData)
			listData = newNode;

		else
		{
			location = listData;

			while (location->next)
				location = location->next;
			
			location->next = newNode;
		}

		length++;
	}

	void ClientList :: deleteClient(Client person)
	{
		ClientNode* location = listData; 
		ClientNode* tempLocation;
		
		if(person.compareTo(location->info) == EQUAL)
		{
			tempLocation = location;
			listData = listData->next;
		}

		else
		{
			while (person.compareTo((location->next)->info) != EQUAL)
				location = location->next;

			tempLocation = location->next;
			location->next = (location->next)->next;

			delete tempLocation;
			length--; 
		}
	}
	void ClientList :: resetList()
	{
		currentPos = NULL;
	}

	Client ClientList :: getNextItem()
	{
		if (currentPos = NULL)
			currentPos = listData;
		else 
			currentPos = currentPos->next;
		
		return currentPos->info;
	}
	void ClientList :: printFreeClients()
	{
		ClientNode* location = listData;
		
		while ( location != NULL)
		{
			if (location->info.getMatch() == " ")
			{
				location->info.getClientDescription();
				cout << endl;
			}
		}
	}
	void ClientList :: printMatches(ClientList& otherList)
	{
		ClientNode* location = listData;
		ClientNode* otherLocation = otherList.listData;
		
		while( location != NULL && otherLocation !=NULL)
		{
			if (location->info.getMatch() != " ")
			{
				location->info.getClientDescription();
				cout << endl;
			}

			if (otherLocation->info.getMatch() != " ")
			{
				otherLocation->info.getClientDescription();
				cout << endl;
			}
		}
	}

	Client ClientList :: getMatchingClient(Client& otherClient)
	{
		ClientNode* location = listData;
		while( location != NULL ) 
		{
			if( location->info.isMatchingClient(otherClient))
			{
				otherClient = location->info;
				return otherClient;
			}
			location = location->next;
		}
	}


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
# include "Client.h"
# include "ClientList.h"
# include <fstream>
# include <string>
# include <cctype>
# include <cstring>
# include <iostream>
using namespace std;

void displayMenu();

int main ()
{
	string choice;

	ClientList femaleList, maleList;

	Client member;

	do 
	{
		cout << "\nEnter one of the options from the menu.\n\n";

		displayMenu();

		cin >> choice;
		cin.ignore();

		if (choice == "NEWCLIENT" || choice == "newclient")
		{
			char clientSex, repeat;

			string	 clientName, 
					 clientPhone,
					 clientInterest;
					
			int numInterest;
			bool found;

			do
			{  
				cout << "Enter the following information for each client:\n\n";
				cout << "Enter the gender of the client (\'M\' for male ";
				cout << "or \'F\' for female)\n";

				cin.get(clientSex);
				cin.ignore();
				
				while (toupper(clientSex) != 'M' && toupper(clientSex) != 'F')
				{
					cout << "Invalid entry. Enter \'m\' or \'f\'.";
					cin.get(clientSex);
					cin.ignore();
				}
		
				if (toupper(clientSex) == 'M')
					member.setGender(clientSex);

				else if (toupper(clientSex) == 'F')
					member.setGender(clientSex);

				cout << "Enter the name  of the client (use only 21 letters)\n";
				getline(cin, clientName);
				member.setName(clientName);

				cout << "Enter the phone number of the client (use only 7 digits)\n";
				getline(cin, clientPhone);
				member.setPhone(clientPhone);

				cout << "Enter the number of interests for the client ";
				cout << "(maximum of 10 interests per person)\n";
				cin >> numInterest;
				cin.ignore();

				for (int index = 0; index < numInterest; index++)
				{
					cout << "Enter interest " << (index+1);
					cout << ": (add a comma after each interest)  ";

					getline(cin, clientInterest);

					member.setInterest(clientInterest);
				}
				
				if (member.getGender() == 'M' || member.getGender() == 'm')
				{
					maleList.newClient(member);

				}

				else if (member.getGender() == 'F' || member.getGender() == 'f')
					femaleList.newClient(member);

				
				
				cout << "Would you like to enter another client? (y/n)?  "; 
				cin.get(repeat);
				cin.ignore();
			}while (toupper(repeat) != 'N');
		}
		else if (choice == "UNMATCH" || choice == "unmatch")
		{
			string empty = " ";
			member.setMatch(empty);
		}
		else if (choice == "PRINTMATCH" || choice == "printmatch")
		{
			maleList.printMatches(femaleList);
			femaleList.printMatches(maleList);
		}
		else if (choice == "PRINTFREE" || choice == "printfree")
		{
			femaleList.printFreeClients();
			maleList.printFreeClients();
		}

		else if (choice == "QUIT" || choice == "quit")
		{
			return 0;
		}

		else
			cout << "Invalid entry. Please try again.\n";
			
		

	}while(choice != "QUIT" || choice != "quit");

	cin.get();
	return 0;
}

void displayMenu()
{
	cout << "NEWCLIENT:\tAdd a new member to the database\n\n";
	cout << "UNMATCH:\tRemoves the matching member from the client\n\n";
	cout << "PRINTMATCH:\tPrints a list of all matched members\n\n";
	cout << "PRINTFREE:\tPrints the name and phone numbers of members that "
		 << "currenctly\n\t\tare not matched\n\n";\
	cout << "QUIT:\t\tEnds program\n\n";
}
Topic archived. No new replies allowed.