Hey, I was wondering how to have Fscanf loop and do multiple calculations for 4 different employee numbers with 4 different hours and 4 different pay rates.

#include <stdio.h>

double pay_calc (double hours_worked, double pay_rate) 
{
double tax,gross_pay,net_pay; 
gross_pay = hours_worked * pay_rate;
if (hours_worked > 40) 
gross_pay = hours_worked * (pay_rate *1.5) ;
tax = gross_pay * .03625;

net_pay = gross_pay - tax;
return net_pay;
}

int main()

{
FILE *input;
int employee_number;
double hours_worked, pay_rate;
input = fopen("pay_file.txt", "r");
fscanf (input, "%d", &employee_number);
/*printf ("Enter in hours worked: ");
scanf ("%lf", &hours_worked);*/ 
fscanf (input, "%lf", &hours_worked);

/*printf ("Enter in pay_rate: ");
scanf ("%lf", &pay_rate);*/
fscanf(input, "%lf", &pay_rate);
fclose(input);

printf ("%d's pay is %lf\n", employee_number,pay_calc(hours_worked, pay_rate));

return 0;

}

This is how I set up pay_file.txt

101
44
7.50
102
30
6.00
103
40
8.50
105
48
12.00

Thanks in advance for any help.

Dani AI

Generated

A few practical points that tie together 's example and 's reply, plus a small correctness fix and error-handling suggestions.

The loop should read whole records and check the conversion count from the input call instead of assuming every value exists. Also the overtime math in the original pay_calc is wrong: overtime pay should be the normal pay for the first 40 hours plus time-and-a-half for hours over 40, not straight time-and-a-half for every hour when hours > 40. A compact, robust approach is shown below: open the file, check fopen, loop while a three-item read succeeds, compute gross correctly, subtract tax, and format output as currency.

double net_pay(double hours, double rate) {
    double gross;
    if (hours > 40.0)
        gross = 40.0 * rate + (hours - 40.0) * rate * 1.5;
    else
        gross = hours * rate;
    double tax = gross * 0.03625;
    return gross - tax;
}

int main(void) {
    FILE *fp = fopen("pay_file.txt", "r");
    if (!fp) { perror("fopen"); return 1; }

    int emp;
    double hours, rate;
    while (fscanf(fp, "%d %lf %lf", &emp, &hours, &rate) == 3) {
        printf("%d's pay is %.2f\n", emp, net_pay(hours, rate));
    }

    if (ferror(fp)) perror("read error");
    fclose(fp);
    return 0;
}

Notes and troubleshooting tips:

  • Check the return value of fscanf; if it returns a value other than the expected count and not EOF, the record is malformed. Report and skip or abort as appropriate.
  • Use fgets + sscanf if input lines may contain comments, blank lines, or partial records; that lets a single bad line be skipped cleanly.
  • Use perror/errno for file/open/read diagnostics.
  • If the source is actually a .dbf (dBase) file, it is not plain text; use a DBF reader library rather than text parsing.

These suggestions combine 's advice about checking conversion counts with the corrected payroll calculation and safer I/O handling.

Recommended Answers

All 2 Replies

You have no formatting of the file beyond a whitespace separated sequence of values, so the loop is trivial:

while (fscanf(input, "%lf", &value) == 1) {
    /* Process the value */
}

fscanf will return the number of converted items, so if you're asking for one item and the return value isn't 1, either there's a read error (bad stream or invalid value) or you've reached end-of-file.

For multiple values in a record, you can typically assume that the first value in the record implies that the subsequent values exist. So it would become:

while (fscanf(input, "%lf", &first_value) == 1) {
    fscanf(input, "%lf", &second_value);
    fscanf(input, "%lf", &third_value);

    /* Process the value */
}

So thats how to do it! Thanks! I have the same problem in retrieving the account names and balances on my .dbf file using fscanf but no worries problem solved! Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.