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
|
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <sstream>
using namespace std;
const int DATA = 100;
const int RECORDS = 4;
// 11.14 - 2
/*
void get_nums(int[], int&);
void sort_nums(int[], int);
void display_nums(int[], int);
int main()
{
int data[DATA];
int nums;
get_nums(data, nums);
sort_nums(data, nums);
display_nums(data, nums);
}
void get_nums(int data[], int& nums)
{
string entered;
cout << "Enter the path of the selected file: ";
cin >> entered;
cout << endl;
ifstream inFile;
inFile.open(entered);
if (inFile.is_open())
{
for (int i = 0; i < DATA; i++)
{
nums = i + 1;
inFile >> data[i];
if (inFile.eof())
break;
}
inFile.close();
}
else
{
cout << "Can't find input file " << entered << endl;
}
}
void sort_nums(int data[], int nums)
{
bool swapped;
int temp;
for (int i = 0; i < nums - 1; i++)
{
swapped = false;
for (int j = 0; j < nums - 1; j++)
{
if (data[j] > data[j + 1])
{
temp = data[j + 1];
data[j + 1] = data[j];
data[j] = temp;
swapped = true;
}
}
if (swapped == false)
break;
}
}
void display_nums(int data[], int nums)
{
cout << "The smallest number in the file is: " << data[0] << "\n\n"
<< "The biggest number in the file is: " << data[nums - 1] << "\n\n";
cout << "The sorted file:\n";
for (int i = 0; i < nums; i++)
{
stringstream temp;
char length[DATA];
temp << data[i];
temp >> length;
cout << i + 1 << ":\t" << setw(12) << left << data[i] << strlen(length) << endl;
}
cout << endl;
}
*/
// 11.14 - 3
/*
void get_data(string[], int&);
void display_data(string[], int);
int main()
{
string data[DATA];
int nums;
get_data(data, nums);
display_data(data, nums);
}
void get_data(string data[], int& nums)
{
string entered;
cout << "Enter the path of the selected file: ";
cin >> entered;
cout << endl;
ifstream inFile;
inFile.open(entered);
if (inFile.is_open())
{
for (int i = 0; i < DATA; i++)
{
nums = i + 1;
getline(inFile, data[i], '\n');
if (inFile.eof())
break;
}
inFile.close();
}
else
{
cout << "Can't find input file " << entered << endl;
}
}
void display_data(string data[], int nums)
{
cout << "The entered file:\n";
for (int i = 0; i < nums; i++)
{
cout << i + 1 << ":\t" << data[i] << '\t' << strlen(data[i].c_str()) << endl;
}
cout << endl;
}
*/
// 11.14 - 4
/*
void get_data(string[][RECORDS], int&, bool&);
void add_data(string[][RECORDS], int&);
void find_data(string[][RECORDS], int);
int search_data(string[][RECORDS], int, string);
void edit_data(string[][RECORDS], int);
void sort_data(string[][RECORDS], int);
void display_data(string[][RECORDS], int);
void save_data(string[][RECORDS], int);
int main()
{
string data[DATA][RECORDS];
int nums;
bool open;
get_data(data, nums, open);
int choice;
if (open == true)
{
do
{
sort_data(data, nums);
cout << "Menu:\n"
<< "\t1: Find a person's information\n"
<< "\t2: Add a person to the database\n"
<< "\t3: Edit a person's information\n"
<< "\t4: Display all records to the screen\n"
<< "\t5: Quit\n\n"
<< "Enter menu choice: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
find_data(data, nums);
break;
case 2:
add_data(data, nums);
break;
case 3:
edit_data(data, nums);
break;
case 4:
display_data(data, nums);
break;
default:
choice = 5;
save_data(data, nums);
cout << "Exiting program\n\n";
break;
}
} while (choice != 5);
}
return 0;
}
void get_data(string data[][RECORDS], int& nums, bool& open)
{
ifstream inFile;
inFile.open("C:\\Temp\\Lab_9.txt");
if (inFile.is_open())
{
open = true;
for (int i = 0; i < DATA; i++)
{
nums = i + 1;
for (int j = 0; j < RECORDS; j++)
{
getline(inFile, data[i][j], ' ');
if (inFile.eof())
{
nums--;
break;
}
}
if (inFile.eof())
break;
}
inFile.close();
}
else
{
open = false;
cout << "Can't find input file " << endl;
}
}
void add_data(string data[][RECORDS], int& nums)
{
cout << "Enter the person's first and last names, their phone number, and their birthday\n\n";
cin >> data[nums][0] >> data[nums][1] >> data[nums][2] >> data[nums][3];
nums++;
}
void find_data(string data[][RECORDS], int nums)
{
int found;
string search;
cout << "Enter the person's last name: ";
cin.ignore(180, '\n');
cin >> search;
found = search_data(data, nums, search);
if (found == -1)
cout << "Person isn't in database.\n\n";
else
{
cout << "Records found:\n\n";
for (int i = 0; i < RECORDS; i++)
{
cout << data[found][i] << '\t';
}
cout << endl << endl;
}
}
int search_data(string data[][RECORDS], int nums, string search)
{
int found = -1;
for (int i = 0; i < nums; i++)
{
if (data[i][1] == search)
{
found = i;
}
}
return found;
}
void edit_data(string data[][RECORDS], int nums)
{
int found;
string search;
cout << "Enter the person's last name: ";
cin.ignore(180, '\n');
cin >> search;
found = search_data(data, nums, search);
if (found == -1)
cout << "Person isn't in database.\n\n";
else
{
cout << "Enter the correct first and last names, phone number, and birthday\n\n";
cin >> data[found][0] >> data[found][1] >> data[found][2] >> data[found][3];
}
}
void sort_data(string data[][RECORDS], int nums)
{
bool swapped;
string temp[RECORDS];
for (int i = 0; i < nums - 1; i++)
{
swapped = false;
for (int j = 0; j < nums - 1; j++)
{
if (data[j][1] > data[j + 1][1])
{
for (int k = 0; k < RECORDS; k++)
{
temp[k]= data[j + 1][k];
data[j + 1][k] = data[j][k];
data[j][k] = temp[k];
}
swapped = true;
}
}
if (swapped == false)
break;
}
}
void display_data(string data[][RECORDS], int nums)
{
cout << "Printing data:\n\n";
for (int i = 0; i < nums; i++)
{
for (int j = 0; j < RECORDS; j++)
{
cout << data[i][j] << ' ';
}
cout << endl << endl;
}
}
void save_data(string data[][RECORDS], int nums)
{
ofstream outFile;
outFile.open("C:\\Temp\\Lab_9.txt");
if (outFile.is_open())
{
for (int i = 0; i < nums; i++)
{
for (int j = 0; j < RECORDS; j++)
{
outFile << data[i][j] << ' ';
cout << data[i][j] << ' ';
}
outFile << endl;
cout << endl;
}
outFile.close();
}
else
{
cout << "Can't find output file" << endl;
}
}
*/
|