Possible memory leaks

Hi all, I trying to refresh some of the concepts I learned using pointers. The program I have below works as the way I would like, but I am looking to see if there are any memory leaks, especially in my exit function. Any and all help would be greatly appreciated. Thank you.

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
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>

const int NUM_OF_LABS(4);

void create(std::string** &);
void initialize(std::string** &);
void menu(int &);
void login(std::string** &);
void logout(std::string** &);
void display(std::string** &);
void search(std::string** &);
void exit(std::string**);

int main()
{
	using namespace std;

	int option;
	string **stations;

	create(stations);
	initialize(stations);

	do
	{
		menu(option);

		if(option == 1)
			login(stations);
		else if(option == 2)
			logout(stations);
		else if(option == 3)
			display(stations);
		else if(option == 4)
			search(stations);
		else
			exit(stations);

	}while(option != 5);

	return 0;
}

void create(std::string** &stations)
{
	using namespace std;

	int numberOfStations[NUM_OF_LABS] = { 5, 6, 4, 3 };

	stations = new string*[NUM_OF_LABS];

	for(unsigned int i = 0; i < NUM_OF_LABS; i++)
		stations[i] = new string[numberOfStations[i]];
}
void initialize(std::string** &stations)
{
	int numberOfStations[NUM_OF_LABS] = { 5, 6, 4, 3 };

	for(unsigned int row = 0; row < NUM_OF_LABS; row++)
	{
		for(unsigned int col = 0; col < numberOfStations[row]; col++)
			stations[row][col] = "empty";
	}
}
void menu(int &op)
{
	using namespace std;

	string s;

	do
	{
		cout << "Please select from the following options.\n\n"
			 << "1. Simulate login\n"
			 << "2. Simulate logoff\n"
			 << "3. Display computer stations\n"
			 << "4. Search for a user\n"
			 << "5. Exit\n\n"
			 << "Option: ";
		getline(cin, s);

		if( (s.find_first_not_of("12345") != string::npos) || (s.length() > 1) )
			cout << "The option entered is not valid!\n";

	}while( (s.find_first_not_of("12345") != string::npos) || (s.length() > 1) );

	cout << endl;

	char *temp = new char[s.length() + 1];
	strncpy(temp, s.c_str(), s.length());
	temp[s.length()] = NULL;

	op = atoi(temp);

	delete [] temp;
	temp = NULL;
}
void login(std::string** &stations)
{
	using namespace std;

	char *temp;
	int labNumber, computer;
	string s, id;

	do
	{
		cout << "Enter the 5 digit userID: ";
		getline(cin, s);

		if( (s.find_first_not_of("1234567890") != string::npos) || (s.length() != 5) )
			cout << "The userID entered is not valid!\n";

	}while( (s.find_first_not_of("1234567890") != string::npos) || (s.length() != 5) );

	id = s;

	do
	{
		cout << "Enter the lab number (1-4): ";
		getline(cin, s);

		if( (s.find_first_not_of("1234") != string::npos) || (s.length() != 1) )
			cout << "The lab number entered is not valid!\n";

	}while( (s.find_first_not_of("1234") != string::npos) || (s.length() != 1) );

	temp = new char[s.length() + 1];
	strncpy(temp, s.c_str(), s.length());
	temp[s.length()] = NULL;
	labNumber = atoi(temp);
	delete [] temp;
	temp = NULL;

	do
	{
		do
		{
			if(labNumber == 1)
				cout << "Enter the computer station (1-5): ";
			else if(labNumber == 2)
				cout << "Enter the computer station (1-6): ";
			else if(labNumber == 3)
				cout << "Enter the computer station (1-4): ";
			else
				cout << "Enter the computer station (1-3): ";

			getline(cin, s);

			if( (s.find_first_not_of("123456") != string::npos) || (s.length() != 1) )
				cout << "The computer station entered is not valid!\n";

		}while( (s.find_first_not_of("123456") != string::npos) || (s.length() != 1) );

		temp = new char[s.length() + 1];
		strncpy(temp, s.c_str(), s.length());
		temp[s.length()] = NULL;
		computer = atoi(temp);
		delete [] temp;
		temp = NULL;

		if( (labNumber == 1 && computer > 5) || (labNumber == 2 && computer > 6) || (labNumber == 3 && computer > 4) || (labNumber == 4 && computer > 3) )
			cout << "The computer station entered is not valid!\n";

	}while( (labNumber == 1 && computer > 5) || (labNumber == 2 && computer > 6) || (labNumber == 3 && computer > 4) || (labNumber == 4 && computer > 3) );
	
	stations[labNumber - 1][computer - 1] = id;
	cout << "\nLogin: Validated.\n\n";
}
void logout(std::string** &stations)
{
	using namespace std;

	bool found = false;
	int numberOfStations[NUM_OF_LABS] = { 5, 6, 4, 3 };
	string id;

	do
	{
		cout << "Enter the 5 digit userID: ";
		getline(cin, id);

		if( (id.find_first_not_of("1234567890") != string::npos) || (id.length() != 5) )
			cout << "The userID entered is not valid!\n";

	}while( (id.find_first_not_of("1234567890") != string::npos) || (id.length() != 5) );

	for(int row = 0; row < NUM_OF_LABS; row++)
	{
		for(int col = 0; col < numberOfStations[row]; col++)
		{
			if(stations[row][col] == id)
			{
				stations[row][col] = "empty";
				found = true;
				break;
			}
		}

		if(found)
			break;
	}

	cout << "Logout: Complete\n\n";
}
void display(std::string** &stations)
{
	using namespace std;

	int numberOfStations[NUM_OF_LABS] = { 5, 6, 4, 3 };

	cout << "Lab Number Computer Stations\n";

	for(unsigned int row = 0; row < NUM_OF_LABS; row++)
	{
		cout << left << setw(11) << row + 1;

		for(unsigned int col = 0; col < numberOfStations[row]; col++)
			cout << col + 1 << ": " << stations[row][col] << " ";

		cout << endl;
	}

	cout << endl;
}
void search(std::string** &stations)
{
	using namespace std;

	bool found = false;
	int r, c, numberOfStations[NUM_OF_LABS] = { 5, 6, 4, 3 };
	string id;

	do
	{
		cout << "Enter the 5 digit userID: ";
		getline(cin, id);

		if( (id.find_first_not_of("1234567890") != string::npos) || (id.length() != 5) )
			cout << "The userID entered is not valid!\n";

	}while( (id.find_first_not_of("1234567890") != string::npos) || (id.length() != 5) );

	cout << endl;

	for(unsigned int row = 0; row < NUM_OF_LABS; row++)
	{
		for(unsigned int col = 0; col < numberOfStations[row]; col++)
		{
			if(stations[row][col] == id)
			{
				r = row + 1;
				c = col + 1;
				found = true;
				break;
			}
		}

		if(found)
			break;
	}

	if(found)
		cout << "User " << id << " is in Lab " << r << " at Station " << c << endl << endl;
	else
		cout << "User " << id << " is not currently logged in.\n\n";
}
void exit(std::string** stations)
{
	for(unsigned int i = 0; i < NUM_OF_LABS; i++)
		stations[i] = NULL;

	for(unsigned int i = 0; i < NUM_OF_LABS; i++)
		delete [] stations[i];
	delete [] stations;
}
Valgrind is a decent tool for checking for memory leaks.

http://valgrind.org/
Thank you pnoid. I will definitely look into this.
Topic archived. No new replies allowed.