Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//structure for employee data
struct edata {
char name[50];
float hours;
float rate;
};
//prototype
int printdata(struct edata * print);
//function for employee data
//pass structure to function by reference
int emplydata(struct edata * data)
{
printf("Enter employee name:\n");
scanf_s("%s", &data->name, 20);
if (strcmp(data->name, "-1") == 0)
return -1;
printf("Enter hours worked:\n");
scanf_s("%f", &data->hours);
if (data->hours == -1)
return -1;
printf("Enter hourly rate:\n");
scanf_s("%f", &data->rate);
if (data->rate == -1)
return -1;
}
//funtion to calculate gross, base, and overtime pay
int paycalc(struct edata * gr)
{
float overpay;
float gross;
float basepay;
float overtime = 1.5;
//variables hr and rt have the value of pointers
if (gr->hours > 40) {
basepay = (40 * gr->rate);
gross = (40 * gr->rate) + ((gr->hours - 40)*(gr->rate * overtime));
overpay = (gr->hours - 40)*(gr->rate * overtime);
printf("Gross pay: %.2lf\n", gross);
printf("Base pay: %.2lf\n", basepay);
printf("Overtime pay: %.2lf\n", overpay);
}
else {
basepay = (gr->hours * gr->rate);
gross = basepay;
overpay = 0;
printf("Gross pay: %.2lf\n", gross);
printf("Base pay: %.2lf\n", basepay);
printf("Overtime pay: %.2lf\n", overpay);
}
//returns the value of gross pay
return gross;
}
//function to calculate taxes and pay after tax is taken from gross
int taxcalc(struct edata * tax, float g)
{
float taxpay;
float taxrate = .2;
if (tax->hours > 40) {
taxpay = taxrate * g;
printf("Tax pay: %.2lf\n", taxpay);
}
else {
taxpay = taxrate * g;
printf("Tax pay: %.2lf\n", taxpay);
}
return taxpay;
}
int fileinput(struct edata * fileemply, float gr, float * tg)
{
FILE * userfile;
char chfl[50];
errno_t err;
printf("Which file would you like to load from?\n");
scanf_s("%s", &chfl, 50);
err = fopen_s(&userfile, chfl, "r+");
if (err != 0)
{
printf("Error\n");
return -1;
}
do {
fscanf_s(userfile, " %s", fileemply->name, 50);
fscanf_s(userfile, " %f", &fileemply->hours);
fscanf_s(userfile, " %f", &fileemply->rate);
printf(" %s", fileemply->name);
printf(" %f", fileemply->hours);
printf(" %f", fileemply->rate);
gr = paycalc(fileemply);
taxcalc(fileemply, gr);
*tg += gr;
} while (!feof(userfile));
fclose(userfile);
}
int keyboard(struct edata * keyemply, float gr, float * tg)
{
emplydata(keyemply);
gr = (paycalc(keyemply));
taxcalc(keyemply, gr);
*tg += gr;
}
int main(void)
{
FILE * emplyfile;
//variables
//array 6 so five employees are still listed while skipping array 0
struct edata emply[10];
int i;
int menu;
float totalgross = 0;
float gross = 0;
int editnum;
int input;
char answer;
char filename[20];
printf("Method of input:\n");
printf("Would you like to load from a file or input from keyboard?\n");
printf("1. Keyboard\n");
printf("2. File\n");
scanf_s("%d", &input);
switch (input)
{
case 1:
for (i = 1; i < 6; i++) {
keyboard(&emply[i], gross, &totalgross);
}
printf("Net pay of all employees is: %.2f\n", totalgross);
break;
case 2:
fileinput(&emply, gross, &totalgross);
printf("Net pay of all employees is: %.2f\n", totalgross);
break;
}
do {
printf("Menu:\n");
printf("1. Update an employee's data\n");
printf("2. Print an employee's data\n");
printf("3. Print all employee's data\n");
printf("4. Exit program\n");
printf("What would you like to do?\n");
scanf_s("%d", &menu);
printf("\n");
//menu switch to choose options
switch (menu)
{
//edit a single employee's data by manipulating array using load employee data function
case 1:
printf("Which employee record would you like to edit?\n");
printf("1. %s\n", emply[1].name);
printf("2. %s\n", emply[2].name);
printf("3. %s\n", emply[3].name);
printf("4. %s\n", emply[4].name);
printf("5. %s\n", emply[5].name);
scanf_s("%d", &editnum);
emplydata(&emply[editnum]);
break;
//print single employee data through array editnum
case 2:
printf("Which employee record would you like to print?\n");
printf("1. %s\n", emply[1].name);
printf("2. %s\n", emply[2].name);
printf("3. %s\n", emply[3].name);
printf("4. %s\n", emply[4].name);
printf("5. %s\n", emply[5].name);
scanf_s("%d", &editnum);
//printdata(emply[i].name);
printdata(&emply[editnum]);
break;
//print all employee data
case 3:
for (i = 1; i < 6; i++) {
printdata(&emply[i]);
};
break;
//exit program
case 4:
//ask to save file
printf("Exiting program...\n");
break;
}
} while (menu != 4);
system("pause");
return 0;
}
//function to print all employee data
int printdata(struct edata * print)
{
float overpay;
float gross;
float basepay;
float taxpay;
float overtime = 1.5;
float taxrate = .2;
printf("Employee name: %s\n", print->name);
printf("Employee hours: %.2lf\n", print->hours);
printf("Employe rate: %.2lf\n", print->rate);
if (print->hours > 40) {
basepay = (40 * print->rate);
gross = (40 * print->rate) + ((print->hours - 40)*(print->rate * overtime));
overpay = (print->hours - 40)*(print->rate * overtime);
printf("Gross pay: %.2lf\n", gross);
printf("Base pay: %.2lf\n", basepay);
printf("Overtime pay: %.2lf\n", overpay);
}
else {
basepay = (print->hours * print->rate);
gross = basepay;
overpay = 0;
printf("Gross pay: %.2lf\n", gross);
printf("Base pay: %.2lf\n", basepay);
printf("Overtime pay: %.2lf\n", overpay);
}
if (print->hours > 40) {
taxpay = taxrate * gross;
printf("Tax pay: %.2lf\n", taxpay);
}
else {
taxpay = taxrate * gross;
printf("Tax pay: %.2lf\n", taxpay);
}
return 0;
}