Can someone explain my possibly logic error in main

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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// global constants
const int MAX_RESIDENTS = 500;
const int EMPTY_ARRAY = 0;
ifstream textIn;
const int SSN_LENGTH = 11;
const int LAST_NAME_LENGTH = 15;
const int PHONE_NUMBER_LENGTH = 12;

// enumerated types
enum PhoneType {HOME, WORK, CELL};

// struct
struct RecordType
{
	string socialSecurityNumber;
	string lastName;
	string phoneNumber;
	PhoneType typePhone;
};

int main()
{
	// local variables
	RecordType ResidentRecords [MAX_RESIDENTS];
	int counter = 0;
	char userOption = 0;
	string userSocial;
	string userLastName;
	string userPhoneNumber;
	string userTypePhone;
	bool returnToMenuFromAdd = true;

	ProgramIntro ();

	if (LoadExistingData ())
	{
		if (OpenAndValidateInputFile (textIn))
		{
			ReadAndStoreInputFile (textIn, ResidentRecords, counter);
		}
		else
		{
			if (!EmptyResidentList ())
			{
				system ("PAUSE");
				return 1;
			}
		}
	}

	if (!LoadExistingData || EmptyResidentList || ReadAndStoreInputFile)
	{	
		DisplayMenu (userOption);

		if (userOption == 'S')
		{
			ShowResidentData (ResidentRecords, counter);
		}
		else if (userOption == 'A')
		{
			AddNewResident (ResidentRecords, counter, userSocial, userLastName, userPhoneNumber, userTypePhone);
		}

		//else if (userOption == 'D')
		//{
			//DeleteResident ();
		//}
		//else if (userOption == 'E')
		//{
			//SaveAndExitProgram ();
		//}		
	}

	system ("PAUSE");
	return 0;
}

void ProgramIntro ()
{
	cout << setw (62) << "This program is used to manage the local police" << endl;
	cout << setw (56) << "department's file of city residents." << endl << endl << endl;
}

bool LoadExistingData ()
{
	char userInput;
	bool result = false;
	bool check = false;
	
	while (!check) 
	{
		cout << "Load existing resident data from a file (Y/N)? ";
		cin >> userInput;

		if (userInput == 'Y' || userInput == 'y')
		{
			result = true;
			check = true;
		}
		else if (userInput == 'N' || userInput == 'n')
		{
			result = false;
			check = true;
		}

		if(check == false) 
		{//display error message
			cout << "Invalid entry! Please try again." << endl << endl;
		}
	}

	return result;
}

bool OpenAndValidateInputFile (ifstream& textIn)
{
	// open input file
    textIn.open ("RESIDENTS.TXT");

	// check if file opens successfully
	// if not, print a warning and exit program from main with a return code 1
	if (!textIn)
	{
		cout << "Error! File cannot be opened!" << endl << endl;
		return false;
	}
	else
	{
		return true;
	}
}

bool EmptyResidentList ()
{
	char userInput;
	bool result = false;
	bool check = false;
	
	while (!check) 
	{
		cout << "Would you like to continue the program with an empty resident list (Y/N)? ";
		cin >> userInput;

		userInput = (toupper (userInput));

		if (userInput == 'Y')
		{
			result = true;
			check = true;
		}
		else if (userInput == 'N')
		{
			result = false;
			check = true;
		}

		if(check == false) 
		{   
			//display error message
			cout << "Invalid entry! Please try again." << endl << endl;
		}
	}

	return result;
}

bool ReadAndStoreInputFile (ifstream& textIn, RecordType ResidentRecords [], int& counter)
{
	bool proceed = true;

	// while file is still readable, and peek does not find end-of-file
	while (textIn.good () && textIn.peek () != EOF)
	{
		// if the file contains too many lines of data, stop reading data and issue warning
		if (counter >= MAX_RESIDENTS)
		{
			cout << "Error: file contains too many lines of data" << endl;
		}
		else
		{
			// read in data from RESIDENTS.TXT
			textIn >> ResidentRecords [counter].socialSecurityNumber;
			textIn >> ResidentRecords [counter].lastName;
			textIn >> ResidentRecords [counter].phoneNumber;
			textIn >> (int&) ResidentRecords [counter].typePhone;

			counter++;
		}
		
		// read newline
		textIn.get ();
	}

	return proceed;
}

