Need help! C programing. Get build messges for my get_stats function. Trying to
ID: 3679704 • Letter: N
Question
Need help! C programing. Get build messges for my get_stats function. Trying to add a total of all the coulums (ex total ot hours, gross, etc) and average under my table using DYNAMIC LINKED LISTS WITH POINTERS however I keep getting the below message:
C:UsersLeeDesktopC ProgramAssignment8LeeAnathamain.c||In function 'grossPay':
unused variable 'ot_pay' [-Wunused-variable]|
.c||In function 'get_stats':|
invalid operands to binary / (have 'float' and 'struct employee *')|
invalid operands to binary / (have 'float' and 'struct employee *')|
invalid operands to binary / (have 'float' and 'struct employee *')|
: invalid operands to binary / (have 'float' and 'struct employee *')|
'my_totals' undeclared (first use in this function)|
each undeclared identifier is reported only once for each function it appears in|
here is my code::
#include <stdio.h>
#include <stdlib.h> /* for malloc */
#include <ctype.h>
#define OVERTIME_RATE 1.5f /* Overtime Rate */
#define STD_WORK_WEEK 40.0f /* Stand weekly hours */
struct employee
{
char first_name [10];
char last_name [10];
int id_number;
float wage;
float hours;
float overtime;
float gross;
struct employee *next;
};
struct stats
{
float total_wage;
float total_hours;
float total_ot;
float total_gross;
float avg_wage;
float avg_hours;
float avg_ot;
float avg_gross;
};
/* TODO - Add Function Prototypes as needed */
void getOT (struct employee *emp1);
void grossPay (struct employee *emp1);
void get_stats (struct employee *emp1, struct stats);
/* TODO - Add Functions here as needed */
//**************************************************************/
// Function: getOT
//
// Purpose: Calculates overtime hours
//
// Parameters:
//
// *emp_ptr - pointer to array of structures
// size - number of employees to process
//
// Returns: Nothing (call by reference)
//
//**************************************************************/
void getOT (struct employee *emp1)
{
int i=0; /*loop index*/
struct employee *tmp; /* tmp pointer value to current node */
/* Process each employee one at a time */
for(tmp = emp1; tmp ; tmp = tmp->next)
{
if (tmp->hours > STD_WORK_WEEK) /* ot */
{
tmp->overtime = tmp->hours - STD_WORK_WEEK;
}
else /* no ot */
{
tmp->overtime = 0;
}
i++;/*process each employee */
} /*end loop */
}
//**************************************************************/
// Function: grossPay
//
// Purpose: Calculates gross pay
//
// Parameters:
//
// *emp_ptr - pointer to array of structures
// size - number of employees to process
//
// Returns: Nothing (call by reference)
//
//**************************************************************/
void grossPay (struct employee *emp1)
{
int i; /* loop index */
struct employee *tmp; /* tmp pointer value to current node */
float ot_pay; /* local variable */
/* Process each employee one at a time */
for(tmp = emp1; tmp ; tmp = tmp->next)
{
/* Calculate Gross Pay */
if (tmp->hours > STD_WORK_WEEK) /* ot */
{
tmp->overtime = (tmp->hours - STD_WORK_WEEK);
tmp->gross = (STD_WORK_WEEK * tmp->wage) + (tmp->overtime * tmp->wage * OVERTIME_RATE);
}
else /* no ot */
{
tmp->overtime = 0;
tmp->gross = (tmp->wage * tmp->hours);
}
i++;/*process each employee */
} /* end loop */
}
//*******************************************************************/
// Function: get_stats
//
// Purpose: Outputs to screen in a table format the following
// information about an employee: Total and average
//
// Parameters:
//
// employeeData - an array of structures containing
// employee information
// my_totals- array to calculate total and average
// size - number of employees to process
//
// Returns: Nothing (void)
//
//******************************************************************/
void get_stats(struct employee *emp1, struct stats my_totals)
{
int i; /* loop index */
float total_wage; /* total wages */
float total_hours; /* total hours */
float total_ot; /* total overtime */
float total_gross; /* total gross pay */
float avg_wage; /* average of wages */
float avg_hours; /* average of hours */
float avg_ot; /* average of overtime */
float avg_gross; /* average of gross pay */
struct employee *tmp; /* tmp pointer value to current node */
for(tmp = emp1; tmp ; tmp = tmp->next) /* any loop is fine */
{
i++;/*process each employee */
/* calculate total and average */
total_wage += tmp->wage;
total_hours += tmp->hours;
total_ot += tmp->overtime;
total_gross += tmp->gross;
avg_wage= tmp->wage / tmp;
avg_hours= tmp->hours / tmp;
avg_ot= tmp->overtime / tmp;
avg_gross= tmp->gross / tmp;
}
printf("Total:");
printf("%30.2f", tmp->wage);
printf("%10.1f", total_hours);
printf("%10.1f", total_ot);
printf("%10.2f ", total_gross);
printf("Average:");
printf("%28.2f", avg_wage);
printf("%10.1f", avg_hours);
printf("%10.1f", avg_ot);
printf("%10.2f ", avg_gross);
}
/*-----------------------------------------------------------------------------*/
/* */
/* FUNCTION: print_list */
/* */
/* DESCRIPTION: This function will print the contents of a linked */
/* list. It will traverse the list from beginning to the */
/* end, printing the contents at each node. */
/* */
/* PARAMETERS: emp1 - pointer to a linked list */
/* */
/* OUTPUTS: None */
/* */
/* CALLS: None */
/* */
/*-----------------------------------------------------------------------------*/
void print_list(struct employee *emp1)
{
struct employee *tmp; /* tmp pointer value to current node */
int i = 0;/* counts the nodes printed */
/* Start a beginning of list and print out each value */
/* loop until tmp points to null (remember null is 0 or false) */
for(tmp = emp1; tmp ; tmp = tmp->next)
{
i++;
/* TODO - print other members as well */
printf("%-10s", tmp->first_name);
printf("%-10s", tmp->last_name);
printf("%-6li", tmp->id_number);
printf("%10.2f", tmp->wage);
printf("%10.1f", tmp->hours);
printf("%10.1f", tmp->overtime);
printf("%10.2f", tmp->gross);
printf(" ");
}
printf(" Total Number of Employees = %d ", i);
}
/*----------------------------------------------------------------------------*/
/* */
/* FUNCTION: main */
/* */
/* DESCRIPTION: This function will prompt the user for an employee */
/* id and wage until the user indicates they are finished. */
/* At that point, a list of id and wages will be */
/* generated. */
/* */
/* PARAMETERS: None */
/* */
/* OUTPUTS: None */
/* */
/* CALLS: print_list */
/* */
/*----------------------------------------------------------------------------*/
int main ()
{
char answer[80]; /* to see if the user wants to add more employees */
int more_data = 1; /* flag to check if another employee is to be processed */
char value; /* gets the first character of answer */
struct employee *current_ptr, /* pointer to current node */
*head_ptr; /* always points to first node */
struct stats;
/* Set up storage for first node */
head_ptr = (struct employee *) malloc (sizeof(struct employee));
current_ptr = head_ptr;
while (more_data)
{
/* TODO - Prompt for Employee Name and Hours as well here */
printf(" Please enter Employee Name:");
scanf("%s %s", & current_ptr -> first_name ,& current_ptr -> last_name);
printf(" Enter how many hours worked:");
scanf("%f", & current_ptr -> hours);
/* Read in Employee ID and Hourly Wage */
printf(" Enter employee ID: ");
scanf("%i", & current_ptr -> id_number);
printf(" Enter employee hourly wage: ");
scanf("%f", & current_ptr -> wage);
/* TODO - Call Function(s) to calculate Overtime and Gross */
/* Function call to calculate overtime */
getOT (head_ptr);
/* Function call to calculate gross pay */
grossPay(head_ptr);
printf("Would you like to add another employee? (y/n): ");
scanf("%s", answer);
/* Ask user if they want to add another employee */
if ((value = toupper(answer[0])) != 'Y')
{
current_ptr->next = (struct employee *) NULL;
more_data = 0;
}
else
{
/* set the next pointer of the current node to point to the new node */
current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
/* move the current node pointer to the new node */
current_ptr = current_ptr->next;
}
} /* while */
/* Column Header*/
printf (" ");
printf ("------------------------------------------------------------------- ");
printf ("Name clock# Wage Hours OT Gross ");
printf ("------------------------------------------------------------------- ");
/* print out listing of all employee id's and wages that were entered */
print_list(head_ptr);
/* Function to call total and average */
get_stats( head_ptr, my_totals);
printf(" End of program ");
return (0);
}
Explanation / Answer
Replace the get_stats function with below code :
----------------------------------------------------------------------
void get_stats(struct employee *emp1, struct stats my_totals)
{
int i = 0; /* loop index */
float total_wage; /* total wages */
float total_hours; /* total hours */
float total_ot; /* total overtime */
float total_gross; /* total gross pay */
float avg_wage; /* average of wages */
float avg_hours; /* average of hours */
float avg_ot; /* average of overtime */
float avg_gross; /* average of gross pay */
struct employee *tmp; /* tmp pointer value to current node */
for(tmp = emp1; tmp ; tmp = tmp->next) /* any loop is fine */
{
i++;/*process each employee */
/* calculate total and average */
total_wage += tmp->wage;
total_hours += tmp->hours;
total_ot += tmp->overtime;
total_gross += tmp->gross;
}
avg_wage= tmp->wage / i;
avg_hours= tmp->hours / i;
avg_ot= tmp->overtime / i;
avg_gross= tmp->gross / i;
printf("Total:");
printf("%30.2f", tmp->wage);
printf("%10.1f", total_hours);
printf("%10.1f", total_ot);
printf("%10.2f ", total_gross);
printf("Average:");
printf("%28.2f", avg_wage);
printf("%10.1f", avg_hours);
printf("%10.1f", avg_ot);
printf("%10.2f ", avg_gross);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.