Linked list not resetting between functions
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
/*Structure declaration*/
struct employee
{
char name[20];
long id_number;
float wage;
float hours;
float overtime;
float gross;
struct employee *next;
};
/*Variable declaration (global)*/
struct employee *current_ptr, /*point to current node*/
*start_ptr; /*point to first node*/
void get_info (struct employee *emp1)
{
struct employee *tmp;
char first_name[7];
char last_name[8];
int temp;
for (temp=0; temp<5; ++temp)
{
for (tmp = emp1; tmp ; tmp = tmp->next)
{
printf ("Enter employee #%i's name: ", temp+1);
scanf ("%s %s", &first_name, &last_name);
strcpy (tmp->name, first_name);
strcat (tmp->name, " ");
strcat (tmp->name, last_name);
printf ("Enter %s's ID #: ", tmp->name);
scanf ("%i", &tmp->id_number);
printf ("Enter %s's wage: ", tmp->name);
scanf ("%f", &tmp->wage);
printf ("\n");
}
}
}
void get_hours (struct employee *emp1)
{
struct employee *tmp;
int temp;
for (temp=0; temp<5; ++temp)
{
for (tmp = emp1; tmp ; tmp = tmp->next)
{
printf ("Enter %s's hours worked: ", tmp->name);
scanf ("%f", &tmp->hours);
}
}
}
void calculate_overtime (struct employee *emp1)
{
struct employee *tmp;
int temp;
for (temp=0; temp<5; ++temp)
{
for (tmp = emp1; tmp ; tmp = tmp->next)
{
if (tmp->hours > 40)
tmp->overtime = tmp->hours - 40;
else
tmp->overtime = 0;
}
}
}
void calculate_gross (struct employee *emp1)
{
struct employee *tmp;
int temp;
for (temp=0; temp<5; ++temp)
{
for (tmp = emp1; tmp ; tmp = tmp->next)
{
if (tmp->overtime > 0)
tmp->gross = (tmp->wage * 40) + ((tmp->wage * 1.5) * tmp->overtime);
else
tmp->gross = (tmp->wage * tmp->hours) + ((tmp->wage * 1.5) * tmp->overtime);
}
}
}
void output (struct employee *emp1)
{
struct employee *tmp;
int temp;
printf ("\n\n");
printf ("--------------------------------------------------------------------------\n");
printf ("Name Clock # Pay Rate Hours OT Gross Pay\n");
printf ("--------------------------------------------------------------------------\n");
for (temp=0; temp<5; ++temp)
{
for (tmp = emp1; tmp ; tmp = tmp->next)
{
printf ("%-13s %06d $%5.2f %5.1f %5.1f $%6.2f\n",
tmp->name, tmp->id_number, tmp->wage, tmp->hours,
tmp->overtime, tmp->gross);
}
}
}
int main ()
{
/* Setup storage for start_ptr (first node)*/
start_ptr = (struct employee *) malloc (sizeof(struct employee));
current_ptr = start_ptr;
/*Execute functions*/
get_info (start_ptr);
get_hours (start_ptr);
calculate_overtime (start_ptr);
calculate_gross (start_ptr);
output (start_ptr);
printf ("\n");
/*Keep window open*/
system("PAUSE");
}
The program is supposed to get 5 peoples' (nodes) info in 2 separate steps, do the calculations, then display them. My problem is that the structures are not resetting to the 1st node between functions. Right now my table display shows 5 copies (rows) of the last data set that I enter. Any suggestions on how I can correct this? Also, if you say any other random sloppiness in my code feel free to let me know. I'd rather do it right than just do it.
Me dons the flamesuit...:eek:
Thanks in advance.