Program Description Add file IO to program 4(I have post the program 4 below aft
ID: 3540045 • Letter: P
Question
Program Description
Add file IO to program 4(I have post the program 4 below after the lines ===========)) . Create a plain-text file that will store employee information from the program code. The program will have the ability to load the array with the data from the file.
When the user chooses to load from a file, ask for the name of the file, then load the data into the employee array. If they choose keyboard, load the data as was done previously. Note that the application is going to want to use the folder where the .exe file is located when looking for the file, unless we type in a full or relative path. Using the debug folder is fine for our purposes.
Once the data is loaded into our array, either from an already existing file, or from keyboard input, we will then have our menu from the previous assignment.
Before the program exits, you will need to ask for the name of the file to save the data to. If we loaded from a file already, we will use the same file and overwrite the contents.
We don%u2019t need to cover deleting employee data. We can also assume that we only need to handle at most 10 employees worth of data.
There are two required functions: LoadEmployee & SaveEmployee which look something like the following. you can use the following 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 from keyboard
break;
default:
//
break;
}
// main process loop
// update employee record
// show employee record
// save records to file
}
=======================================================================
above was the question and the program 4 which was asked in the beginning of
the question i will post below use this program 4 to update to program 5
========================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct userdata{
char name[20];
float rate;
float worked;
float basepay;
float overtimePay;
float grosspay;
float earned;
float taxespaid;
float paid;
int userintputformenu;
};
int input(struct userdata *userinput)
{
printf("Enter name: ");
scanf_s("%s", &userinput->name, 20);
if(strcmp(userinput->name, "-1") == 0)
{return -1;}
printf("Enter hourly rate: ");
scanf_s("%f", &userinput->rate);
if (userinput->rate == -1)
{return -1;}
printf("Enter hours worked: ");
scanf_s("%f", &userinput->worked);
printf(" ");
if (userinput->worked == -1)
{return -1;}
return 0;
}
void calc(struct userdata *userinput)
{
userinput->overtimePay = 0;
if (userinput->worked > 40)
{
userinput->basepay = userinput->rate *40;
userinput->overtimePay = (userinput->worked - 40) * (userinput->rate * 1.5f);
}
else{
userinput->basepay = userinput->rate * userinput->worked;
}
userinput->grosspay = userinput->basepay + userinput->overtimePay;
userinput->earned = userinput->basepay + userinput->overtimePay;
userinput->taxespaid = userinput->earned * 0.2f;
userinput->paid = userinput->earned - userinput->taxespaid;
}
void load(struct userdata *userinput,int x){
int i;
--x;
printf("Enter data for %d employees : ",x);
for(i=1;i<=x;i++){
input(&userinput[i]);
}
}
int main()
{
int x=3;
int userintputformenu;
int i;
struct userdata userinput[20];
printf("Enter total no. of employees ");
scanf_s("%d", &x);
++x;
load(userinput,x);
printf(" Main Menu: ");
printf(" 1) Print single employee ");
printf(" 2) Print all the employess ");
printf(" 3) Edit an employee ");
printf(" 4) Exit the program ");
scanf_s("%d", &userintputformenu);
while(userintputformenu!=4)
{
switch(userintputformenu)
{
case 1:
//printf("Enter employee number ");
printf("choose an employee to print. ");
for(i=1;i<x;++i){
printf(" %d. %s", i, userinput[i].name);
}
printf(" ");
scanf_s("%d", &i);
calc(&userinput[i]);
printf(" Pay to: %s ", userinput[i].name);//prints user
printf("Hours worked: %.1f ", userinput[i].worked);//prints user's hours worked
printf("Hourly rate: $ %.2f ", userinput[i].rate);//prints user's hourlyRate
printf("Gross pay: $ %.2f ", userinput[i].grosspay);
printf("Base pay: $ %.2f ", userinput[i].basepay);
printf("Overtime pay: $ %.2f ", userinput[i].overtimePay);
printf("Taxes paid: $ %.2f ", userinput[i].taxespaid);//prints how much user paid taxes
printf("Net pay: $ %.2f ", userinput[i].paid);//prints user's paid check
break;
case 2:
for(i=1;i<x;++i)
{
calc(&userinput[i]);
printf(" Pay to: %s ", userinput[i].name);//prints user
printf("Hours worked: %.1f ", userinput[i].worked);//prints user's hours worked
printf("Hourly rate: $ %.2f ", userinput[i].rate);//prints user's hourlyRate
printf("Gross pay: $ %.2f ", userinput[i].grosspay);
printf("Base pay: $ %.2f ", userinput[i].basepay);
printf("Overtime pay: $ %.2f ", userinput[i].overtimePay);
printf("Taxes paid: $ %.2f ", userinput[i].taxespaid);//prints how much user paid taxes
printf("Net pay: $ %.2f ", userinput[i].paid);//prints user's paid check
}
break;
case 3:
printf("choose an employee to edit. ");
for(i=1;i<x;++i){
printf(" %d. %s", i, userinput[i].name);
}
printf(" ");
scanf_s("%d", &i);
printf(" ");
printf("Enter name: ");
scanf_s("%s", &userinput[i].name, 20);
printf("Enter hourly rate: ");
scanf_s("%f", &userinput[i].rate);
printf("Enter hours worked: ");
scanf_s("%f", &userinput[i].worked);
printf(" ");
break;
case 4:
break;
}
printf(" Main Menu: ");
printf(" 1) Print single employee ");
printf(" 2) Print all the employess ");
printf(" 3) Edit an employee ");
printf(" 4) Exit the program ");
scanf_s("%d", &userintputformenu);
}
system("pause");
}
Explanation / Answer
Load the Employee.h file.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.