Quantcast

Maximum PC

It is currently Thu Aug 28, 2014 12:40 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Help with Programming in C (semi-basic stuff)
PostPosted: Fri Dec 03, 2010 10:36 pm 
8086
8086
User avatar

Joined: Wed Jun 09, 2010 7:18 pm
Posts: 98
So what this program does is read input from a file, store that information into a structure, and then prints out the result of calculations and such on another file. The problem i'm having is that what is being written to the other file is completely wrong or not there. What i get on the output file is n = 40000 (some big number) when it was successfully read from the input file as 2.

EDIT: n is now fixed changed n=0 in if statement to n==0. and got rid of amperstand on the printing statement. Everything gets printed out but all values except n are 0.

EDIT2: got rid of amperstands on all final print statements at the end and changed "lf" to "%d" on the part of comparing strings. but still some calculation errors

EDIT3: I've figured out how to calculate the hours correctly (had to cast them to float) and the gross for each employee, however the taxes are slightly off. I think the problem is that for jane worker she has to go to the else statement when calculating the taxes because one week she does works 44 hours. What helped me make progress on the other stuff is that i misread the directions and k is the number of weeks in the file which made calculating hours in each week much easier. Another part of the assignment is dividing this into functions, i tried to do that at the end, but i dont know if i'm doing it right. I'm not sure how to pass a struct into a function.

EDIT4: The calculation is all correct now!! Now time to make more readable as in splitting it into functions as i've already done one of them.


Program:
Code:
/* Programming Assignment #6
* Problem: Payroll - W2 Forms
* Programmer: Brandon Autrey
* ------------
* 12/3/10
*/

#include <stdio.h>
#include <stdlib.h> // for system pause
#include <string.h> //used to compare the read string to the names in the structures. 
                    //Impotant because it allows input data to be in any order and it would still be able to read it in correctly.
#define MAX_LEN 30
#define ARRAY_LEN 20 //a maximum of 20 employees can be inputted from the file
#define MAXIMUM_WEEKS 51

struct employee {
    char first[MAX_LEN + 1];
    char last[MAX_LEN + 1];
    double payperhr;
    double gross;
    double taxes;
    double hours_in_week[MAXIMUM_WEEKS]; //may need to be an array array to accomodate weeks so calculation of salary will be correct
                            //each cell in this array will be one weeks worth of hours, even if the input file goes to half a week
    double total_hours;             
}employee_n[ARRAY_LEN]; 
/*Stores an array (size 20) for each employee,
* is basically an array with a stucture in each cell
*/

//prints out w2 file
//void scan_clock2(FILE *file_clock, int k, int m, int n)
void calculation();
void print_w2(FILE *w2, int n, int k);
//hours_to_zero(employee employee_n);

int n, k, m, i_n, i_k, i_m, HrIn, MinIn, HrOut, MinOut;
/* n is the number of employees between 1 and 20
* k is represents the total number of sets of data contained in the file, number of weeks of data there is
* m represents the number of employee records for the week
* i_n is the counter variable for for which employee
* i_k is the counter variable for k lines
* i_m is the counter variable for m lines
* HrIn: 0 - 23 military time, the hour the worker clocked in
* MinIn: 0 - 59 military time, the minute the worker clocked in
* HrOut: 0 - 23 military time, the hour the worker clocked out
* MinOut: 0 - 59 military time, the minute the worker clocked out
*/
char filename[18], first_temp[MAX_LEN + 1], last_temp[MAX_LEN + 1];
double net_pay, total_hours;
/* filename[] is the array that stores the name of the file which the user inputs
* employee_n is the array the stores each employee specified in file in its separate cell of the arrray
* first_temp is used to compare the read string to the structure
* last_temp is used to compare the read string to the structure
* net_pay is the total amount he/she gets paid without subtracting taxes
* total hours calculates how many hours each employee worked over the total amount of weeks
*/
   
