Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help finishing this program which mainly includes steps 2 and 3 which I\'

ID: 656519 • Letter: I

Question

I need help finishing this program which mainly includes steps 2 and 3 which I'm not really sure how to do. Does anyone want to fill in the blanks?

1)Write a code to implement the structure data type Employee. Employee has three
members: int employeeId, string employeeName and string departmentName.


2) Implement a function saveEmployee that accepts fstream fileStream and
Employee emp as arguments. The function will use fileStream to write emp. The
data will be written by using the append mode and the following format:
employeeId;employeeName;departmentName.


3) Implement a function getEmployeeList that has the arguments: fstream
fileStream and int n. The function will return an array employeeArray of length
n
. The function will read n employees from the file and save them in
employeeArray. (you will use the string functions to parse each line)


4) In the main function, you ask the user to enter the number of Employee records
that he wants to save and the file name that he wants to use. You will use the file
name to create an fstream object. After finishing the writing operation, you will use
the same stream object
to read all the saved records and display them on the
console. The fstream object will be closed after finishing the reading and writing
operations.

I mainly need help with parts 3 and 4. Could anyone help me fill in the blanks with the template below?

#include <iostream>
#include <fstream>

using namespace std;

struct employee
{
   int employeeId;
   char employeeName;
   char employeeDepartment;
}

int main()
{
int n;
string fileName;

cout << "Welcome to the Employee Management Program" << endl;

cout << "Enter the number of Employee Records to save." << endl;

cin >> n;

cout << "Enter a file name for the Employee Records." << endl;

cin >> fileName;

}

Explanation / Answer

#include <iostream.h>
#include <fstream.h>

using namespace std;

struct employee
{
int employeeId;
char employeeName[50];
char employeeDepartment[20];
};

void getEmployeeList(ofstream infile, int n)
{
string line;
int count=0;

while(read(infile,line) && count<=n)
{
std::stringstream linestream(line);
std::string value;
count=count+1;
while(getline(linestream,value,';'))
{
   std::cout << value << " ";
}
std::cout << std::endl;

}

}

void saveEmployee(ofstream outfile,struct employee emp)
{
string str;
strcpy(str,emp.employeeId);
strcat(str,';');
strcat(str,emp.employeeName);
strcat(str,';');
strcat(str,emp.employeeDepartment);
outfile.write ( str, sizeof(str) );
outfile.close();
}

int main()
{
int n,i;
string fileName;
struct employee e[50];

cout << "Welcome to the Employee Management Program" << endl;
cout << "Enter the number of Employee Records to save." << endl;
cin >> n;
cout << "Enter a file name for the Employee Records." << endl;
cin >> fileName;
clrscr();
cout<<"Enter Employee details";
ofstream outfile;
for(i=1;i<=n;i++)
{
cout<<" Employee:"<<i<<" ";
cin>>e[i].employeeId;
cin>>e[i].employeeName;
cin>>e[i].employeeDepartment;
outfile.open (fileName.c_str(), ios::out|ios::binary|ios::app);
saveEmployee(outfile,emp);
outfile.close();
}
ofstream infile;
infile.open (filename.c_str(), ios::in|ios::binary);
cout<<"Employee details";
getEmployeeList(infile,n);
infile.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote