aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab9/CST116F2021-Lab9_Schroeder.cpp
blob: 0ac4b0167c717b5b9aa6cda68e3b83a5538ff00b (plain) (blame)
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
// CST116F2021-Lab9_Schroeder.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

/*
////   11.14 Programming Exercises  pp 337 - 338  #2 //////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

char fileName[200];
int inputIntegers[10]{};
ifstream fin;

int main()
{


	cout << "Enter the the name of the file you want to open: ";
	cin >> fileName;


	fin.open(fileName);
	while (fin && !fin.eof())
	{
		for (int i = 0; i < 10; i++)
			fin >> inputIntegers[i];
	}

	int j = 0, k = 0, temp = 0;
	for (j = 0; j < 10; j++)
	{
		for (k = 0; k < 10 - 1; k++)
		{
			if (inputIntegers[k] > inputIntegers[k + 1])
			{
				temp = inputIntegers[k];
				inputIntegers[k] = inputIntegers[k + 1];
				inputIntegers[k + 1] = temp;
			}
		}
	}

	cout << "The integers in this file range from " << inputIntegers[0] << " to " << inputIntegers[9]<< "\n";
	cout << "\nSorted list of integers from the file:\n";
	for (int l = 0; l < 10; l++)
	{
		cout << inputIntegers[l]<< "\n";
	}
*/



/*
// /// 11.14 Programming Exercises  pp 337 - 338  #3  /////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

char fileName[200];
int inputIntegers[10]{};
string lines[1000];
int num_records = 0;

ifstream fin;

int main()
{
	cout << "Enter the the name of the file you want to open: ";
	cin >> fileName;

	fin.open(fileName);
	while (fin && !fin.eof())
	{
		getline(fin, lines[num_records]);

		cout << num_records+1 << ". " << lines[num_records] << " (" << lines[num_records].length() << " characters)\n";
		num_records++;
	}
	fin.close();
}
*/




// /// 11.14 Programming Exercises  pp 337 - 338  #4  ///////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

string first[100]{};
string last[100]{};
string ph[100]{};
string bd[100]{};

int num_records = 0;
ifstream fin;
ofstream fout;

void DisplayMenu();
void Find();
void Add();
void Edit();
void Display();
void Sort();
void Update();


int main()
{
	// READ THE FILE IN
	fin.open("INFO.txt");
	while (fin && !fin.eof())
	{
		fin >> first[num_records]
			>> last[num_records]
			>> ph[num_records]
			>> bd[num_records];
		num_records++;
	}

	fin.close();

	DisplayMenu();

}

void DisplayMenu()
{
	int menuChoice = 0;
	cout << "\t    Menu Driven Database\n";
	cout << "************************************************\n";
	cout << "\t1) Find a person's information\n";
	cout << "\t2) Add a person to the database\n";
	cout << "\t3) Edit a person's information\n";
	cout << "\t4) Display all records to the screen\n";
	cout << "\t5) Quit\n**************************************************\n";
	cout << "Enter the number of the option you desire: ";
	cin >> menuChoice;

	switch (menuChoice)
	{
		case 1:
		{
			Find();
			break;
		}
		case 2:
		{
			Add();
			break;
		}
		case 3:
		{
			Edit();
			break;
		}
		case 4:
		{
			Display();
			break;
		}
		case 5:
		{
			Update();
			cout << "\tThank you, Goodbye!";
			break;
		}
	}
}

void Find()
{
	string temp = " ";
	cout << "Enter the Last Name to search for:  ";
	cin >> temp;

	string subString{ temp };
	int found = 0;
	for (int k = 0; k <= num_records - 1; k++)
	{
		if (last[k] == subString)
		{
			cout << "found: " << first[k] << " " << last[k] << "\t\tph: " << ph[k] << "\tDOB: " << bd[k] << "\n";
			found++;
		}		
	}
	if (found == 0)
		cout << "Account not found.\n";
	
	cout << "\n\n";

	DisplayMenu();
}


void Add()
{
	cout << "Last Name: ";
	cin >> last[num_records];
	cout << "First Name: ";
	cin >> first[num_records];
	cout << "ph #: ";
	cin >> ph[num_records];
	cout << "Date Of Birth: ";
	cin >> bd[num_records];
	num_records++;
	
	cout << "\n\n";

	DisplayMenu();
}
void Edit()
{
	Sort();

	cout << "\nAll of the current records:\n"; 
	for (int k = 0; k <= num_records - 1; k++)
	{
		cout << k + 1 << ".\t" << first[k] << " " << last[k] << "\t\tph: " << ph[k] << "\tDOB: " << bd[k] << "\n";
	}
	int choice = 0;
	cout << "\n\nEnter the # of the record you would like to edit: ";
	cin >> choice;
	cout << "Last Name: ";
	cin >> last[choice - 1];
	cout << "First Name: ";
	cin >> first[choice - 1];
	cout << "ph #: ";
	cin >> ph[choice - 1];
	cout << "Date Of Birth: ";
	cin >> bd[choice - 1];
	cout << "\n\n";

	DisplayMenu();
}


void Display()
{  
	Sort();
	
	cout << "\nAll of the current records:\n"; //sorted alphabetically by last name
	for (int k=0;k<=num_records-1;k++)
	{
		cout << k + 1 << ".\t" << first[k]<<" "<< last[k]<<"\t\tph: "<<ph[k]<<"\tDOB: "<<bd[k]<<"\n";
	}
	cout << "\n\n";

	DisplayMenu();
}

void Sort()
{
	// Sort the records by last name
	bool swapped = true;
	int j = 0;
	string tmp1;
	string tmp2;
	string tmp3;
	string tmp4;

	while (swapped)
	{
		swapped = false;
		j++;
		for (int i = 0; i < num_records-1; i++)
		{
			//if (last[i]!=null)
			if (last[i] > (last[i + 1]))
			{
				tmp1 = last[i]; tmp2 = first[i];	tmp3 = ph[i];	tmp4 = bd[i];
				last[i] = last[i + 1]; first[i] = first[i + 1];  ph[i] = ph[i + 1];	bd[i] = bd[i + 1];
				last[i + 1] = tmp1; first[i + 1] = tmp2;	ph[i + 1] = tmp3;	bd[i + 1] = tmp4;
				swapped = true;
			}
		}
	}

}

void Update()
{
	Sort();

	fout.open("INFO.txt");
	for (int i = 0; i <=num_records-2; i++)
		fout << first[i] << " " << last[i] << " " << ph[i] <<" "<< bd[i] << endl;
		//fout << first[i] << "\n" << last[i] << "\n" << ph[i] << "\n" << bd[i] << "\n";
	fout << first[num_records-1] << " " << last[num_records - 1] << " " << ph[num_records - 1] << " " << bd[num_records - 1];
	fout.close();
}