Write a C program using Microsoft Visual Studio Write a payroll program that rea
ID: 3604344 • Letter: W
Question
Write a C program using Microsoft Visual Studio
Write a payroll program that reads employee payroll data from a file and performs payroll processing.
Write a program that accomplishes the following
: Reads up to 100 payroll records into an array of structs from a file in the form of: firstName (string) lastName (string) hourlyRate (double) hoursWorked (double).
Calculates the gross pay (hourlyRate * hoursWorked) for each employee.
Calculates the taxes for each employee (0% tax for under $100, 10% tax for $100-$499.99, 20% tax from $500-$999.99, and 30% tax from $1,000 and above).
Calculates the net pay for each employee (gross pay - taxes).
Prints the entire employee record, gross pay, taxes, and net pay to the screen for each employee.
Use a separate function to read the employee records from a file into an array.
Use a separate function to calculate the pay for each employee.
Use a separate function to print the employee record.
Terminate the program and print an error that includes the line number in the file if any payroll records contain an error (missing or invalid data). Negative hours worked and a negative or 0 hourly rate should be considered errors.
Explanation / Answer
here is your program : ---------------------------->>>>>>>>>>>>>>>>>>>>>>>>>
#include<stdio.h>
/*struct definition for storing the data from file*/
struct payRoll{
char fname[20];
char lname[20];
double hourlyRate;
double hoursWorked;
double grossPay;
double netPay;
double taxes;
};
/*for reading the data from file*/
int readPayRoll(struct payRoll *p){
FILE *fp;
char file[50];
int c=0;
printf(" Enter the file name to read the Pay Roll ");
scanf("%s",&file);
fp = fopen(file,"r");
while (fscanf(fp, "%s %s %1f %1f", p[c].fname,p[c].lname,p[c].hourlyRate,p[c].hoursWorked) != EOF){
if(p[c].hoursWorked < 0){
printf(" Hourly worked is not correct in line %d in file",c);
return 0;
}
if(p[c].hourlyRate <= 0){
printf(" Hourly rate is not correct in line %d in file",c);
return 0;
}
}
if(fp == NULL){
return 0;
}
fclose(fp);
return 1;
}
/*function definition to calculate gross pay */
void calculatePay(struct payRoll *p){
int c = 0;
for(c;c<100;c++){
p[c].grossPay = p[c].hourlyRate*p[c].hoursWorked;
}
}
/*function definition to calculate taxes of each employee*/
void calculateTax(struct payRoll *p){
int c=0;
for(c;c<100;c++){
if(p[c].grossPay < 100){
p[c].taxes = 0;
}
if(p[c].grossPay >= 100 && p[c].grossPay <= 499.99){
p[c].taxes = (p[c].grossPay*0.1);
}
if(p[c].grossPay >= 500 && p[c].grossPay <= 999.99){
p[c].taxes = (p[c].grossPay*0.2);
}
if(p[c].grossPay > 1000){
p[c].taxes = (p[c].grossPay*0.3);
}
}
}
/*function definition for calculating net pay of each employee*/
void calculateNetPay(struct payRoll *p){
int c=0;
for(c;c<100;c++){
p[c].netPay = p[c].grossPay - p[c].taxes;
}
}
/*function definition to display all payRoll*/
void displayEmployee(struct payRoll *p){
int c = 0;
for(c;c<100;c++){
if(p[c].fname == NULL){
break;
}
printf(" Employee no. : %d",c+1);
printf(" First Name : %s",p[c].fname);
printf(" Last Name : %s",p[c].lname);
printf(" Gross Pay : %s",p[c].grossPay);
printf(" Taxes : %s",p[c].taxes);
printf(" Net Pay : %s",p[c].netPay);
}
}
/*main function started*/
int main(){
struct payRoll employee[100];/*employee variable to store 100 struct*/
int check = readPayRoll(employee);/*calling function to read the data from file*/
if(check == 1){/*checking error occured in file read or not*/
calculatePay(employee);/*function calling to calculate gross pay*/
calculateTax(employee);/*function calling to calculate taxes*/
calculateNetPay(employee);/*function calling to calculate net pay*/
displayEmployee(employee);/*function calling to display employee*/
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.