I'm trying to scan a txt file and based on if some conditions are met, I want to print it out into another txt file. Here is my main function:
#include "Header.h"
int main(void) {
int payment=0, hours_worked = 0, rate_per_hour = 0, overtime = 0, total_payment, min = 0, max = 0, average = 0;
total_payment=0;
double total = 0;
Employee payroll[200];
FILE* infile = fopen("payroll.txt", "r");
FILE* outfile = fopen("paid.txt", "w");
int i = 0;
while (!feof(infile)) {
fscanf(infile, "%s", &payroll[i].name);
fscanf(infile, "%s", &payroll[i].title);
fscanf(infile, "%s", &payroll[i].hours_worked);
fscanf(infile, "%s", &payroll[i].payrate);
if (payroll[i].title == 'B') {
if (payroll[i].hours_worked > 40) {
payroll[i].payment = 40 * payroll[i].payrate;
payroll[i].payment += (1.5 * payroll[i].payrate * (payroll[i].hours_worked - 40));
}
else
payroll[i].payment = payroll[i].hours_worked * payroll[i].payrate;
}
else {
if (payroll[i].hours_worked > 40) {
payroll[i].payment = 40 * payroll[i].payrate;
payroll[i].payment += (1.8 * payroll[i].payrate * (payroll[i].hours_worked - 40));
}
else
payroll[i].payment = payroll[i].hours_worked * payroll[i].payrate;
}
if (i == 0)
min = payroll[i].payment;
total = +payroll[i].payment;
if (min > payroll[i].payment)
min = payroll[i].payment;
if (max < payroll[i].payment)
max = payroll[i].payment;
i++;
}
average = total / i;
fprintf(outfile, "total: $%.2lf\n average: $%.3lf\nMAx: $%.3lf\n Min: $%.2lf", total, average, max, min);
fclose(infile);
fclose(outfile);
return 0;
}
Header file:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct employee
{
char name[100]; // employee's name - last, first
char title; // title 'B' or 'M'
double hours_worked; // total number of hours worked
double payrate; // pay rate per hour
double payment; // total payment for the pay period – you will compute!
} Employee;
#endif
payroll.txt, scanning this file:
Smith, Susan
B
80.0
17.76
Sanders, Fred
M
87.25
23.45
Kerr, Heidi
M
80.0
47.86
Russo, Rick
B
83.75
12.15
outputing to paid.txt:
total: $8098405204118553210089249756384123022462475737784054204141978203412550137716411406580975068756992904139218004071573362835456.00
average: $0.000
MAx: $0.000
Min: $0.00
problem is I wasn't expecting it to print out the total that much and the average, max, and min are all 0s.