Write aprogram that inputs the employee records from the user and saves itto a f
ID: 3615538 • Letter: W
Question
Write aprogram that inputs the employee records from the user and saves itto a file called employee.dat. The program opens the file again,reads all the records from the file employee.dat, stores them intoa one-dimensional array, and then displays the contents of allrecords. Create a structure to store the employeerecord.
structEmployee
{ char nam[80];
double salary;
};
Output sample
Entername:
aa
Enter salary:
45.8
Do you want to enter another record?y
Enter name:
bb
Enter salary:
23.6
Do you want to enter another record?y
Enter name:
cc
Enter salary:
90.5
Do you want to enter another record?y
Enter name:
dd
Enter salary:
100.35
Do you want to enter another record?n
EMPLOYEE RECORDS:
aa
45.8
bb
23.6
cc
90.5
dd
100.35
Hint: Review the codingon pages 639 and 640 in the the text book for fileprocessing.
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib>
using namespace std;
struct Employee
{ char name[80];
double salary;
};
int main()
{
Employee e;
char again;
//Open an output file called employee.dat for writing only
____________ employeeoutputfile("employee.dat",_________________________);
do
{ cout<<"Enter name:"<<endl;
cin.getline(e.name,80);
cout<<"Enter salary:"<<endl;
cin>>e.salary;
//Write an employee record to the file
employeeoutputfile.write(________________, sizeof(_____));
cout<<"Do you want to enter another record?";
cin>>again;
cin.ignore();
}while(again =='Y' || again=='y');
//Close the file
employeeoutputfile.___________;
//Create an array called earray to store 10 Employee structurevariables
_________ earray____________;
int i =0;
//Open an input file called employee.dat for reading only
__________ employeeinputfile(_______ , __________);
//Read the firstrecord
employeeinputfile.______________(_________________, sizeof(e));
while(!________________ ) //Check for end of file
{ strcpy( earray[i].name,e.name);//get name
________ = e.salary; //get salary
i++;
//get another record
employeeinputfile.______________(_________________,sizeof(e));
}
employeeinputfile.close(); //close the fiel
//Display th records
cout<<"EMPLOYEE RECORDS:"<<endl;
_______________________
}
struct Employee
{
char name[80];
double salary;
};
int main()
{
Employee e;
char again;
//Open an output file called employee.dat for writingonly
fstream employeeoutputfile("employee.dat", ios::out);
do
{
cout<<"Entername:"<<endl;
cin.getline(e.name, 80);
cout<<"Entersalary:"<<endl;
cin>>e.salary;
//Write an employee record to thefile
employeeoutputfile.write( e.name ,sizeof(e.name));
cout<<"Do you want to enteranother record?";
cin>>again;
cin.ignore();
}while( again =='Y' || again=='y');
//Close the file
employeeoutputfile.close();
//Create anarray called earray to store 10 Employee structure variables
char earray[10];
int i =0;
//Open an input file called employee.dat for readingonly
fstream employeeinputfile( "employee.dat" , ios::in);
//Read the first record
employeeinputfile.read( earray, sizeof(e));
while( !employeeinputfile.eof() ) //Check for end of file
{
strcpy( earray[i].name,e.name); //get name
earray[i] =e.salary; //get salary
i++;
//get another record
employeeinputfile.read( earray,sizeof(e));
}
employeeinputfile.close(); //close the file
//Display the records
cout<<"EMPLOYEE RECORDS:"<<endl;
// ____________________
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
struct Employee
{
char name[80];
double salary;
};
int main()
{
Employee e;
char again;
//Open an output file called employee.dat for writingonly
fstream employeeoutputfile("employee.dat", ios::out);
do
{
cout<<"Enter name:"<<endl;
cin.getline(e.name, 80);
cout<<"Enter salary:"<<endl;
cin>>e.salary;
//Write an employee record to the file
employeeoutputfile<<e.name<<endl;
employeeoutputfile<<e.salary<<endl;
cout<<"Do you want to enter anotherrecord?";
cin>>again;
cin.ignore();
}while( again =='Y' || again=='y');
//Close the file
employeeoutputfile.close();
//Create an array called earray to store 10 Employeestructure variables
Employee earray[10];
int i =0;
//Open an input file called employee.dat for readingonly
fstream employeeinputfile( "employee.dat" , ios::in);
//Read the first record
//employeeinputfile.read( earray, sizeof(e));
employeeinputfile>>earray[0].name;
employeeinputfile>>earray[0].salary;
while( !employeeinputfile.eof() ) //Check for end of file
{
//strcpy( earray[i].name, e.name); //get name
employeeinputfile>>earray[i].name;
employeeinputfile>>earray[i].salary;//getsalary
i++;
//get another record
//employeeinputfile.read( earray,sizeof(e));
}
employeeinputfile.close(); //close the file
//Display the records
cout<<"EMPLOYEERECORDS:"<<earray[0].name<<endl;
// ____________________
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.