Thread: Coding Problem-Help needed

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Coding Problem-Help needed

    hi, I have done everything right and yet the output says that the "file 'employees.in' cannot be opened for reading". I have the employees.in and employees.out in the same directory as the program code. The problem and the source code are below.

    Code:
    /*
    Write a program to process weekly employee time cards for all employees of an organization.
    Each employee will have three data items: an identification number, the hourly wage rate,
    and the number of hours worked during the week. Each employee is to be paid time and
    one half for all hours worked over 40. A tax amount of 23.625 percent of gross salary will be
    deducted. The program output should show the employee's identification number and net pay. 
    Display the total payroll and the average amount paid at the end of the run.
    
    Make the program modular by creating functions to:
    - Get the employee data
    - Calculate gross pay
    - Calculate net pay
    - Display results
    
    You may choose to get the input data from the keyboard or from a text file
    of your own creation. You must determine what type of loop control you will use.
    For example, you may choose to use Sentinel value or an EOF controlled loop. 
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* declare functions */
    
    /* Get the employee data */
    void get_employee_data(FILE* fp,int* id, float* rate, float* hours);
    
    /* Calculate gross pay */
    float calc_gross_pay(float rate, float hours);
    
    
    /* Calculate net pay */
    float calc_net_pay(float gross_pay);
    
    
    /* Display results */
    void display(FILE* fp,int id, float net_pay);
    
    
    int main()
    {
    int id; /* employee identification */
    float rate; /* hourly wage */
    float hours; /* hours worked */
    float gross_pay; /* gross pay */
    float net_pay; /* net pay */
    float total_payroll=0; /* total payroll */
    float average_payroll=0; /* total payroll */
    int nums=0; /* number employees */
    
    FILE *fpin; /* read file pointer */
    FILE *fpout; /* write file pointer */
    
    fpin = fopen("employees.in","r"); /* open file for reading */
    
    /* check if file open */
    if(fpin == NULL)
    {
    printf("file 'employees.in'  cannot be opened for reading\n");
    exit(1); /* close all file streams  and terminate */
    }
    
    fpout = fopen("employees.out","w"); /* open file for writing */
    
    /* check if file open */
    if(fpout == NULL)
    {
    printf("file 'employees.out'  cannot be opened for writing\n");
    exit(1); /* close all file streams  and terminate */
    }
    
    /* read employees in loop */
    
    /* Get the employee data */
    get_employee_data(fpin,&id, &rate, &hours);
    
    while(!feof(fpin))
    {
    nums++; /* count employees */
    
    /* Calculate gross pay */
    gross_pay = calc_gross_pay(rate, hours);
    
    
    /* Calculate net pay */
    net_pay = calc_net_pay(gross_pay);
    
    /* sum totals */
    total_payroll = total_payroll + net_pay;
    
    /* Display results */
    display(fpout,id,  net_pay);
    
    /* get next employee data */
    get_employee_data(fpin,&id, &rate, &hours);
    
    }
    
    /* close read file */
    fclose(fpin);
    
    
    
    /* caculate average payents */
    average_payroll = total_payroll / nums;
    
    /* print total */
    fprintf(fpout,"\nTotal Payroll: %.2f  Average Payroll: %.2f\n",total_payroll, average_payroll);
    printf("\nTotal Payroll: %.2f  Average Payroll: %.2f\n",total_payroll, average_payroll); 
    
    /* close write file */
    fclose(fpout);
    
    return 0;
    }
    
    
    
    
    /* Get the employee data */
    void get_employee_data(FILE* fp,int* id, float* rate, float* hours)
    {
    
    /* get employee id */
    fscanf(fp,"%d",id);
    
    /* get employee id */
    fscanf(fp,"%f",rate);
    
    /* get employee id */
    fscanf(fp,"%f",hours);
    
    }
    
    
    /* Calculate gross pay */
    float calc_gross_pay(float rate, float hours)
    {
    float gross_pay = 0;
    
    if(hours <= 40)
        gross_pay = rate * hours;
    else
        gross_pay = rate * 40 + (hours - 40) * rate * 1.5f;
    
    return gross_pay;
    }
    
    
    
    /* Calculate net pay */
    float calc_net_pay(float gross_pay)
    {
    
    float tax = .23625f * gross_pay;
    
    float net_pay = gross_pay - tax;
    
    return net_pay;
    }
    
    
    
    /* Display results */
    void display(FILE* fp, int id, float net_pay)
    {
    
    fprintf(fp,"%d %.2f\n",id,net_pay);
    printf("%d %.2f\n",id,net_pay);
    
    }

  2. #2
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    It's safest to specify whether you're reading text or binary, using "rt" or "rb", rather than leaving it to your compiler to decide. Same with writing, "wt" or "wb". But this isn't really your problem.

    Make sure your files are in the same folder as the program's binary executable. For example, if you're using MS Visual Studio, you need to put your data files in the Debug directory where the program's EXE is.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Yea, I did all that, but still I am having the same issue. don't know what's wrong. thanks for replying.

  4. #4
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    Try rebooting your machine, in case some program is holding that file open and refusing to let other programs access it (not too likely, but just in case).

    Also, what OS and compiler are you using?

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Turbo C++ 4.5, hey dont worry about it, I think my system has some problem compiling it or something. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM