structure arrays to functions

This is a discussion on structure arrays to functions within the C Programming forums, part of the General Programming Boards category; I am trying to figure out how this works. So far I thought I understood it and yet it wont ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    structure arrays to functions

    I am trying to figure out how this works. So far I thought I understood it and yet it wont compile. ?? (this is only a small piece of the whole program, trying to just get one piece to work at at time) Any suggestion, tip or hints would be appreciated.

    [code]

    #include <stdio.h>

    #define NUM_EMPL 5
    #define OVERTIME_RATE 1.5
    #define STD_WORK_WEEK 40.000


    struct employee
    {
    unsigned long int clock; /*clock number*/
    float wage; /*hourly pay*/
    float hours; /*hours worked*/
    float overtime; /*overtime hours above 40*/
    float total; /*regular 40 hour pay*/
    float gross; /*total pay including any overtime*/
    };


    void get_input (struct employee emp_num[], emp_hrs[])
    {
    /*Local Variable Declaration */

    int count; /* Variable used in loop counter */

    /* Loop for number of employees defined in NUM_EMPL */
    /* Gets number of employee hours and stores them in an array. */

    for (count = 0; count < NUM_EMPL; ++count)
    {
    printf("Enter Hours for Employee #%lu: ",
    emp_num[count].clock);
    scanf ("%f", &emp_hrs[count].hours);
    }

    printf("\n\n\n");
    }



    main ()
    {
    struct employee data[NUM_EMPL];

    int n;

    get_input(data,NUM_EMPL)

    for (n = 0, n < NUM_EMPL, n++)
    {
    printf ("employee clock = %lu employee hours = %f\n", data[n].clock, data[n].hours);
    }
    retun 0;

    }
    [code]

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    sorry did he code code wrong

    I am trying to figure out how this works. So far I thought I understood it and yet it wont compile. ?? (this is only a small piece of the whole program, trying to just get one piece to work at at time) Any suggestion, tip or hints would be appreciated.

    Code:
    #include <stdio.h>
    
    #define NUM_EMPL 5
    #define OVERTIME_RATE 1.5
    #define STD_WORK_WEEK 40.000
    
    
    struct employee
    {
    	unsigned long int clock;   /*clock number*/
    	float wage;          /*hourly pay*/
    	float hours;          /*hours worked*/
    	float overtime;       /*overtime hours above 40*/
        float total;          /*regular 40 hour pay*/
    	float gross;         /*total pay including any overtime*/
    };
    
    
    void get_input (struct employee emp_num[], emp_hrs[])
    {
      /*Local Variable Declaration */
    
        int count; /* Variable used in loop counter */
    
         /* Loop for number of employees defined in NUM_EMPL */
         /* Gets number of employee hours and stores them in an array. */
    
        for (count = 0; count < NUM_EMPL; ++count)
          {
            printf("Enter Hours for Employee #%lu: ",
                    emp_num[count].clock);
            scanf ("%f", &emp_hrs[count].hours);
          }
    
      printf("\n\n\n");
    }
    
    
    
    main ()
    {
         struct employee data[NUM_EMPL];
         
         int n;
         
         get_input(data,NUM_EMPL)
    
         for (n = 0, n < NUM_EMPL, n++)
         {
              printf ("employee clock = %lu    employee hours = %f\n", data[n].clock, data[n].hours);
         }
    retun 0;
    
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    >void get_input (struct employee emp_num[], emp_hrs[])
    The compiler does not know what variable 'emp_hrs' is.
    Last edited by Scarlet7; 04-12-2003 at 02:35 PM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    I revised

    Now I am getting strage output.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_EMPL 5
    #define OVERTIME_RATE 1.5
    #define STD_WORK_WEEK 40.000
    
    
    struct employee
    {
    	unsigned long int clock;   /*clock number*/
    	float wage;          /*hourly pay*/
    	float hours;          /*hours worked*/
    	float overtime;       /*overtime hours above 40*/
        float total;          /*regular 40 hour pay*/
    	float gross;         /*total pay including any overtime*/
    };
    
    
    void get_input (struct employee emp_num[], struct employee emp_hrs[])
    {
      /*Local Variable Declaration */
    
        int count; /* Variable used in loop counter */
    
         /* Loop for number of employees defined in NUM_EMPL */
         /* Gets number of employee hours and stores them in an array. */
    
        for (count = 0; count < NUM_EMPL; ++count)
          {
            printf("Enter Hours for Employee #%lu: ",
                    emp_num[count].clock);
            scanf ("%f", &emp_hrs[count].hours);
          }
    
      printf("\n\n\n");
    }
    
    struct employee emp_data[NUM_EMPL] =
        {{98401, 10.60, 0, 0, 0, 0},
        {526488, 9.75, 0, 0, 0, 0},
        {765349, 10.50, 0, 0, 0, 0},
        {34645, 12.25, 0, 0, 0, 0},
        {127615, 9.35, 0, 0, 0, 0}};
    
    
    main ()
    {
         struct employee data[NUM_EMPL], data2[NUM_EMPL];
         
         int count;
         
         get_input(data,data2);
         
         printf ("\n");
    
           for (count = 0; count < NUM_EMPL; count++)
         {
              printf ("employee clock = %lu    employee hours = %f\n", data[count].clock, data2[count].hours);
         }
    system("PAUSE"); 
    return 0;
    
    }

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Specify the number of digits after the decimal point by adding the precision value.
    %f.2 = 2 decimal places, etc...
    Code:
    printf ("employee clock = %lu    employee hours = %.2f\n", data[count].clock, data2[count].hours);

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    more code

    I found that and several other errors I am still getting strange output.. I have completed my program but for some reason my "clock" is not passing the correct data.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_EMPL 5
    #define OVERTIME_RATE 1.5
    #define STD_WORK_WEEK 40.000
    
    
    struct employee
    {
    	unsigned long int clock;         /*clock number*/
    	float wage;          /*hourly pay*/
    	float hours;          /*hours worked*/
    	float overtime;       /*overtime hours above 40*/
        float total;          /*regular 40 hour pay*/
    	float gross;         /*total pay including any overtime*/
    };
    
      
          
    void header (void)      
    {     
            /* Explain what program is going to do */
    
    	printf ("To find out the gross pay of your employees,\n");
    	printf ("input the hours worked at the clock# prompts.\n");
    	printf ("\n");
    }
    
    
    
    void get_input (struct employee emp[])
    {
      /*Local Variable Declaration */
    
        int count; /* Variable used in loop counter */
    
         /* Loop for number of employees defined in NUM_EMPL */
         /* Gets number of employee hours and stores them in an array. */
    
        for (count = 0; count < NUM_EMPL; ++count)
          {
            printf("Enter Hours for Employee #%06lu: ", emp[count].clock);
            scanf ("%f", &emp[count].hours);
          }
    
      printf("\n\n\n");
    }
    
    
    
    void calc_gross (struct employee emp[])
    {
      /*Local Variable Declaration */
    
        float ot_wage[NUM_EMPL]; /*Variable for overtime wages only */
        float total[NUM_EMPL]; /*Variable used to get the total or regular hours before overtime added */
        int count; /* Variable used in loop counter */
        
       /* Loop for number of employees defined in NUM_EMPL */
    
    	 for (count = 0; count < NUM_EMPL; ++count)
    
    
        /* calculate gross pay including testing for overtime hours using if-else-if statement */
        /* use the DEFINE for STD_WORK_WEEK and OVERTIME_RATE*/
        /* pull the hours collected from get_input with emp_hours[] array*/
    
    		if (emp[count].hours > STD_WORK_WEEK)
    		{	(emp[count].overtime = emp[count].hours - STD_WORK_WEEK);
    			(total[count] = emp[count].wage * STD_WORK_WEEK);
    			(ot_wage[count] = emp[count].wage * OVERTIME_RATE);
    			(emp[count].gross = ot_wage[count] * emp[count].overtime + total[count]);
    			
    		}
    		else if (emp[count].hours <= STD_WORK_WEEK)
    		{	(emp[count].overtime = 0);
    			(emp[count].gross = emp[count].wage * emp[count].hours);
    		}
    }
    
    
    
    void output_screen (struct employee emp[])
    {
         /*Local Variable Declaration */
    
        int count; /* Variable used in loop counter */
        
        /*print output in table format using arrays */
        
        	printf ("\n");
    	printf ("__________________________________________________________________________\n");
    	printf ("Clock #\t\t   Wage\t\t  Hours\t\t   OT\t\tGross Pay\n");
    	printf ("__________________________________________________________________________\n");
    		
    	
    	for (count = 0; count < NUM_EMPL; ++count)
         	{
         	  printf ("%06i4\t\t%7.2f\t\t%7.2f\t\t%7.2f\t\t%7.2f\n", emp[count].clock, 
         	              emp[count].wage, emp[count].hours, emp[count].overtime, emp[count].gross);
         	}
    }
    
           /* initialize struct. */
    
    struct employee emp[NUM_EMPL] =
        {{98401, 10.60, 0, 0, 0, 0},
        {526488, 9.75, 0, 0, 0, 0},
        {765349, 10.50, 0, 0, 0, 0},
        {34645, 12.25, 0, 0, 0, 0},
        {127615, 9.35, 0, 0, 0, 0}};
    
    
    int main ()
    {
         struct employee emp[NUM_EMPL];
         
         
           /* Function to call header. */
      
         header ();
         
           /* Function to call get input using the struct */
         
         get_input(emp);
         
         /* Function call to calculate gross pay including ot using struct*/
    
    	 calc_gross (emp);
    	 
        /* Function call to output results to the screen in table format using struct */
    	
    	 output_screen (emp);
         
         
         
    
    system("PAUSE"); 
    return 0;
    
    }

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    15
    i also just fixed this.. and still getting wrong "clock" returned
    Code:
     printf ("%06lu\t\t%7.2f\t\t%7.2f\t\t%7.2f\t\t%7.2f\n", emp[count].clock, 
         	              emp[count].wage, emp[count].hours, emp[count].overtime, emp[count].gross);

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    lol, it took me a little bit but i figured it out. It is the most simple thing in the world. well, not really, you cant initialize a global struct then initialize a struct array of the same name in main() and have that blank. You pass that blank array in the function call, if you just get rid of that initialized one line, it works just fine. Here, i have done it for you.
    Code:
    struct employee emp[NUM_EMPL] =
        {{98401, 10.60, 0.0, 0.0, 0.0, 0.0}, {526488, 9.75, 0.0, 0.0, 0.0, 0.0}};
    
    
    int main ()
    {
     //    struct employee emp[NUM_EMPL]; THIS IS NOT NEEDED
         
         
           /* Function to call header. */
      
         header ();
         
           /* Function to call get input using the struct */
         
         get_input(emp);
         
         /* Function call to calculate gross pay including ot using struct*/
    
    	 calc_gross (emp);
    	 
        /* Function call to output results to the screen in table format using struct */
    	
    	 output_screen (emp);
         
         
         
    
    system("PAUSE"); 
    return 0;
    
    }
    --edit--
    Oh, and the struct array is smaller because i didn't feel like putting in 5 hours, i changed it to 2, sorry, lol, all you need to do is get rid of that line, oh and i think there was some other error, but it was small, just check your debugger.

    --edit #2--
    Oh, actually, to get away from global variables, just get rid of that line and copy the other (global) struct array into main(). So you kill 2 birds, stay away from globals in this program and get it actually working
    Last edited by stumon; 04-12-2003 at 08:06 PM.
    The keyboard is the standard device used to cause computer errors!

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    thank

    first of all you gave me a good laugh this morning with all your lol.. I needed that. 2nd I cant believe it was something so simple and I didnt see it. I think when looking at a program so long trying to figure things out you sometimes over look the simplest things. Also at times when you make changes you leave "stuff" behind that should be left.

    Well my program works beautifully now. Thanks again.

  10. #10
    .........
    Join Date
    Nov 2002
    Posts
    303
    I think when looking at a program so long trying to figure things out you sometimes over look the simplest things.
    Just had to comment on this, I couldn't agree more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  2. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 05:46 PM
  3. Character arrays in a Structure
    By vsriharsha in forum C Programming
    Replies: 7
    Last Post: 07-11-2004, 05:36 PM
  4. Replies: 7
    Last Post: 04-13-2003, 10:53 PM
  5. Identifying size of structure arrays
    By gowtham in forum C Programming
    Replies: 2
    Last Post: 02-10-2003, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21