@@ -5,7 +5,7 @@ a. Emp Id
5
5
b. Emp Name
6
6
c. Emp Address
7
7
d. Emp Dept (Admin/Sales/Production/IT)
8
- e. Emp Phone
8
+ e. Emp Phone Number
9
9
f. Emp Age
10
10
11
11
Write a C++ program to read the above file. Create a new file such as adm.dat,
@@ -36,7 +36,7 @@ class Employee
36
36
private:
37
37
// // instance member variables
38
38
unsigned int id, age;
39
- char name[MAX_CHARS_IN_NAME], address[MAX_CHARS_IN_ADDRESS], department[MAX_CHARS_IN_DEPARTMENT], phone [MAX_CHARS_IN_PHONE];
39
+ char name[MAX_CHARS_IN_NAME], address[MAX_CHARS_IN_ADDRESS], department[MAX_CHARS_IN_DEPARTMENT], phoneNumber [MAX_CHARS_IN_PHONE];
40
40
41
41
public:
42
42
// // constructors
@@ -47,15 +47,15 @@ class Employee
47
47
name[0 ] = 0 ;
48
48
address[0 ] = 0 ;
49
49
department[0 ] = 0 ;
50
- phone [0 ] = 0 ;
50
+ phoneNumber [0 ] = 0 ;
51
51
}
52
52
53
- Employee (int id, const char *name, int age, const char *phone , const char *address, const char *department)
53
+ Employee (int id, const char *name, int age, const char *phoneNumber , const char *address, const char *department)
54
54
{
55
55
this ->id = id;
56
56
this ->age = age;
57
57
strcpy (this ->name , name);
58
- strcpy (this ->phone , phone );
58
+ strcpy (this ->phoneNumber , phoneNumber );
59
59
strcpy (this ->address , address);
60
60
strcpy (this ->department , department);
61
61
}
@@ -102,16 +102,16 @@ class Employee
102
102
return name;
103
103
}
104
104
105
- // // instance member function to set phone
106
- void setPhone (const char *phone )
105
+ // // instance member function to set phoneNumber
106
+ void setPhoneNumber (const char *phoneNumber )
107
107
{
108
- strcpy (this ->phone , phone );
108
+ strcpy (this ->phoneNumber , phoneNumber );
109
109
}
110
110
111
- // // instance member function to get phone
112
- const char *getPhone ()
111
+ // // instance member function to get phoneNumber
112
+ const char *getPhoneNumber ()
113
113
{
114
- return phone ;
114
+ return phoneNumber ;
115
115
}
116
116
117
117
// // instance member function to set address
@@ -141,45 +141,85 @@ class Employee
141
141
// // instance member function to input and set employee data
142
142
void inputEmployeeData ()
143
143
{
144
- int empId;
145
- char bookTitle[Employee::MAX_CHARS_IN_TITLE];
146
- double bookPrice;
144
+ int id;
147
145
148
- // // Get book id
146
+ // // get and set employee id
149
147
cout << " \n Enter Employee Id => " ;
150
- cin >> empId ;
148
+ cin >> id ;
151
149
152
- // // Get book title
153
- cout << " \n Enter Employee Title (MAX_CHARS " << Employee::MAX_CHARS_IN_TITLE - 1 << " ) => " ;
150
+ // // set id
151
+ setId (id);
152
+
153
+ // // get name
154
+ cout << " \n Enter Employee Name (MAX_CHARS " << Employee::MAX_CHARS_IN_NAME - 1 << " ) => " ;
154
155
cin.ignore ();
155
- cin.getline (bookTitle, Employee::MAX_CHARS_IN_TITLE);
156
+ cin.getline (name, Employee::MAX_CHARS_IN_NAME);
157
+
158
+ // // get age
159
+ cout << " \n Enter Employee Age => " ;
160
+ cin >> age;
156
161
157
- // // Get book price
158
- cout << " \n Enter Employee Price => " ;
159
- cin >> bookPrice;
162
+ // // get phone number
163
+ cout << " \n Enter Employee Phone Number (MAX_CHARS " << Employee::MAX_CHARS_IN_PHONE - 1 << " ) => " ;
164
+ cin.ignore ();
165
+ cin.getline (phoneNumber, Employee::MAX_CHARS_IN_PHONE);
160
166
161
- // // set data
162
- setEmployeeId (empId);
163
- setEmployeeTitle (bookTitle);
164
- setEmployeePrice (bookPrice);
167
+ // // get address
168
+ cout << " \n Enter Employee Address (MAX_CHARS " << Employee::MAX_CHARS_IN_ADDRESS - 1 << " ) => " ;
169
+ cin.ignore ();
170
+ cin.getline (address, Employee::MAX_CHARS_IN_ADDRESS);
171
+
172
+ // // get department
173
+ int choice;
174
+ while (true )
175
+ {
176
+ cout << " \n >>>>>>>>>> Choose Department <<<<<<<<<<<" ;
177
+ cout << " \n Press 1. Admin" ;
178
+ cout << " \n Press 2. Sales" ;
179
+ cout << " \n Press 3. Production" ;
180
+ cout << " \n Press 4. IT" ;
181
+ cout << " \n Enter Department Number => " ;
182
+ cin >> choice;
183
+
184
+ if (choice != 1 && choice != 2 && choice != 3 && choice != 4 )
185
+ {
186
+ cout << " \n !!!! Invalid Department Number... Try Again" << endl;
187
+ cin.ignore ();
188
+ }
189
+ else
190
+ {
191
+ if (choice == 1 )
192
+ strcpy (department, " Admin" );
193
+ else if (choice == 2 )
194
+ strcpy (department, " Sales" );
195
+ else if (choice == 3 )
196
+ strcpy (department, " Production" );
197
+ else
198
+ strcpy (department, " IT" );
199
+ break ;
200
+ }
201
+ }
165
202
}
166
203
167
- // // instance member function to show book data
168
- void showEmployeeData ()
204
+ // // instance member function to show employee data
205
+ void showEmployeeData () const
169
206
{
170
- cout << " \n Employee Id => " << empId;
171
- cout << " \n Employee Title => " << bookTitle;
172
- cout << " \n Employee Price => " << bookPrice;
207
+ cout << " \n Employee Id => " << id;
208
+ cout << " \n Employee Name => " << name;
209
+ cout << " \n Employee Age => " << age;
210
+ cout << " \n Employee Phone Number => " << phoneNumber;
211
+ cout << " \n Employee Address => " << address;
212
+ cout << " \n Employee Department => " << department;
173
213
}
174
214
175
- // // instance member function to store book record
215
+ // // instance member function to store employee data
176
216
int storeEmployeeData ()
177
217
{
178
- if (empId == -1 || bookPrice == -1 )
179
- return 0 ; // book data not stored
218
+ if (id == -1 || age == -1 )
219
+ return 0 ; // employee data not stored
180
220
181
221
// // specify file name
182
- const char *fileName = " books_data .dat" ;
222
+ const char *fileName = " emp .dat" ;
183
223
184
224
// create an instance of ofstream for writing in a file
185
225
ofstream fout;
@@ -189,19 +229,19 @@ class Employee
189
229
190
230
// // check if the file is successfully opened
191
231
if (!fout.is_open ())
192
- return 0 ; // book data not stored
232
+ return 0 ; // employee data not stored
193
233
194
234
fout.write ((char *)this , sizeof (*this ));
195
235
196
- return 1 ; // book data successfully stored
236
+ return 1 ; // employee data successfully stored
197
237
}
198
238
};
199
239
200
- // // function to fetch books data from a fike and show
240
+ // // function to fetch employees data from a fike and show
201
241
void fetchAndShowEmployeeData ()
202
242
{
203
243
// // specify file name
204
- const char *fileName = " books_data .dat" ;
244
+ const char *fileName = " emp .dat" ;
205
245
206
246
// create an instance of ifstream for reading from a file
207
247
ifstream fin;
@@ -230,11 +270,11 @@ void fetchAndShowEmployeeData()
230
270
}
231
271
}
232
272
233
- // // function to search a book from file using book id
273
+ // // function to search a employee from file using employee id
234
274
void searchEmployeeById (int empId)
235
275
{
236
276
// // specify file name
237
- const char *fileName = " books_data .dat" ;
277
+ const char *fileName = " employees_data .dat" ;
238
278
239
279
// create an instance of ifstream for reading from a file
240
280
ifstream fin;
@@ -257,7 +297,7 @@ void searchEmployeeById(int empId)
257
297
258
298
while (!fin.eof ())
259
299
{
260
- if (tempEmployee.getEmployeeId () == empId)
300
+ if (tempEmployee.getId () == empId)
261
301
{
262
302
cout << " \n >>>>>>>>> Employee Found <<<<<<<<" << endl;
263
303
tempEmployee.showEmployeeData ();
@@ -288,39 +328,39 @@ int main()
288
328
}
289
329
290
330
// // dynamically allocate memory for n objects of Employee
291
- Employee *books = new Employee[n];
331
+ Employee *employees = new Employee[n];
292
332
293
- // // input, set and store books data
333
+ // // input, set and store employees data
294
334
cout << " \n >>>>>>>>>> Enter Data of " << n << " Employees <<<<<<<<<<<<<" << endl;
295
335
for (int i = 0 ; i < n; i++)
296
336
{
297
337
cout << " \n >>>>>>>>>>> Enter Data of Employee " << i + 1 << " <<<<<<<<<<<<" << endl;
298
338
299
- // // input and set book data
300
- books [i].inputEmployeeData ();
339
+ // // input and set employee data
340
+ employees [i].inputEmployeeData ();
301
341
302
- // // store book data
303
- if (!(books [i].storeEmployeeData ()))
342
+ // // store employee data
343
+ if (!(employees [i].storeEmployeeData ()))
304
344
{
305
345
cout << " \n !!! Employee Data Not Stored..." << endl;
306
346
return 0 ;
307
347
}
308
348
}
309
349
310
- // // books data stored successfully
350
+ // // employees data stored successfully
311
351
cout << " \n Employees Data Successfully Stored..." << endl;
312
352
313
- // // read and show books data
353
+ // // read and show employees data
314
354
cout << " \n >>>>>>>>>> Employees Data Stored In File <<<<<<<<<<<<<<" ;
315
355
fetchAndShowEmployeeData ();
316
356
317
- // // get book id to search a book in file
357
+ // // get employee id to search a employee in file
318
358
int empId;
319
359
320
360
cout << " \n Enter Employee Id to Search A Employee => " ;
321
361
cin >> empId;
322
362
323
- // // search book
363
+ // // search employee
324
364
searchEmployeeById (empId);
325
365
326
366
cout << endl; // Add new line
0 commit comments