Suppose, you are running a company where 50 employees work.

 4. Suppose, you are running a company where 50 employees work.

a. Design a structure name Employee to store their Name, Age, Basic salary, Bonus and any

other thing that is needed.

b. Take all the data for all the employees from user.


c. Write a function name emp_bonus(). For those persons who are older than 50 years, will get a

bonus which is 40% of their basic salary. The rest of the people will get 25% bonus of their

salary. Print the name and the total salary of every employee.

d. Open a text file name Employee_Data. If the file opens successfully, write all the employees

name and age on that file.

e. Now, create function named search_age() in a way that if you search by an employee name, it

can find out the age of that person by reading the file Employee_Data.txt.


CODE IN C++: #include<bits/stdc++.h> using namespace std; // Structure EMployee struct Employee { string name; int age; float basic_salary; float bonus; }; // Definition of function emp_bounus() void emp_bonus( Employee &emp ) { if(emp.age >= 50) emp.bonus = 0.40 * emp.basic_salary; else emp.bonus = 0.25 * emp.basic_salary; } // Definition of function search_age() int search_age(string name) { fstream file; string line;

file.open

("Employee_Data.txt",ios::in); cout<<"\nSearching in File.....\n"; while(file) { getline(file , line); if(line == "-1") return -1; if(line == name) { getline(file , line); return stoi(line); } getline(file , line); } file.close(); return -1; } // main()function int main() { int emp_size = 5; Employee emp[emp_size]; cout<<"Enter the details of "<<emp_size<<" employees->"; for(int i = 0 ; i < emp_size ; i++) { cout<<"\nEmployee "<<i+1<<" :\n"; cout<<"\tEnter Employee's Name: "; cin>>emp[i].name; cout<<"\tEnter Employee's Age: "; cin>>emp[i].age; cout<<"\tEnter Employee's Basic Salary: "; cin>>emp[i].basic_salary; emp_bonus(emp[i]); cout<<"\tBonus: "<<emp[i].bonus; } fstream filee;

filee.open

("Employee_Data.txt", ios::out | ios::trunc); for(int i = 0 ; i < emp_size ; i++) { filee<<emp[i].name<<endl; filee<<emp[i].age<<endl; } filee<<"-1"; filee.close(); cout<<"\n\nEmployee's names and age has been written to Employee_Data.txt file.."; cout<<"\n\nEnter Employee Name to be searched: "; string name; cin>>name; int age = search_age(name); if( age == -1 ) cout<<"Employee Not Found!!\n"; else cout<<"\nAge: "<<age<<endl; return 0; } Employee_Data.txt


Image 01:

https://media.cheggcdn.com/coop/fa2/fa29fd51-4236-4400-b01b-717237f092ce/1620985564263_image.png

OUTPUT: Image 02:

https://media.cheggcdn.com/coop/74b/74b21c12-7a6f-4a8c-9477-9ae944a00093/1620985549168_image.png








Comments