int main()
{
    printf("Enter the name of the clock file: ");
    scanf("%s", filename);   
    //opens file
    FILE *file_clock;  // *ifp
    do //allows checking for the file name to determine if it exists
   {
      file_clock = fopen(filename, "r");
      if (file_clock == NULL)
      {
         printf("Incorrect file name!  Reenter.\n");
         scanf("%s", filename);
        }
   }
   while (file_clock == NULL);
   
    fscanf(file_clock, "%d", &n); 
    //printf("n was read as: %d\n", n);
   
    if (n == 0) {
       printf("There were no employee's in the file\n");
       getch("pause");
       return 0;
    }
   
    for (i_n = 0; i_n < n && i_n != ARRAY_LEN; i_n++)
    {
        fscanf(file_clock, "%s", employee_n[i_n].first);
        fscanf(file_clock, "%s", employee_n[i_n].last);
        fscanf(file_clock, "%lf", &employee_n[i_n].payperhr);
    }
   
    fscanf(file_clock, "%d", &k); //there are k weeks of data.
    //printf("k = %d\n", k);
    //initializes all values in hours_in_week to 0
    for (i_n = 0; i_n < n; i_n++)
    {
        for (i_k = 0; i_k < k; i_k++)
        {
            employee_n[i_n].hours_in_week[i_k] = 0;
        }
    }
   
    for (i_k = 0; i_k < k; i_k++) //goes through k group of entries
    {
        fscanf(file_clock, "%d", &m); //each m group is one week's worth of hours
        //printf("m = %d\n", m);
        for (i_m = 0; i_m < m; i_m++) //goes through m amount of clock ins/outs
        {
            fscanf(file_clock, "%s", last_temp);
            fscanf(file_clock, "%s", first_temp);
            //printf("%s ", last_temp);
            //printf("%s\n", first_temp);
            for (i_n = 0; i_n < n; i_n++) //goes through each line and determines which person gets the hours
            {
                if (strcmp(last_temp, employee_n[i_n].last) == 0)
                {
                   if (strcmp(first_temp, employee_n[i_n].first) == 0)
                   {
                       fscanf(file_clock, "%d", &HrIn);
                       fscanf(file_clock, "%d", &MinIn);
                       fscanf(file_clock, "%d", &HrOut);
                       fscanf(file_clock, "%d", &MinOut);
                       employee_n[i_n].hours_in_week[i_k] = employee_n[i_n].hours_in_week[i_k] + ((HrOut - HrIn) + (float)(MinOut - MinIn) / 60);                           
                   }
                }
            }   
            //printf("m = %d, k = %d, last_temp = %s, first_temp = %s", m, k, last_temp, first_temp);   
        } 
    }
    fclose(file_clock);
   
    //calculates each employee's gross and taxes based on how much they worked each week
    for (i_n = 0; i_n < n; i_n++)
    {
        for (i_k = 0; i_k < k; i_k++) //goes through the hours each employee worked each week
        {           
            if (employee_n[i_n].hours_in_week[i_k] <= 40) { //40 hours per week is the average work week,
            //if a employee works over 40 hours they get paid more but they have more of that pay deducted in taxes
               employee_n[i_n].gross += employee_n[i_n].hours_in_week[i_k] * employee_n[i_n].payperhr;
               employee_n[i_n].taxes += employee_n[i_n].hours_in_week[i_k] * employee_n[i_n].payperhr * 0.1;
            }
            else { //employee_n[i_n].hours_in_week[i_k] > 40
                 employee_n[i_n].gross += ((employee_n[i_n].hours_in_week[i_k] - 40) * employee_n[i_n].payperhr * 0.5) + (employee_n[i_n].hours_in_week[i_k] * employee_n[i_n].payperhr);
                 employee_n[i_n].taxes += ((employee_n[i_n].hours_in_week[i_k] - 40) * employee_n[i_n].payperhr * 1.5 * 0.2) + (40 * employee_n[i_n].payperhr * 0.1);
            }
            employee_n[i_n].total_hours += employee_n[i_n].hours_in_week[i_k];
        }
    }
    //writes to w2 file
    FILE *w2;
    w2 = fopen("w2.txt", "w");
    print_w2(w2, n, k);  //function to printf w2 file
    fclose(w2);
         
    //getch("pause");
    return 0;
} //end of main

void print_w2(FILE *w2, int n, int k)
{
   
    fprintf(w2, "Number of employees: %d\n\n", n); //n is not being printed corectly, its somehow being changed throughout the program
    for (i_n = 0; i_n < n; i_n++)
    {
        fprintf(w2, "W2 Form\n-------\n");
        fprintf(w2, "Name: %s %s\n", employee_n[i_n].first, employee_n[i_n].last);
        fprintf(w2, "Gross Pay: %.2lf\n", employee_n[i_n].gross);
        fprintf(w2, "Taxes Withheld: %.2lf\n", employee_n[i_n].taxes);
        net_pay = employee_n[i_n].gross - employee_n[i_n].taxes;
        fprintf(w2, "Net Pay: %.2lf\n", net_pay);
        fprintf(w2, "Pay per hour: %.2lf\n", employee_n[i_n].payperhr);
        fprintf(w2, "Total hours worked: %5.2lf\n"
        "Worked a total of %d weeks, averaging %.2lf hours per week.\n\n", employee_n[i_n].total_hours, k, employee_n[i_n].total_hours / k);
    }
}




Sample Input file:

2
John Slacker 5.15
Jane Worker 10.00
10
5
Worker Jane 9 0 13 45
Slacker John 10 50 18 10
Slacker John 7 30 13 30
Worker Jane 8 30 18 50
Slacker John 12 20 13 20
4
Worker Jane 8 0 18 0
Worker Jane 8 0 19 0
Worker Jane 8 0 19 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0
2
Slacker John 10 0 15 0
Worker Jane 8 0 20 0

Sample Output file:

Number of employees: 2

W2 Form
-------
Name: John Slacker
Gross Pay: 279.82
Taxes Withheld: 27.98
Net Pay: 251.84


W2 Form
-------
Name: Jane Worker
Gross Pay: 1570.83
Taxes Withheld: 163.08
Net Pay: 1407.75



- Any help would be greatly appreciated


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group