0

This is my first time posting as I'm very much stuck on this programming assignment. This chapter we began learning Structures. The last two weeks we learned Arrays and Functions, so the assignment is trying to tie them all together. What I'm trying to do is pass this array of structure to a function as a reference and not by elements. My thought process is that most of these functions return nothing, so I would like to have my arrays updated within them. My problem is, I keep getting an error of 'Incompatible Type for Argument 1 of getInput'.

As far as I can see my syntax is correct. I outlined the structure outside of my main function, I defined a variable of emp specifying an array derived from a constant etc. My function should be OK. This program utiized individual arrays on my last assignment, this time around I removed the arrays and substituted a structure. Am I completely lost? I'm just trying to get past this first error, and if somebody could just point me in the right direction?

#include <stdio.h>

//Structures
struct employee {

    long int id_number;
    float wage;
    float hours;
    float overtime;
    float gross;

};

//Constants
#define NUM_EMPL 5
#define OVERTIME_RATE 1.5f
#define WORK_WEEK 40.0f

//Prototypes
void getInput (struct employee input, int size);
float overtime (struct employee input);
void gross_pay (struct employee input, float work, float ot_rate, int size);
void output (struct employee input, int size);

int main ()  {

    struct employee emp[NUM_EMPL] = {

                                {98401, 10.60},
                                {526488, 9.75},
                                {765349, 10.50},
                                {34645, 12.25},
                                {127615, 8.35}
    };


    int count;

    getInput (emp, NUM_EMPL);

    for (count = 0; count < NUM_EMPL; count++)  {
        //if overtime rate applies, function 'overtime' is called
        if (emp.hours[count] > WORK_WEEK)  {
            //function overtime takes specific elements of array and two constants as arguments
            emp.overtime[count] = overtime(emp, WORK_WEEK);
        }
        //if above does not apply, ot hours are set to 0
        else  {
            emp.overtime[count] = 0.0;
        }
    }
    //function called to calculate gross for each employee
    gross_pay (emp, WORK_WEEK, OVERTIME_RATE, NUM_EMPL);

    //function called to display output with proper header and formatting
    output(emp, NUM_EMPL);

    return (0);


}



//**************************************************************/
// Function: getInput
//
// Purpose: Obtains input from user, the number of hours worked
// per employee and stores the results in an array that is
// passed back to the calling program by reference.
//
// Parameters: emp_num - Array of employee clock numbers.
// hrs - Array of number of hours worked by an employee
// size - Number of employees to process */
//
// Returns: Nothing (call by refernce)
//
//**************************************************************/

void getInput (struct employee input, int size)    //(long emp_num[], float emp_hrs[], int size)
{

    int count; /* loop counter */

    /* Get Hours for each employee */
    for (count = 0; count < size; ++count)
    {
        printf("\nEnter hours worked by emp # %06li: ", input.id_number[count]);
        scanf ("%f", &input.hours[count]);
    }

    printf("\n\n");
}

//**************************************************************/
// Function: overtime
//
// Purpose: Figures out amount of overtime hours worked by
//subtracting 'work' from emp hrs.
//
// Parameters: emp_hrs - Array of hours worked by employees
// work - standard hours of work before OT

// Returns: float ot_hours
//
//**************************************************************/


float overtime (struct employee input, int size)                        //(float emp_hrs, float work)
{
    return (input.hours - work);
}

//**************************************************************/
// Function: gross_pay
//
// Purpose: Function that determines gross pay of each employee by multiplying ot rate by wage rate by hours worked
//and adding this amount to work times wage rate.
//
// Parameters: emp_hrs - Array of hours worked by employee
// wage_rate - Array of hourly wages for employees
// ot_hours - overtime hours if worked
// work - standard hours of work before OT
// ot_rate - time and a half constant
// size - amount of employees to be tested
//
// Returns: float gross to array of gross
//
//**************************************************************/
void gross_pay (struct employee input, float work, float ot_rate, int size)              //(float emp_hrs[], float gross[], float wage_rate[], float ot_hours[], float work, float        ot_rate, int size)
{
    int count; //loop counter


    for (count = 0; count < size; count++)  {
        input.gross[count] = (ot_rate * input.wage[count] * input.overtime[count]) + ((input.hours[count] - input.overtime[count]) * input.wage[count]);

    }
}

//**************************************************************/
// Function: output
//
// Purpose: Displays output of calculations in a neat,
// table.  Incorporates suppressed 0's and proper line allignment.
//
// Parameters: clock_number - array of each employee
// wage_rate - hourly wages for employees
// hours - hours worked by employees
// ot - overtime hours worked by employees
// gross - gross amount made by each employee
//
// Returns: Nothing: Displays printf within function
//
//**************************************************************/

void output (struct employee input, int size)   //(long clock_number[], float wage_rate[], float hours[], float ot[], float gross[], int size)
{

    int counter;

    printf("\n\n####################################################################\n");
    printf("##RESULTS##RESULTS##RESULTS##RESULTS##RESULTS##RESULTS##RESULTS#####\n");
    printf("####################################################################\n\n");

    printf("-------------------------------------------------------\n");
    printf("        Clock Number    Wage    Hours   OT      Gross\n");
    printf("-------------------------------------------------------\n");

    //employee data is displayed in a proper format
    for (counter = 0; counter < size; counter++)  {

        printf("    %06li       %-4.2f  %-4.2f  %-2.2f  %-5.2f\n", input.id_number[counter], input.wage[counter], input.hours[counter], input.overtime[counter], input.gross[counter]);
    }
}
6
  • 3
    gross_pay is declared to take just one struct employee, not an array. Commented Jun 29, 2014 at 11:30
  • E.g void getInput (struct employee input, int size) --> void getInput (struct employee *input, int size) and input[count].id_number and &input[count].hours Commented Jun 29, 2014 at 11:36
  • ahh, i think i know what you mean. what i did was added empty brackets to both the prototype of the get_input function as well as the function definition and it doesn't show that message anymore. I believe my syntax was the problem. Thank You. Commented Jun 29, 2014 at 11:38
  • BLUEPIXY, does that have anything to do with pointers? The asterisk and ampersand symbol that is? We haven't gotten that far in the book, and all of my examples so far show the ability to pass an array of structure to a function by reference, not utilizing those two symbols. I think we get into that next week though. Commented Jun 29, 2014 at 11:40
  • Additionally to the answers: There is no work visible in overtime, so this can't compile. And note, that structures are passed by-value, so your getInput works on a local copy, so the input will be discarded after return. Commented Jun 29, 2014 at 11:41

2 Answers 2

1

Do this in declaration as well as definition

void getInput (struct employee input[], int size);
float overtime (struct employee input[],int size);
void gross_pay (struct employee input[], float work, float ot_rate, int size);
void output (struct employee input[], int size);

Your declaration and definition of function overtime() is different.They contain different parameters.You should also fix it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help Ayush. I do see where my problem is. I forgot the brackets in both the declarations and in the definitions. Also, I checked the overtime function and fixed the parameters. My mistake was changing the names of the variables and not double checking them. Thank you for the help.
0

Your functions are defined to operate on a single employee, not an array of them. I'll show how to fix gross_pay, you'll need to make similar fixes to the other functions.

void gross_pay (struct employee input[], float work, float ot_rate, int size)              
{
    int count; //loop counter
    for (count = 0; count < size; count++)  {
        input[count].gross = (ot_rate * input[count].wage * input[count].overtime) + ((input[count].hours - input[count].overtime) * input[count].wage);

    }
}

When accessing the structure members, it's input[count].member, not input.member[count] -- the latter would be used if the member is an array, rather than than being an element of a structure that's in an array.

1 Comment

ahhh, i see. i was incorrectly trying to cycle through the same member of just one element of my structure variable, as opposed to cycling through the elements of the array. thank you fellow Massachusettsan. I guess i need to hit the books more and keep a close eye on my syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.