Lots of BUGS in your code.
Code:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#define total_employees 2
#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;
};
//changed
struct s_employee *emps;
struct s_employee *emps2; // why ?
struct s_employee *emps3;
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);
}
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(struct s_employee) ); // CHANGED to THIS STATEMENT
/* 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;
}
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; // not necessary WHY ?
}
struct s_employee *f_gross_pay_calc (struct s_employee *emps2)
{
struct s_employee *tmp = emps2;
while ( tmp != NULL )
{
if (tmp->hours_worked <= forty_hour_week)
{
tmp->gross_pay = (tmp->hours_worked * tmp->hourly_rate),
(tmp->overtime_hours = 0) ;
}
else {
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;
}
void f_print_headings (void)
{
printf ("\nName Clock Number Hours Worked OT Hours Hourly Rate Gross Pay \n") ;
printf ("-------------- ------------ ------------ -------- ----------- --------- \n") ;
}
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");
}
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) ; // why EMPS2
/* perform print headings function */
f_print_headings ();
/* perform Print employee information to the screen function */
f_print_detail (emps); // WHY EMPS3
/********************** end performed functions *************************/
}
1. Use all caps for Macro definitions. It is easier to read.
Please Explain what you want to accomplish with these fragments of code,
1.
Code:
emp1.next_emp = &emp2;
emp2.next_emp = &emp3;
emp3.next_emp = &emp4;
emp4.next_emp = &emp5;
emp5.next_emp = ( struct s_employee *) 0;
Why do you want to create a linked list like this, when you are already making one dynamically ?
2.
Code:
f_print_detail (emps); // WHY EMPS3
The biggest BUG is this one. The head of your linked list is "emps". But you are passing somethig else , i.e. emps3
3.
Code:
struct s_employee *emps;
struct s_employee *emps2; // why ?
struct s_employee *emps3;
what is the purpose of these variables ?
4.
Code:
struct s_employee *emp = malloc( sizeof(*emp) );
struct s_employee *emp = malloc( sizeof(struct s_employee) );
Malloc wants the number of bytes that need to be allocated. The sizeof operator will accept a data type and return the number of bytes required for that data type.