void DisplayMenu (char userOption)
{
	while (userOption != 'S' && userOption != 'A' && userOption != 'D' && userOption != 'E')
	{
		cout << endl << endl;

		cout << "Please choose from the following menu of options:" << endl;
		cout << "[S]how (display to the screen) all resident data" << endl;
		cout << "[A]dd resident(s) to the array" << endl;
		cout << "[D]elete a resident" << endl;
		cout << "[E]xit the program" << endl << endl;

		cout << "How would you like to continue?: ";
		cin >> userOption;

		userOption = (toupper (userOption));

		cout << endl;

		if (userOption != 'S' && userOption != 'A' && userOption != 'D' && userOption != 'E')
		{
			cout << "Invalid Entry!  Please try again." << endl << endl;
		}
	}
}

void ShowResidentData (RecordType ResidentRecords [], int& counter)
{
	if (counter > EMPTY_ARRAY)
	{
		cout << "Lastname";
		cout << setw (12) << "SSN";
		cout << setw (15) << "Phone";
		cout << setw (13) << "Type" << endl;

		cout << "---------------  -----------  ------------  ----" << endl;

		for (int i = 0; i < counter; i++)
		{
			cout << ResidentRecords [i].lastName;
			cout << setw (23) << ResidentRecords [i].socialSecurityNumber;
			cout << setw (14) << ResidentRecords [i].phoneNumber;
			switch (ResidentRecords [i].typePhone)
			{
				case HOME:
					cout << setw (6) << "HOME" << endl;
					break;
				case WORK:
					cout << setw (6) << "WORK" << endl;
					break;
				case CELL:
					cout << setw (6) << "CELL" << endl;
					break;
			}
		}
	}
	else
	{
		cout << "No residents have been stored." << endl << endl;
	}
}

string ReadAndValidateUserSocial (string userSocial)
{
	bool check = false;

	while (!check)
	{
		check = true;

		cout << "Enter Social Security Number (###-##-####): ";
		cin >> userSocial;

		if (userSocial.length () != SSN_LENGTH)
		{
			check = false;
		}
		
		if (userSocial [3] != '-' && userSocial [6] != '-')
		{
			check = false;
		}
		else
		{
			userSocial = userSocial.replace (3, 1, "");
			userSocial = userSocial.replace (5, 1, "");

			for (unsigned int i = 0; i < userSocial.length(); i++)
			{
				if (!isdigit (userSocial [i]))
				{
					check = false;
				}
			}
		}
		
		if (!check)
		{
			cout << "Invalid Entry! Please try again." << endl;
		}
	}
	
	return userSocial;
}

string ReadAndValidateUserLastName (string userLastName)
{
	bool check = false;

	while (!check)
	{
		check = true;

		cout << "Enter Last Name: ";
		getline (cin, userLastName);

		if (userLastName.length () > LAST_NAME_LENGTH)
		{
			check = false;
		}
		
		for (unsigned int i = 0; i < userLastName.length(); i++)
		{
			if (!isalpha (userLastName [i]) || isspace (userLastName [i]))
			{
				check = false;
			}
		}

		if (!check)
		{
			cout << "Invalid Entry! Please try again." << endl;
		}
	}

	userLastName [0] = toupper (userLastName [0]);

	return userLastName;
}

