Write a program using a class that has array data members NAME, SALARY, YEAR_HIR
ID: 3605622 • Letter: W
Question
Write a program using a class that has array data members NAME, SALARY, YEAR_HIRED. This program will
1) write the data for 10 records to a random access file
2) allow the user to output the name, salary, hire_date for a selected record
3) allow the user to change the name, salary, or hire_date for a selected record.
4) allow the user to add a record.
Note: The record number is the sbscript of the array for NAME, SALARY, DATE_HIRED.
PLEASE FIRST WRITE A PROGRAM THAT WRITES NAME TO A RANDOM ACCESS FILE AND READS IT
*************** MAKE SURE THIS WORKS !!*****************
***********THEN MAKE NAME, SALARY, DATE_HIRED ARRAYS and MAKE SURE IT WORKS *********
***************THEN MAKE YOUR PROGRAM WRITE and READ 10 RECORDS. . MAKE SURE THIS WORKS **********
***************** FINALLY ALLOW USER TO OUTPUT AND MODIFY SELECTED RECORD.***********
c++
Explanation / Answer
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class Employee
{
char name[30],
float salary;
int year_hired;
public:
void accept()
{
cout<<"Enter Name of employee:";
cin>>name;
cout<<"Enter salary :";
cin>>salary;
cout<<"Enter Year of hiring :";
cin>>year_hired;
}
void display()
{
cout<<setw(30)<<name
<<setw(10)
<<setprecision(2)
<<setiosflags(ios::showpoint)
<<salary
<<setw(10)<<year_hired
<<endl;
}
};
int main()
{
fstream f1;
Employee e1;
f1.open("EMP.DAT",ios::binary | ios::ate | ios::in | ios::out);
//add new employee records
f1.seekp(0,ios::end);
f1.clear;
cout<<"Enter employee details to add: ";
e1.accept();
f1.write((char *)&e1 , sizeof(e1));
cout<<"employee record saved";
//display the records
f1.seekg(0); //move pointer to the start of file
while(f1.read(char *)&e1 , sizeof(e1)))
{
e1.display();
}
//modify existing records
cout<<"Enter record to modify:";
int m;
cin>>m;
int pos=(m-1)*sizeof(e1);
f1.seekp(pos , ios::beg); //move put pointer to mth record
f1.clear();
cout<<"Enter new values of the emplyee:";
e1.accept();
f1.write((char *)&e1 , sizeof(e1));
cout<<" emplyee record is modified";
f1.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.