Thread: Homework: 6 Structure Array For Emoployee Pay

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Homework: 6 Structure Array For Emoployee Pay

    Ok, so this assignment is to create a structure that allows input for up to 6 employees that then makes a 6 structure array showing Employee ID, Employee Last Name, Employee Pay Rate, Employee Hours Worked, Employee Pay, and Total Gross Pay for All Employees.

    I don't have a printf yet for total gross, but right now I am just trying to tackle the input. Obviously I am not doing it right because although gcc complier is not giving errors the program is not ending when I type 'q' (sentinel issue) or when I reach 6 employees. It just continues input forever.

    I haven't had a lot of practice with do-while loops and this chapter is over structures so I am not very skilled in either area. Here is my code so far:

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    #define MAXARRAY 6
    
    
    struct Record{
       int idnum;
       char lname[20];
       double pay_rate;
       double hrs_worked;
       double emp_pay;
       double gross_pay;
    };
    
    int main (){
    
       int i;
       char messege;
       struct Record payment[MAXARRAY];
    
    
       printf("\nThis program will calculate the amount of pay for employees entered.");
       printf("Program will automatically calculate up to 6 employees entered.");
       printf("Type \"q\" to exit early.\n");
    
       do {
        for (i = 0; i < MAXARRAY; i++){
            printf("Enter employee ID Number: ");
            scanf("%d", &payment[i].idnum);
            printf("Enter employee Last Name: ");
            scanf("%s", &payment[i].lname);
            printf("Enter employee Pay Rate: $");
            scanf("%f", &payment[i].pay_rate);
            printf("Enter employee Hours Worked: ");
            scanf("%f", &payment[i].hrs_worked);
        }
    
       } while (scanf("%c",&messege) != 'q');
    
    
       for (i = 0; i < MAXARRAY; i++){
            payment[i].emp_pay = payment[i].pay_rate * payment[i].hrs_worked;
            payment[i].gross_pay = payment[i].gross_pay + payment[i].emp_pay;
            
            printf("The Pay for employee #%d is %f", payment[i].idnum, payment[i].emp_pay);
        }
       return 0;
    
    }
    Last edited by Cameron Taylor; 07-23-2013 at 03:20 PM.

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Gonna work from a more simple code for now. Worry about a sentinel and extras after I get the basics working. Right now, the program will continue, but it is printing out 0's for employee payment.

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    #define MAXARRAY 2
    
    
    struct Record{
       int idnum;
       char lname[20];
       double pay_rate;
       double hrs_worked;
       double emp_pay;
       double gross_pay;
    };
    
    int main (){
    
       int i;
       struct Record payment[MAXARRAY];
    
    
       printf("\nThis program will calculate the amount of pay for employees entered.");
       printf("Program will calculate pay for up to 6 employees.\n");
    
    
       for (i = 0; i < MAXARRAY; i++){
        printf("Enter employee ID Number: ");
        scanf("%d", &payment[i].idnum);
        printf("Enter employee Last Name: ");
        scanf("%s", &payment[i].lname);
        printf("Enter employee Pay Rate: $");
        scanf("%f", &payment[i].pay_rate);
        printf("Enter employee Hours Worked: ");
        scanf("%f", &payment[i].hrs_worked);
       }
    
    
    
       for (i = 0; i < MAXARRAY; i++){
            payment[i].emp_pay = payment[i].pay_rate * payment[i].hrs_worked;
            payment[i].gross_pay += payment[i].emp_pay;
            
            printf("\nThe Pay for employee #%d is %f", payment[i].idnum, payment[i].emp_pay);
       }
       return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile with warnings turned all the way up:
    Code:
    $ make foo
    gcc -Wall -ggdb3 -std=c99 -o foo foo.c -lm -lpthread
    foo.c: In function ‘main’:
    foo.c:30:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    foo.c:32:5: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat]
    foo.c:34:5: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat]
    Your scanf calls are wrong. Drop the & from line 30, the name of an array is already a pointer to the first element, so you don't need it. The other two, read the docs for scanf, you need a l (ell) in there to scan a double: %lf.

    If you are trying to find a problem in a calculated value, it's a good idea to print or watch in the debugger, and make sure you are using the right values for your calculation. If you added the following printf, you might see something:
    Code:
    $ tail foo.c
      for (i = 0; i < MAXARRAY; i++){
        printf("\ncalculating with pay_rate = %f, hrs_worked = %f\n", payment[i].pay_rate, payment[i].hrs_worked);
        payment[i].emp_pay = payment[i].pay_rate * payment[i].hrs_worked;
        payment[i].gross_pay += payment[i].emp_pay;
    
    
        printf("\nThe Pay for employee #%d is %f", payment[i].idnum, payment[i].emp_pay);
      }
      return 0;
    
    
    }
    $ make foo
    
    
    <warnings snipped>
    $ ./foo
    
    
    This program will calculate the amount of pay for employees entered.Program will calculate pay for up to 6 employees.
    Enter employee ID Number: 1
    Enter employee Last Name: aaa
    Enter employee Pay Rate: $12.34
    Enter employee Hours Worked: 7
    Enter employee ID Number: 2
    Enter employee Last Name: bbb
    Enter employee Pay Rate: $13.99
    Enter employee Hours Worked: 5
    
    
    calculating with pay_rate = 0.000000, hrs_worked = 0.000000
    
    
    The Pay for employee #1 is 0.000000
    calculating with pay_rate = 0.000000, hrs_worked = 0.000000
    
    
    The Pay for employee #2 is 0.000000
    Uh oh, all your values are zero, another sign you read them incorrectly.

    A few more notes, use a %d in the printf on line 23, along with MAXARRAY so your output will always be correct, even if you change MAXARRAY to 2:
    Code:
    printf("Program will calculate pay for up to %d employees.\n", MAXARRAY);
    Also, a couple aesthetic things: print an empty line between entering each employee, it makes it easier to know when you're on a new employee, and an empty line after the last line of output, so your command prompt looks correct after your program is done.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Sorry for the long reply, had other stuff to work on (and got hungry). So I think I did everything you said, but I am still getting an issue with the "gross_sum" part of the array to show the gross of all employees entered.

    Updated Code:
    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    #define MAXARRAY 2
    
    
    struct Record{
       int idnum;
       char lname[20];
       double pay_rate;
       double hrs_worked;
       double emp_pay;
       double gross_pay;
    };
    
    int main (){
    
       int i, total;
       struct Record payment[MAXARRAY];
    
    
       printf("\nThis program will calculate the amount of pay for employees entered.");
       printf("\nProgram will calculate pay for up to %d employees.\n", MAXARRAY);
    
    
       for (i = 0; i < MAXARRAY; i++){
        printf("\nEnter employee ID Number: ");
        scanf("%d", &payment[i].idnum);
        printf("Enter employee Last Name: ");
        scanf("%s", payment[i].lname);
        printf("Enter employee Pay Rate: $");
        scanf("%lf", &payment[i].pay_rate);
        printf("Enter employee Hours Worked: ");
        scanf("%lf", &payment[i].hrs_worked);
       }
    
    
    
       for (i = 0; i < MAXARRAY; i++){
            payment[i].emp_pay = payment[i].pay_rate * payment[i].hrs_worked;
            payment[i].gross_pay =+ payment[i].emp_pay;
            
            printf("\nThe Pay for employee #%d is %f", payment[i].idnum, payment[i].emp_pay);
       }
    
       i = MAXARRAY;
       printf("\n\nGross Pay for all employees is %lf", payment[i].gross_pay);
       return 0;
    
    }
    The calculation for pay works for each individual person, but again not the total of all employees.

    commiedic@localhost Project19$ ./Exercise3ab

    This program will calculate the amount of pay for employees entered.
    Program will calculate pay for up to 2 employees.

    Enter employee ID Number: 1
    Enter employee Last Name: t
    Enter employee Pay Rate: $30
    Enter employee Hours Worked: 25

    Enter employee ID Number: 2
    Enter employee Last Name: j
    Enter employee Pay Rate: $28
    Enter employee Hours Worked: 28

    The Pay for employee #1 is 750.000000
    The Pay for employee #2 is 784.000000

    Gross Pay for all employees is 0.000000commiedic@localhost Project19$

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't use this:
    Code:
       i = MAXARRAY;
       printf("\n\nGross Pay for all employees is %lf", payment[i].gross_pay);
    Because in C, array indices are only valid to MAXARRAY - 1. So payment[i] is out of bounds.
    What you need is another variable (not part of the payment[] array), to sum up all the employee's pay into. You have the right idea, but you need to add a new variable. It can sum up right inside the very same for loop - but DON'T have this variable be a part of the payment array. That array is full.

    The gross pay in each struct is the gross pay for ONE employee, I presume. emp_pay should be their net pay. I don't see any deductions (like taxes), in your program. Are those forthcoming?
    Last edited by Adak; 07-23-2013 at 07:16 PM.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, I made a new double variable named "total". The net pay of all employees is supposed to be "gross_pay" and emp_pay is the individual employee pay. Their are no taxes being added to the equation, just emp id, name, rate of pay and hours worked calculated to give total pay for each employee and total pay for all employees.

    Code:
    
       for (i = 0; i < MAXARRAY; i++){
            payment[i].emp_pay = payment[i].pay_rate * payment[i].hrs_worked;
            payment[i].gross_pay += payment[i].emp_pay;
            total = payment[i].gross_pay;
            printf("\nThe Pay for employee #%d is %f", payment[i].idnum, payment[i].emp_pay);
       }
    
    
       printf("\n\nGross Pay for all employees is %lf\n", total);
       return 0;
    Forgot to add... That the output for total is the pay for the 2nd employee and not employee 1 and employee 2 added together. I am not sure what that is happening since it is supposed to add them together.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, This works, but I am supposed to use 6 structures in the array so I can't do it lol. Just need to figure out how to make payment.gross_pay work.

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    #define MAXARRAY 2
    
    
    struct Record{
       int idnum;
       char lname[20];
       double pay_rate;
       double hrs_worked;
       double emp_pay;
       double gross_pay;
    };
    
    int main (){
    
       int i;
       double total;
       struct Record payment[MAXARRAY];
    
    
       printf("\nThis program will calculate the amount of pay for employees entered.");
       printf("\nProgram will calculate pay for up to %d employees.\n", MAXARRAY);
    
    
       for (i = 0; i < MAXARRAY; i++){
        printf("\nEnter employee ID Number: ");
        scanf("%d", &payment[i].idnum);
        printf("Enter employee Last Name: ");
        scanf("%s", payment[i].lname);
        printf("Enter employee Pay Rate: $");
        scanf("%lf", &payment[i].pay_rate);
        printf("Enter employee Hours Worked: ");
        scanf("%lf", &payment[i].hrs_worked);
       }
    
    
    
       for (i = 0; i < MAXARRAY; i++){
            payment[i].emp_pay = payment[i].pay_rate * payment[i].hrs_worked;
            total += payment[i].emp_pay;
            printf("\nThe Pay for employee #%d is %f", payment[i].idnum, payment[i].emp_pay);
       }
    
    
       printf("\n\nGross Pay for all employees is %lf\n", total);
       return 0;
    
    }

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, this works now:

    Code:
       for (i = 0; i < MAXARRAY; i++){
            payment[i].emp_pay = payment[i].pay_rate * payment[i].hrs_worked;
            payment[i].gross_pay += payment[i].emp_pay;
            total += payment[i].gross_pay;
            printf("\nThe Pay for employee #%d is %f", payment[i].idnum, payment[i].emp_pay);
       }
    
    
       printf("\n\nGross Pay for all employees is %lf\n", total);
       return 0;
    
    }
    It just feels like an unnecessary extra step to make sure there are 6 structures, but I guess that is what the assignment calls for.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Not sure what you mean by the "unnecessary extra step" comment.

    You may want to ensure that total is zero before you start adding to it.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you have say, 6 employees, and the array of structs is full, then you can't use the array to hold a total pay for all the employees.

    Unless you're tricky.

    I didn't mention this because beginners easily get "too clever" in their programs, but this is how it could be done:

    Use gross_pay is a "running tally" of all the emp_pay's that have been accessed, before it.

    employee zero gets emp_pay of $300. employee zero gross_pay then is $300
    employee one gets emp_pay of $400. employee one gross_pay then would be $700
    employee two gets emp_pay of $500. employee two gross_pay then would be $1,200.

    etc.

    So it CAN be done, but you then have to remember than the employee's struct member gross_pay, will actually be a running total for the entire list of employees.

    Final gross pay would be the gross_pay of the last employee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help (Array loop?)
    By Langard in forum C Programming
    Replies: 1
    Last Post: 03-25-2013, 01:32 AM
  2. Homework Help (array)
    By IamTheCarter in forum C Programming
    Replies: 9
    Last Post: 10-14-2011, 08:51 PM
  3. Replies: 12
    Last Post: 11-21-2010, 09:46 AM
  4. array homework
    By kokujampo in forum C Programming
    Replies: 4
    Last Post: 04-05-2010, 12:47 PM
  5. Homework help Array Arguments
    By VanillaSparkles in forum C Programming
    Replies: 3
    Last Post: 08-24-2007, 01:27 AM