blob: 053bc9dc8077c5c242a1198362af920bb5090b4a (
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
|
CST 116 Module 9: Lab 9
16a 11.14 pg 337-338 #2, #3, #4
#2
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int index = 0;
int ints[10];
ifstream inputFile;
string fileName;
cout << "Enter the file name having 10 ints each in new line: ";
cin >> fileName;
inputFile.open(fileName);
if (!inputFile.is_open()) {
cout << "\n\nUnable to open file. Ending..\n\n\n\n";
return 0;
}
while (!inputFile.eof())
{
inputFile >> ints[index];
index++;
}
inputFile.close();
int smallest = ints[0], largest = ints[0];
for (int i = 1; i < 10; i++)
{
if (ints[i] > largest) {
largest = ints[i];
}
if (ints[i] < smallest) {
smallest = ints[i];
}
}
cout << "\nSmallest int in the file is: \n" << smallest;
cout << "\nLargest int in the file is: \n" << largest;
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (ints[i] > ints[j]) {
int newInt = ints[j];
ints[j] = ints[i];
ints[i] = newInt;
}
}
}
cout << "\nSorted list is as below: \n\n" << endl;
for (int i = 0; i < 10; i++) {
cout << ints[i] << "\n";
}
return 0;
}
#3
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string fileName;
cout << "Enter file name: ";
cin >> fileName;
ifstream in(fileName.c_str());
if (in.is_open()) {
string line;
int lineNumber = 1;
while (getline(in, line)) {
cout << lineNumber << " " << line << " " << line.size() << endl;
++lineNumber;
}
in.close();
}
else {
cout << fileName << " is not a file that exists, terminating program..." << endl;
}
return 0;
}
#4
#include<iostream>
#include<cstring>
#include<string.h>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
using namespace std;
struct PersonInfo
{
char firstname[100];
char LastName[100];
char birthdate[100];
long long Phone_number;
};
struct PersonInfo persondata[100];
class Person
{
public:
char firstname[100];
char LastName[100];
char birthdate[100];
long long Phone_number;
public:
void AddNewPerson();
void PrintDetails();
void Edit_Details(int index2);
int Find_Person(char Name[]);
public:
int index;
};
int Person::Find_Person(char Name[])
{
for (int i = 0; i < index; i++)
{
if (!(strcmp(persondata[i].firstname, Name)))
{
cout << "\nThe Details are Found" << endl;
cout << "\nFirstName: " << persondata[i].firstname << endl;
cout << "\nPhone: " << persondata[i].Phone_number << endl;
return i;
}
}
return -1;
}
void Person::Edit_Details(int index2)
{
int option;
cout << "\nFirstName: " << persondata[index2].firstname << endl;
cout << "\nLastName: " << persondata[index2].LastName << endl;
cout << "\nBirthDate: " << persondata[index2].birthdate << endl;
cout << "\nPhone: " << persondata[index2].Phone_number << endl;
cout << "\nSelect Any Field to Update" << endl;
cin >> option;
switch (option)
{
case 1:
cout << "\nPlease Enter New Name" << endl;
cin >> firstname;
strcpy_s(persondata[index2].firstname, firstname);
break;
case 2:
cout << "\nPlease Enter Last Name" << endl;
cin >> LastName;
strcpy_s(persondata[index2].LastName, LastName);
break;
case 3:
cout << "\nPlease Enter Birthdate" << endl;
cin >> birthdate;
strcpy_s(persondata[index2].birthdate, birthdate);
break;
case 4:
cout << "\nPlease enter Phone number" << endl;
cin >> Phone_number;
persondata[index2].Phone_number = Phone_number;
break;
}
}
void Person::AddNewPerson()
{
cout << "\nPlease enter FirstName: " << endl;
cin >> persondata[index].firstname;
cout << "\nPlease Enter LastName: " << endl;
cin >> persondata[index].LastName;
cout << "\nPlease enter Birth Date: " << endl;
cin >> persondata[index].birthdate;
cout << "\nPlease enter Phone Number: " << endl;
cin >> persondata[index].Phone_number;
cout << "\n Added New Person Details Successfully " << endl;
}
void Person::PrintDetails()
{
if (index == 0)
cout << "\nEmpty" << endl;
else
{
cout << "\nFirstName\tLastName\t\tBirthDate\tPhone_number" << endl;
for (int i = 0; i < index; i++)
{
cout << persondata[i].firstname << "\t"
<< persondata[i].LastName << "\t"
<< persondata[i].birthdate << "\t"
<< persondata[i].Phone_number << endl;
}
}
}
int main()
{
Person obj;
ifstream infile;
int option, retval;
obj.index = 0;
char FirstName[100];
infile.open("Emp_info.txt");
ofstream outfile;
for (int i = 0; i < 6; i++)
{
infile >> persondata[i].firstname;
infile >> persondata[i].LastName;
infile >> persondata[i].birthdate;
infile >> persondata[i].Phone_number;
obj.index++;
}
while (true)
{
cout << "\n------------------------------------------" << endl;
cout << "***********Persons Information ***************" << endl;
cout << "------------------------------------------" << endl;
cout << "\n1. Find a person's information" << endl;
cout << "\n2. Add a person to the database???" << endl;
cout << "\n3. Edit a person's information" << endl;
cout << "\n4. Display all records to the screen" << endl;
cout << "\n5. Quit" << endl;
cout << "\n Please Select any option" << endl;
cin >> option;
switch (option)
{
case 1:
cout << "\nPlease enter Person FirstName: " << endl;
cin >> FirstName;
retval = obj.Find_Person(FirstName);
if (retval == -1)
cout << "\nThe Person Details not Found" << endl;
break;
case 2:
obj.index++;
obj.AddNewPerson();
cout << "\nNew Person Details Added Successfully " << endl;
break;
case 3:
cout << "\nPlease enter Person FirstName: " << endl;
cin >> FirstName;
retval = obj.Find_Person(FirstName);
if (retval == -1)
cout << "\nThe Person Details not Found" << endl;
else
{
obj.Edit_Details(retval);
}
break;
case 4:
obj.PrintDetails();
break;
case 5:
outfile.open("PersonReports.txt", ios::app);
outfile << "\nFirstname" << setw(20) << "LastName" << setw(20) << "BirthDate" << setw(20) << "Phone" << endl;
for (int i = 0; i < obj.index; i++)
{
outfile << persondata[i].firstname << setw(20)
<< persondata[i].LastName << setw(20)
<< persondata[i].birthdate << setw(20)
<< persondata[i].Phone_number << endl;
}
cout << "\nPerson Details Saved Successfully in the File (PersonReports.txt)" << endl;
exit(0);
break;
default:
cout << "wrong Option" << endl;
}
}
}
|