My final data does not display
My final data does not display. I must being passing the pointers incorrectly to the subsequent functions. Can anyone help? I didn't want to list all my code but felt it was necessary for clarity.
code:--------------------------------------------------------------------------------
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#define total_employees 5
#define over_time_rate 1.5
#define forty_hour_week 40
struct s_employee
{
char name[20] ; /* user name */
int clock_number ; /* clock # */
float hourly_rate ; /* hourly rate of pay */
float hours_worked ; /* hours worked */
float overtime_hours ; /* OT hours worked */
float gross_pay ; /* calc gross pay amount */
float forty_hour_pay ; /* calc gross pay amount for 40 hours */
float overtime_pay ; /* calc gross pay amount for time > 40 hours */
struct s_employee *next_emp;
};
struct s_employee *emps;
struct s_employee *emps2;
struct s_employee *emps3;
/************************************************** **********************/
/* begin - Function: f_explain_messsage */
/************************************************** **********************/
/* */
/* Purpose: explain what we will be processing */
/* */
/* Parameters: none */
/* */
/* Returns: Nothing, since we're only displaying an informational */
/* message */
/* */
/************************************************** **********************/
void f_explain_message (void)
{
printf ("\nWe will determine gross pay based on hours ");
printf ("worked times rate of pay. Overtime will be calculated.");
printf ("\nWe will process a total of five employees.\n\n") ;
fflush(stdout);
}
/************************************************** **********************/
/* end - Function: f_explain_message */
/************************************************** **********************/
/************************************************** **********************/
/* begin - Function: f_input1 */
/************************************************** **********************/
/* */
/* Purpose: Obtains input from user */
/* */
/* Parameters: emp1 - ptr to linked list */
/* */
/* Returns: Nothing */
/* */
/************************************************** **********************/
struct s_employee *f_input1 (void)
{
/* local variable declaration */
int f_counter = 0 ;
int f_clock_number = 0 ;
char f_first_name [20] = "" ;
char f_last_name [20] = "" ;
float f_hourly_rate = 0 ;
/* misc pointers */
struct s_employee *current_pointer, *initial_pointer;
/* null pointers */
initial_pointer = NULL;
current_pointer = NULL;
/* populate struct */
while ( f_counter < total_employees )
{
/* allocate memory */
struct s_employee *emp = malloc( sizeof(*emp) );
/* Prompt for user to input their first name / last name */
/* concat first/last name and move to array struct, copy string */
printf ("Please enter the first name: ");
scanf ("%s", f_first_name);
printf ("Please enter the last name: ");
scanf ("%s", f_last_name);
strcat (f_first_name, " ");
strcat (f_first_name, f_last_name);
strcpy(emp->name, f_first_name);
/* Prompt for user to input the clock # */
printf ("Please enter the clock number: ") ;
scanf ("%i", &emp->clock_number);
/* Prompt for user to input the hourly rate of pay */
printf ("Please enter the hourly rate: ") ;
scanf ("%f", &emp->hourly_rate);
printf("\n");
emp->next_emp = NULL;
if ( initial_pointer == NULL )
{
/* list empty, this is where it starts */
initial_pointer = emp;
}
else
/* non-empty list, append to the end of the list */
{
current_pointer->next_emp = emp;
}
/* process next_emp */
current_pointer = emp;
++f_counter;
}
/* return pointer to the start of the list */
return initial_pointer;
}
/************************************************** **********************/
/* end - Function: f_input1 */
/************************************************** **********************/
/ **************************************************
**********************/
/* begin - Function: f_input2 */
/ **************************************************
**********************/
/* */
/* Purpose: Obtains hours worked from user */
/* */
/* Parameters: emps - ptr to linked list */
/* */
/* Returns: original pointer */
/* */
/ **************************************************
**********************/
struct s_employee *f_input2 (struct s_employee *emps)
{
/* misc pointers */
struct s_employee *tmp = emps;
while ( tmp != NULL )
{
printf("Please enter the total hours worked by %s: ", tmp->name );
scanf("%f", &tmp->hours_worked);
printf("\n");
tmp = tmp->next_emp;
}
/* return pointer to the start of the list */
return emps;
}
/ **************************************************
**********************/
/* end - Function: f_input2 */
/ **************************************************
**********************/
/ **************************************************
**********************/
/* begin - Function: f_gross_pay_calc */
/ **************************************************
**********************/
/* */
/* Purpose: calculate gross pay */
/* */
/* Parameters: emps2 - ptr to linked list */
/* */
/* Returns: initial pointer */
/* */
/ **************************************************
**********************/
struct s_employee *f_gross_pay_calc (struct s_employee *emps2)
{
struct s_employee *tmp = emps2;
while ( tmp != NULL )
{
/ **************************************************
**********************/
/* If hours worked <= 40 */
/* Calculate gross pay by multiplying hours worked by the hourly rate */
/* overtime hours is set to 0 */
/ **************************************************
**********************/
if (tmp->hours_worked <= forty_hour_week)
{
tmp->gross_pay = (tmp->hours_worked * tmp->hourly_rate),
(tmp->overtime_hours = 0) ;
}
else {
/ **************************************************
**********************/
/* else If hours worked > 40 */
/* calculate pay for 40 hours worked */
/* calculate overtime hours = hours worked minus 40 */
/* calculate overtime pay by multiplying hours hourly rate times 1.5 */
/* and that result times overtime hours */
/* and finally, calculate gross pay by adding 40 hour pay + overtime pay*/
/ **************************************************
**********************/
tmp->forty_hour_pay = (tmp->hourly_rate * forty_hour_week) ;
tmp->overtime_hours = (tmp->hours_worked - forty_hour_week) ;
tmp->overtime_pay =
(tmp->overtime_hours * (tmp->hourly_rate * over_time_rate)) ;
tmp->gross_pay =
(tmp->forty_hour_pay + tmp->overtime_pay) ;
}
tmp = tmp->next_emp;
}
/* return pointer to the start of the list */
return emps2;
}
/ **************************************************
**********************/
/* end - Function: f_gross_pay_calc */
/ **************************************************
**********************/
/ **************************************************
**********************/
/* begin - Function: f_print_headings */
/ **************************************************
**********************/
/* */
/* Purpose: print rpt headings */
/* */
/* Parameters: none */
/* */
/* Returns: Nothing, since we're only displaying headers */
/* */
/ **************************************************
**********************/
void f_print_headings (void)
{
printf ("\nName Clock Number Hours Worked OT Hours Hourly Rate Gross Pay \n") ;
printf ("-------------- ------------ ------------ -------- ----------- --------- \n") ;
}
/ **************************************************
**********************/
/* end - Function: f_print_headings */
/ **************************************************
**********************/
/ **************************************************
**********************/
/* begin - Function: f_print_detail */
/ **************************************************
**********************/
/* */
/* Purpose: prints all detail info to screen */
/* */
/* Parameters: emps3 - ptr to linked list */
/* */
/* Returns: Nothing */
/* */
/ **************************************************
**********************/
void f_print_detail (struct s_employee *emps3)
{
struct s_employee *tmp = emps3;
while ( tmp != NULL )
{
printf ("%-13s"" %06i %5.1f %4.1f $%6.2f $%7.2f\n",
tmp->name,
tmp->clock_number, tmp->hours_worked,
tmp->overtime_hours, tmp->hourly_rate,
tmp->gross_pay) ;
tmp = tmp->next_emp;
}
printf ("\n\n");
}
/ **************************************************
**********************/
/* end - Function: f_print_detail */
/ **************************************************
**********************/
main()
{
/* create a linked list */
struct s_employee emp1, emp2, emp3, emp4, emp5;
emp1.next_emp = &emp2;
emp2.next_emp = &emp3;
emp3.next_emp = &emp4;
emp4.next_emp = &emp5;
emp5.next_emp = ( struct s_employee *) 0;
/********************** begin performed functions ***********************/
/* Explain what we will be processing */
f_explain_message ();
/* perform get misc function */
emps = f_input1();
/* perform get hours function */
f_input2(emps);
/* perform grosspay calc function */
f_gross_pay_calc (emps2);
/* perform print headings function */
f_print_headings ();
/* perform Print employee information to the screen function */
f_print_detail (emps3);
/********************** end performed functions *************************/
}
[/code]