@@ -5,7 +5,7 @@ a. Emp Id
55b. Emp Name
66c. Emp Address
77d. Emp Dept (Admin/Sales/Production/IT)
8- e. Emp Phone
8+ e. Emp Phone Number
99f. Emp Age
1010
1111Write a C++ program to read the above file. Create a new file such as adm.dat,
@@ -36,7 +36,7 @@ class Employee
3636private:
3737 // // instance member variables
3838 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];
4040
4141public:
4242 // // constructors
@@ -47,15 +47,15 @@ class Employee
4747 name[0 ] = 0 ;
4848 address[0 ] = 0 ;
4949 department[0 ] = 0 ;
50- phone [0 ] = 0 ;
50+ phoneNumber [0 ] = 0 ;
5151 }
5252
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)
5454 {
5555 this ->id = id;
5656 this ->age = age;
5757 strcpy (this ->name , name);
58- strcpy (this ->phone , phone );
58+ strcpy (this ->phoneNumber , phoneNumber );
5959 strcpy (this ->address , address);
6060 strcpy (this ->department , department);
6161 }
@@ -102,16 +102,16 @@ class Employee
102102 return name;
103103 }
104104
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 )
107107 {
108- strcpy (this ->phone , phone );
108+ strcpy (this ->phoneNumber , phoneNumber );
109109 }
110110
111- // // instance member function to get phone
112- const char *getPhone ()
111+ // // instance member function to get phoneNumber
112+ const char *getPhoneNumber ()
113113 {
114- return phone ;
114+ return phoneNumber ;
115115 }
116116
117117 // // instance member function to set address
@@ -141,45 +141,85 @@ class Employee
141141 // // instance member function to input and set employee data
142142 void inputEmployeeData ()
143143 {
144- int empId;
145- char bookTitle[Employee::MAX_CHARS_IN_TITLE];
146- double bookPrice;
144+ int id;
147145
148- // // Get book id
146+ // // get and set employee id
149147 cout << " \n Enter Employee Id => " ;
150- cin >> empId ;
148+ cin >> id ;
151149
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 << " ) => " ;
154155 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;
156161
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);
160166
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+ }
165202 }
166203
167- // // instance member function to show book data
168- void showEmployeeData ()
204+ // // instance member function to show employee data
205+ void showEmployeeData () const
169206 {
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;
173213 }
174214
175- // // instance member function to store book record
215+ // // instance member function to store employee data
176216 int storeEmployeeData ()
177217 {
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
180220
181221 // // specify file name
182- const char *fileName = " books_data .dat" ;
222+ const char *fileName = " emp .dat" ;
183223
184224 // create an instance of ofstream for writing in a file
185225 ofstream fout;
@@ -189,19 +229,19 @@ class Employee
189229
190230 // // check if the file is successfully opened
191231 if (!fout.is_open ())
192- return 0 ; // book data not stored
232+ return 0 ; // employee data not stored
193233
194234 fout.write ((char *)this , sizeof (*this ));
195235
196- return 1 ; // book data successfully stored
236+ return 1 ; // employee data successfully stored
197237 }
198238};
199239
200- // // function to fetch books data from a fike and show
240+ // // function to fetch employees data from a fike and show
201241void fetchAndShowEmployeeData ()
202242{
203243 // // specify file name
204- const char *fileName = " books_data .dat" ;
244+ const char *fileName = " emp .dat" ;
205245
206246 // create an instance of ifstream for reading from a file
207247 ifstream fin;
@@ -230,11 +270,11 @@ void fetchAndShowEmployeeData()
230270 }
231271}
232272
233- // // function to search a book from file using book id
273+ // // function to search a employee from file using employee id
234274void searchEmployeeById (int empId)
235275{
236276 // // specify file name
237- const char *fileName = " books_data .dat" ;
277+ const char *fileName = " employees_data .dat" ;
238278
239279 // create an instance of ifstream for reading from a file
240280 ifstream fin;
@@ -257,7 +297,7 @@ void searchEmployeeById(int empId)
257297
258298 while (!fin.eof ())
259299 {
260- if (tempEmployee.getEmployeeId () == empId)
300+ if (tempEmployee.getId () == empId)
261301 {
262302 cout << " \n >>>>>>>>> Employee Found <<<<<<<<" << endl;
263303 tempEmployee.showEmployeeData ();
@@ -288,39 +328,39 @@ int main()
288328 }
289329
290330 // // dynamically allocate memory for n objects of Employee
291- Employee *books = new Employee[n];
331+ Employee *employees = new Employee[n];
292332
293- // // input, set and store books data
333+ // // input, set and store employees data
294334 cout << " \n >>>>>>>>>> Enter Data of " << n << " Employees <<<<<<<<<<<<<" << endl;
295335 for (int i = 0 ; i < n; i++)
296336 {
297337 cout << " \n >>>>>>>>>>> Enter Data of Employee " << i + 1 << " <<<<<<<<<<<<" << endl;
298338
299- // // input and set book data
300- books [i].inputEmployeeData ();
339+ // // input and set employee data
340+ employees [i].inputEmployeeData ();
301341
302- // // store book data
303- if (!(books [i].storeEmployeeData ()))
342+ // // store employee data
343+ if (!(employees [i].storeEmployeeData ()))
304344 {
305345 cout << " \n !!! Employee Data Not Stored..." << endl;
306346 return 0 ;
307347 }
308348 }
309349
310- // // books data stored successfully
350+ // // employees data stored successfully
311351 cout << " \n Employees Data Successfully Stored..." << endl;
312352
313- // // read and show books data
353+ // // read and show employees data
314354 cout << " \n >>>>>>>>>> Employees Data Stored In File <<<<<<<<<<<<<<" ;
315355 fetchAndShowEmployeeData ();
316356
317- // // get book id to search a book in file
357+ // // get employee id to search a employee in file
318358 int empId;
319359
320360 cout << " \n Enter Employee Id to Search A Employee => " ;
321361 cin >> empId;
322362
323- // // search book
363+ // // search employee
324364 searchEmployeeById (empId);
325365
326366 cout << endl; // Add new line
0 commit comments