Add file IO to program 4. Create a text file that will storeemployee information
ID: 3619389 • Letter: A
Question
Add file IO to program 4. Create a text file that will storeemployee information from the program code. The program will havethe ability to load the array with the data from the file.There are two required functions: LoadEmployee & SaveEmployeewhich look like the following, or something like the following. Thefollowing is also a possible base structure for the program.
#include "Employee.h"
void LoadEmployee(FILE *input, Employee *employee)
{}
void SaveEmployee(FILE *output, Employee employee)
{}
char InputMode()
{
// prompt user for input mode
// return input mode
return 'f';
}
void EntryPoint()
{
// create employee array
// create file
switch (InputMode())
{
case 'f':
case 'F':
// file
// load from file
break;
case 'k':
case 'K':
// keyboard
// load fromkeyboard
break;
default:
//
break;
}
// main process loop
// update employee record
// show employee record
// save records to file
}
--------------
PREVIOUS PROGRAMME 4
#include <stdio.h>
#include<stdlib.h>
#include <conio.h>
#include<string.h>
struct employee
{char name[10];
double rate;
double hours;
double tax;
double baseSalary;
double overtimePay;
double grossSalary;
double netPay;
}person[5];
void input(struct employee[]);
void calcpay(struct employee[]);
double calctax(struct employee);
void print(struct employee[],double);
int main()
{int i;
double total=0;
input(person);
calcpay(person);
for(i=0;i<5;i++)
{person[i].tax=calctax(person[i]); //calculatingtax
person[i].netPay=person[i].grossSalary-person[i].tax; //calculatingnet pay
total+=person[i].grossSalary;
}
print(person,total);
getch();
return 0;
}
void input(struct employee e[])
{int i;
for(i=0;i<5;i++)
{printf("Enter name: ");
scanf("%s",&e[i].name);
printf("Enter hourly rate: ");
scanf("%lf",&e[i].rate);
printf("Enter hours worked: ");
scanf("%lf",&e[i].hours);
//if the inputs are -1 break out of the loop
if(e[i].hours==-1||e[i].rate==-1||strcmp(e[i].name,"-1")==0)
{
exit(0);
}
}
}
void calcpay(struct employee e[])
{int i;
for(i=0;i<5;i++)
if(e[i].hours>40)
{
e[i].grossSalary=((e[i].hours-40)*(1.5*e[i].rate)+(e[i].rate*40));//gross pay
e[i].baseSalary=e[i].rate*40; //base salary incase of overtime
e[i].overtimePay= ((e[i].hours-40)*(e[i].rate*1.5)); //calculatingovertime pay
}
else
{
e[i].grossSalary=e[i].hours*e[i].rate;
e[i].baseSalary=e[i].hours*e[i].rate;
e[i].overtimePay =0;
}
}
double calctax(struct employee e)
{return e.grossSalary*.2;
}
void print(struct employee e[],double total)
{int i;
printf(" ");
for(i=0;i<5;i++)
{printf("Pay to: %s ",e[i].name);
printf("Hourly rate:%.2f ",e[i].rate);
printf("Hours worked:%.2f ",e[i].hours);
printf("Gross Pay:$%.2f ",e[i].grossSalary);
printf("Base Pay:$%.2f ",e[i].baseSalary);
printf("Overtime Pay:$%.2f ",e[i].overtimePay);
printf("Taxes paid:$%.2f ",e[i].tax);
printf("Net Pay:$%.2f ",e[i].netPay);
}
printf(" Total pay:$%.2f ",total);
}
Explanation / Answer
void LoadEmployee(FILE *input, Employee *employee)
{ if(fread(employee,1,sizeof(Employee),input) <= 0) printf("Error in loading from inputfile"); else printf("Data successfulyloaded"); }
//here is load and storefunction, please provide Employee.h for completeprogram.
void SaveEmployee(FILE *output, Employee employee) { if(fwrite(&employee,1,sizeof(Employee),output) <= 0) printf("Error in writtingfile"); else printf("Data successfuly written tofile"); } void LoadEmployee(FILE *input, Employee *employee)
{ if(fread(employee,1,sizeof(Employee),input) <= 0) printf("Error in loading from inputfile"); else printf("Data successfulyloaded"); }
//here is load and storefunction, please provide Employee.h for completeprogram.
void SaveEmployee(FILE *output, Employee employee) { if(fwrite(&employee,1,sizeof(Employee),output) <= 0) printf("Error in writtingfile"); else printf("Data successfuly written tofile"); }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.