Code:
// Creates a Payroll Register from an Employee File Saved on the Computer
#include <stdio.h>
#include <stdlib.h>
//Function Declarations
int getEmpData (FILE* spEmpDataIn,
int* empNum, int* dept, float* rate, char* exempt, int* hrs);
int writeEmpData (FILE* spEmpDataOut,
int empNum, int dept, float rate, char exempt, int hrs,
float basePay);
void calcPay (float rate, int hrs, float* basePay);
int main (void)
{
//Local Declarations
FILE* spEmpDataIn;
FILE* spEmpDataOut;
int empNum;
int dept;
float rate;
char exempt;
int hrs;
float basePay;
//Statements
printf("Creating Payroll Register...\n");
if (!(spEmpDataIn = fopen ("C:\\Employee.txt", "r")))
{
printf("Could not open file\a\n");
return 100;
} //if open input
if (!(spEmpDataOut = fopen ("C:\\PayrollRegister.txt", "w")))
{
printf("Error Opening Payroll Register\a\n");
return 102;
} //if open output
while (getEmpData
(spEmpDataIn, &empNum, &dept, &rate, &exempt, &hrs))
{
calcPay (rate, hrs, &basePay);
writeEmpData (spEmpDataOut, empNum, dept, rate, exempt, hrs, basePay);
} //while
fclose (spEmpDataIn);
fclose (spEmpDataOut);
printf("Payroll Register Completed\n");
return 0;
} //main
/*=====================getEmpData=====================
Reads data from the employee file.
Pre spEmpDataIn is an open file
empNum, dept, rate, exempt, hrs pass by address
Post reads employee information
if data read --returns 1
if EOF or error--returns 0
*/
int getEmpData (FILE* spEmpDataIn, int* empNum, int* dept,
float* rate, char* exempt, int* hrs)
{
//Local Declarations
int ioResult;
//Statements
ioResult = fscanf(spEmpDataIn, "%4d%2d%4f%1c%2d", &empNum, &dept, &rate, &exempt, &hrs);
if(ioResult == EOF)
return 0;
else
return 1;
} //getEmpData
/*===========================calcPay===========================
Determines base pay based on the employees pay rate and hours worked.
Pre rate and hrs contain employee pay rate and hours worked
basePay is the adress of the variable
Post Base Pay is copied to the address
*/
void calcPay (float rate, int hrs, float* basePay)
{
//Statements
*basePay = rate * hrs;
return;
} //calcPay
/* =======================writeEmpData=======================
Write payroll register to output file.
Pre spEmpDataOut is an open file
empNum,dpt, rate, exempt, hrs, and basePay have values to write
Post Data written to file
*/
int writeEmpData (FILE* spEmpDataOut, int empNum, int dept,
float rate, char exempt, int hrs,
float basePay)
{
//Statements
fprintf(spEmpDataOut, "%-4d %d %f %c %d %f\n",
empNum, dept, rate, exempt, hrs, basePay);
return 0;
} //writeEmpData