#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>

FILE *fp1, *fp2;
int count;
    struct record
    {
    char name[20];
    int empnum;
    char depart[20];
    float hours;
    float rate;
    };

void main()
{
 clrscr();
 record h;
 int acount=0,adcount=0,scount=0;
 float grosspay,totalg=0;
 float tax,totalt=0;
 float health,totalh=0;
 float pension,totalp=0;
 float netpay,totaln=0;
 float deductions;

 fp1=fopen("employ.dat","r");
 fp2=fopen("payrol.dat","w");
 count = 0;
 printf("                       ""Page1\n");
 printf("        ""ABC COMPANY EMPLOYEE REPORT""\n");
 printf("------------------------------------------\n");
 printf("%5s %20s %10s %12s %5s %10s %9s %7s\n","Name","Empnumber","Depart","Tax","Health","Pension","Netpay");
 printf("___________________________________________________________________________________\n");

 while(fread(&h,sizeof(struct record),1,fp1)==1)
    {
    grosspay=h.hours*h.rate;
    tax = 0.155 * grosspay;
    pension=0.03*grosspay;
    deductions=(tax+health+pension);
    netpay=grosspay-deductions;
    totalg=totalg+grosspay;
    totalt=totalt+tax;
    totalh=totalp+pension;
    totaln=totaln+netpay;

    printf("%2s\t %5d\t %10s\t %4.2f\t %4.2f\t %4.2f\t %4.2f\t %4.2f\n",h.name,h.empnum,h.depart,grosspay,tax,health,pension,netpay);
      if(strcmp(h.depart,"Sales")==0
      scount=scount+1;
      else if(strcmp(h.depart,"Admin")==0)
      adcount=adcount+1;
      else
      acount=acount+1;
      count++;
    }
    printf("__________________________________________\n");
    printf("TOTALS     %4.2f\t %4.2\t %4.2f\t %4.2f\t %4.2f\n",totalg,totalt,totalh,totalp,totaln);

    printf("Total Number From Sales Department:%d\n",scount);
        printf("Total Number From Sales Department:%d\n",scount);
        printf("Total Number From Admin Department:%d\n",adcount);
        printf("Total Number From Accounts Department:%d\n",&acount);
        printf("Total Number Of Employees:%d\n",count);
        fwrite(&h,sizeof(h),1,fp2);
        fflush(stdin);
        fclose(fp1);
        fclose(fp2);
}

Dani AI

Generated

Good diagnostics from , and — there are both syntax/compiler-level problems and a few logic/formatting bugs that no compiler error will point out directly. Fix things in this order: (1) resolve the compile error (missing parenthesis/brace in the department comparison), (2) make the program portable (standard main, no nonstandard headers or conio calls), and (3) correct logic/IO mistakes so totals and output are valid.

Key logic/behaviour bugs not yet fully addressed in the thread:

  • Some aggregate assignments mix variables (for example a totals variable is being assigned from the wrong names). Totals should accumulate the same per-item value you print.
  • One printf passes an address instead of the count variable — that prints garbage. Check every printf format string matches the exact number and types of arguments.
  • fwrite/fread are being used with text-mode file opens and/or placed outside the loop; decide whether payroll is a binary copy of records (use "rb"/"wb" and fwrite inside the loop) or a text report (use fprintf and write formatted lines).
  • The per-employee health deduction is used but never initialized — give it a defined value or read it from the record.

A short, portable pattern to follow (replace with your own field names/policy):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct employee { char name[20]; int id; char dept[20]; float hours, rate; };

int main(void) {
    FILE *in = fopen("employ.dat","rb");
    FILE *out = fopen("payrol.txt","w");
    if (!in || !out) { perror("fopen"); return 1; }

    struct employee e;
    while (fread(&e, sizeof e, 1, in) == 1) {
        float gross = e.hours * e.rate;
        float tax = 0.155f * gross;
        float pension = 0.03f * gross;
        float health = 5.00f; /* set according to policy */
        float net = gross - (tax + pension + health);
        fprintf(out, "%-20s %6d %-10s %8.2f %6.2f %6.2f %6.2f\n",
                e.name, e.id, e.dept, gross, tax, health, net);
    }
    fclose(in); fclose(out);
    return 0;
}

Compile with strict warnings (for example: gcc -std=c99 -Wall -Wextra) and fix every warning — they point to mismatched formats, uninitialized variables, and other defects. Remove non-portable calls like clrscr() and fflush(stdin), check fopen returns, and test with a tiny sample file to verify totals before trusting program output.

Recommended Answers

All 7 Replies

Maybe your issue is related to the missing ).

if(strcmp(h.depart,"Sales")==0

[EDIT]Oh, wait! Are you compiling C++ code in a C compiler?

Oh, wait! Are you compiling C++ code in a C compiler?

I was wandering the same thing

#include<iomanip.h>

==> C++ code.

sizeof(struct record)

==> C code ?? .

its supposed to be in c code.i dont know if that is the right way of coding.

<iomanip.h> is a (deprected) C++ header that most likely rightly caused the error. Delete this #include (you're not using anything in it in the code anyway).

C requires you to declare all variables before executing statements (unless you are using C99), so you would need to move the call to clrscr() after the declarations.

C does not automatically typedef structures, so this

record h;

should be this.

struct record h;

Your main should return an int, not void. As such, return 0 is most commonly used.

Here:

deductions=(tax+health+pension);

the variable health is used before it has been given a value.

And fflush(stdin) is a no-no.

><iomanip.h> is a (deprected) C++ header
Nope, iostream.h was removed completely. Deprecated presumes that the header is still valid C++, but not recommented as it might be removed in future revisions. As it is, iostream.h is nonstandard and should be treated as such.

I"ve done all the changes but still it wont run please help

>I"ve done all the changes
Prove it.

>but still it wont run
What's it saying?

>please help
You're not helping us to help you, so you won't get much help.

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.