string ReadAndValidateUserPhoneNumber (string userPhoneNumber)
{
	bool check = false;

	while (!check)
	{
		check = true;

		cout << "Enter Phone Number (###-###-####): ";
		cin >> userPhoneNumber;

		if (userPhoneNumber.length () != PHONE_NUMBER_LENGTH)
		{
			check = false;
		}
		
		if (userPhoneNumber [3] != '-' && userPhoneNumber [6] != '-')
		{
			check = false;
		}
		else
		{
			userPhoneNumber = userPhoneNumber.replace (3, 1, "");
			userPhoneNumber = userPhoneNumber.replace (6, 1, "");

			for (unsigned int i = 0; i < userPhoneNumber.length(); i++)
			{
				if (!isdigit (userPhoneNumber [i]))
				{
					check = false;
				}
			}
		}
		
		if (!check)
		{
			cout << "Invalid Entry! Please try again." << endl;
		}
	}
	
	return userPhoneNumber;
}

string ReadAndValidateUserPhoneType (string userTypePhone)
{
	bool check = false;
	char userInput;

	while (!check)
	{
		cout << "Enter Phone Type:" << endl;
		cout << "[H]ome" << endl;
		cout << "[W]ork" << endl;
		cout << "[C]ell" << endl;
		cin >> userInput;

		(userInput) = (toupper (userInput));

		if (userInput == 'H')
		{
			userTypePhone = "HOME";
			check = true;
		}
		else if (userInput == 'W')
		{
			userTypePhone = "WORK";
			check = true;
		}
		else if (userInput == 'C')
		{
			userTypePhone = "CELL";
			check = true;
		}
		else
		{
			cout << "Invalid Entry! Please try again." << endl;
		}
	}

	return userTypePhone;
}
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
void AddNewResident (RecordType ResidentRecords [], int& counter, string userSocial, string userLastName, string userPhoneNumber, string userTypePhone)
{
	bool check = false;
	bool flag = false;
	char userInput;

	while (!check)
	{
		if (counter >= MAX_RESIDENTS)
		{
			cout << "There are too many lines of data!" << endl;
			check = true;
		}
		else
		{
			cout << "Would you like to enter a new resident (Y/N)? ";
			cin >> userInput;
			
			userInput = (toupper (userInput));

			while (!flag)
			{
				if (userInput == 'N')
				{
					flag = true;
					check = true;
				}
				else if (userInput == 'Y')
				{
					flag = true;
					
					string ReadAndValidateUserLastName (userLastName);
					string ReadAndValidateUserSocial (userSocial);
					string ReadAndValidateUserPhoneNumber (userPhoneNumber);
					string ReadAndValidateUserPhoneType (userTypePhone);
				}

				if (flag = false)
				{
					cout << "Invalid Entry! Please try again!" << endl;
				}
			}
		}
	}
}


When I get through to the DisplayMenu, i am prompted to choose from a menu of options. I am returning the userOption as a char, and in main I have an if statement that should take that userOption and determine which function to call next. Currently, it is not doing so, it isn't displaying anything after i make my selection, just saying "Press any key to continue" and exiting the program...any help would be appreciated!

Note: I removed the prototypes from the program because it is complaining about too many characters to post here :)
Last edited on
userOption in DisplayMenu is a copy of userOption in main so changes made to userOption in DisplayMenu will not be visible in main. Pass userOption by reference or make DisplayMenu return the new value of userOption at the end of the function.
Last edited on
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
char DisplayMenu (char userOption)
{
	while (userOption != 'S' && userOption != 'A' && userOption != 'D' && userOption != 'E')
	{
		cout << endl << endl;

		cout << "Please choose from the following menu of options:" << endl;
		cout << "[S]how (display to the screen) all resident data" << endl;
		cout << "[A]dd resident(s) to the array" << endl;
		cout << "[D]elete a resident" << endl;
		cout << "[E]xit the program" << endl << endl;

		cout << "How would you like to continue?: ";
		cin >> userOption;

		userOption = (toupper (userOption));

		cout << endl;

		if (userOption != 'S' && userOption != 'A' && userOption != 'D' && userOption != 'E')
		{
			cout << "Invalid Entry!  Please try again." << endl << endl;
		}
	}

	return userOption;
}


like this correct? I thought i had that, but i guess not. I see that it is being returned to main for sure now but no matter what i select, it is calling the function associated with userOption = 'S'
You need to update userOption in main.
userOption = DisplayMenu(userOption);
Topic archived. No new replies allowed.