The c++ program to solve the problem is shown below. For now, the name does not accept space-contained names (such as full names).
#include<iostream>
// for file operations
#include<fstream>
#include<cstring>
using namespace std;
class Employee{
private:
char name[30];
int age;
float salary;
public:
void read_data(){
cout<<"Enter Name:"<<endl;
// this does not accept space contained string
cin>>name;
cout<<"Enter Age:"<<endl;
cin>>age;
cout<<"Enter Salary:"<<endl;
cin>>salary;
}
void write_to_file(){
Employee emp;
ofstream file("employee_record.txt",ios::app);
emp.read_data();
file.write(reinterpret_cast<char *>(&emp), sizeof(emp));
}
void read_from_file(){
Employee emp;
ifstream file("employee_record.txt");
while(!file.eof()){
if (file.read(reinterpret_cast<char*>(&emp), sizeof(emp))){
emp.show_data();
}
}
}
void show_data(){
cout<<"Name : "<<name<<endl;
cout<<"Age : "<<age<<endl;
cout<<"Salary : "<<salary<<endl;
}
// function to display the employees with salaries > lowest_salary
void search_record(float lowest_salary){
Employee emp;
ifstream file("employee_record.txt");
cout<<"#### Employee with salary greater than 50000 are ####"<<endl;
for (int i = 0; !file.eof(); i++)
{
file.seekg(i * sizeof(emp));
file.read(reinterpret_cast<char *>(&emp), sizeof(emp));
if (emp.salary > lowest_salary)
{
emp.show_data();
}
}
}
};
int main(){
// record number can be any
int record_number;
cout<<"Enter Record Number :"<<endl;
cin>>record_number;
Employee emp;
for (int i = 0; i < record_number; i++)
{
cout<<endl<<"Employee:"<<i+1<<endl;
emp.write_to_file();
}
emp.search_record(50000);
return 0;
}
The sample output is shown below:
Enter Record Number :
10
Employee:1
Enter Name:
John
Enter Age:
34
Enter Salary:
49999
Employee:2
Enter Name:
David
Enter Age:
55
Enter Salary:
55555
Employee:3
Enter Name:
George
Enter Age:
66
Enter Salary:
66666
Employee:4
Enter Name:
Ryan
Enter Age:
23
Enter Salary:
33333
Employee:5
Enter Name:
Chris
Enter Age:
44
Enter Salary:
60000
Employee:6
Enter Name:
Brian
Enter Age:
34
Enter Salary:
50000
Employee:7
Enter Name:
Maria
Enter Age:
45
Enter Salary:
45000
Employee:8
Enter Name:
Aviva
Enter Age:
45
Enter Salary:
78780
Employee:9
Enter Name:
Greg
Enter Age:
23
Enter Salary:
33000
Employee:10
Enter Name:
Glen
Enter Age:
45
Enter Salary:
45000
#### Employee with salary greater than 50000 are ####
Name : David
Age : 55
Salary : 55555
Name : George
Age : 66
Salary : 66666
Name : Chris
Age : 44
Salary : 60000
Name : Aviva
Age : 45
Salary : 78780
Process returned 0 (0x0) execution time : 129.661 s
Press any key to continue.