I'm having a problem in my second performed function. After the first 'hours' are entered I get a 'segmentation fault'. Any thoughts?
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;
};
/************************************************************************/
/* begin - Function: f_input1 */
/************************************************************************/
/* */
/* Purpose: Obtains input from user */
/* */
/* Parameters: emp1 - ptr to linked list */
/* */
/* Returns: Nothing */
/* */
/************************************************************************/
void 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, *emp1 ;
/* allocate memory */
initial_pointer = (struct s_employee *)malloc(sizeof(struct s_employee));
/* Set current_pointer item to point to the first item. */
current_pointer = initial_pointer;
/* populate struct */
while ( f_counter < total_employees )
{
/* 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(current_pointer->name, f_first_name);
/* Prompt for user to input the clock # */
printf ("Please enter the clock number: ") ;
scanf ("%f", ¤t_pointer->clock_number);
/* Prompt for user to input the hourly rate of pay */
printf ("Please enter the hourly rate: ") ;
scanf ("%f", ¤t_pointer->hourly_rate);
/* process next_emp */
current_pointer = current_pointer->next_emp;
current_pointer = (struct s_employee *)malloc(sizeof(struct s_employee));
++f_counter;
}
}
/************************************************************************/
/* end - Function: f_input1 */
/************************************************************************/
/************************************************************************/
/* begin - Function: f_input2 */
/************************************************************************/
/* */
/* Purpose: Obtains hours worked from user */
/* */
/* Parameters: emp1 - ptr to linked list */
/* */
/* Returns: Nothing */
/* */
/************************************************************************/
void f_input2 (void)
{
/* local variable declaration */
int f_counter = 0 ;
/* misc pointers */
struct s_employee *current_pointer, *initial_pointer, *emp1 ;
/* Set current_pointer item to point to the first item. */
current_pointer = initial_pointer;
/* populate struct */
while ( f_counter < total_employees )
{
/* Prompt for user to input the clock hours */
printf ("Please enter the total hours worked: ") ;
scanf ("%f", ¤t_pointer->hours_worked);
printf ("\n");
/* process next_emp */
current_pointer = current_pointer->next_emp;
++f_counter;
}
}
/************************************************************************/
/* end - Function: f_input2 */
/************************************************************************/
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 */
f_input1 ();
/* perform get hours function */
f_input2 ();
/* perform grosspay calc function */
f_gross_pay_calc ();
/* perform print headings function */
f_print_headings ();
/* perform Print outstruct employee information to the screen function */
f_print_detail ();
/********************** end performed functions *************************/
}