W
Wabz
I am trying to use pointers to create a payroll program. I've used
various functions to do the process, but I seem to be having problems
in calling the functions appropriately so each pointer knows where to
read its data from.
Here's what I have so far...can someone help me figure out what am
doing wrong?
Many thanks.
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#define NUM_EMPL 5
#define OVERTIME_RATE 1.5
#define STD_WORK_WEEK 40
struct employee
{
char Name[20]; /* Employee name*/
long id_number; /* Employee clock number/ID No.*/
float WageRate; /* Employees Wage rate*/
float ClockedHours; /* Hours worked by employee */
float OvertimeHours; /* Overtime hours worked by employee*/
float GrossPay; /* Employees Gross Pay*/
struct employee *next;
};
/
************************************************************************/
/* Function:
CalcOvertimeHours */
/
*
*/
/* Purpose: Calculates the number of overtime hours given the
number*/
/* of hours worked. Overtime hours are the number of
hours */
/* worked after 40
hrs. */
/
*
*/
/* Parameters: ClockedHours- Number of hours worked by
employee */
/
*
*/
/* Returns: The number of overtime
hours. */
/
************************************************************************/
float CalcOvertimeHours ( float ClockedHours)
{
/* Test if hours worked is more than 40hrs, and if so return overtime
hours worked*/
if (ClockedHours > STD_WORK_WEEK)
return (ClockedHours - STD_WORK_WEEK); /*return overtime hours*/
else
return (0.0); /*if no overtime worked return zero*/
}
/
***************************************************************************/
/* Function: CalcGrossPay */
/* Purpose: Calculates gross pay including overtime given the
number */
/* of hours an employee works and wage rate. */
/* */
/*Parameters: ClockedHours - Number of hours worked by employee
*/
/* WageRate - Hourly rates of employees. */
/* GrossPay - Array of gross pay amounts for
employees. */
/* OvertimeHours - Overtime hours worked by employee */
/* */
/*Returns: Gross pay */
/* */
/
****************************************************************************/
float CalcGrossPay (float ClockedHours,float OvertimeHours,float
WageRate)
{
/*Local variable declaration*/
float GrossPay; /* Holds Grosspay calculated*/
/* Calculate GrossPay including overtime pay*/
if (ClockedHours > STD_WORK_WEEK)
{
OvertimeHours = ClockedHours - STD_WORK_WEEK;
GrossPay = WageRate*STD_WORK_WEEK + (OvertimeHours *
(WageRate*OVERTIME_RATE));
return (GrossPay);
}
/*calculate Gross Pay where there is no overtime*/
else
{
GrossPay = WageRate*ClockedHours;
return (GrossPay);
}
}
/
*----------------------------------------------------------------------
*/
/
*
*/
/* 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(emp1)
struct employee *emp1;
{
/* Declare variables */
FILE *outputfileptr; /* Pointer to the output file */
struct employee *tmp; /* tmp pointer value to current node */
int i = 0; /* counts the nodes printed */
/* open a file called Payroll.txt */
if ((outputfileptr = fopen("Payroll.txt", "w")) == (FILE *)
NULL)
{
fprintf(stderr, "Error, Unable to open file\n");
exit(1);
}
/* 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++;
/* print out header information to file */
fprintf (outputfileptr, "\nPAYROLL\n\n\n");
fprintf (outputfileptr, "\n
\n--------------------------------------------------------------------
\n");
fprintf (outputfileptr, "\nName \t\tEmployee ID \tWage \tHours \tOT
\tGross Pay\n");
fprintf (outputfileptr,
"------------------------------------------------------------------------
\n");
fprintf(outputfileptr,"%s \t\t%06i \t\t%5.1f \t%5.1f \t%8.2f\n",tmp-
}
printf("\n\nTotal Number of Employees = %d\n", 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];
int more_data = 1;
char value;
float OvertimeHours; /* Overtime hours worked by employee*/
float ClockedHours; /* Hours worked by employee */
float WageRate; /* Employees Wage rate*/
float GrossPay; /* Employees Gross Pay*/
struct employee *current_ptr, /* pointer to current node */
*head_ptr; /* always points to first node */
/* Set up storage for first node */
head_ptr = (struct employee *) malloc (sizeof(struct employee));
current_ptr = head_ptr;
/* Print program information to the screen*/
printf ("This is a program to calculate gross pay.\n");
printf ("Please enter employee data when prompted.\n\n");
while (more_data)
{
/*printf ("\n");
printf ("Please enter the employee's name: ");
scanf ("\n");
gets ("%s", ¤t_ptr->Name); CHECK THIS LATER*/
printf("\nEnter employee Name: ");
scanf("%s", ¤t_ptr->Name);
printf("\nEnter employee ID: ");
scanf("%d", ¤t_ptr->id_number);
printf("\nEnter Hours Worked: ");
scanf("%f", ¤t_ptr->ClockedHours);
printf("\nEnter employee hourly wage: ");
scanf("%f", ¤t_ptr->WageRate);
printf("\n\nWould 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;
}
}
/* Calc overtime hours Function call */
OvertimeHours = CalcOvertimeHours (ClockedHours);
/* Calc Gross Pay Function call */
GrossPay = CalcGrossPay ( ClockedHours, OvertimeHours, WageRate);
/* print out listing of all employee id's and wages that were
entered */
print_list(head_ptr);
printf("\n\nEnd of program\n");
return 0;
}
various functions to do the process, but I seem to be having problems
in calling the functions appropriately so each pointer knows where to
read its data from.
Here's what I have so far...can someone help me figure out what am
doing wrong?
Many thanks.
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#define NUM_EMPL 5
#define OVERTIME_RATE 1.5
#define STD_WORK_WEEK 40
struct employee
{
char Name[20]; /* Employee name*/
long id_number; /* Employee clock number/ID No.*/
float WageRate; /* Employees Wage rate*/
float ClockedHours; /* Hours worked by employee */
float OvertimeHours; /* Overtime hours worked by employee*/
float GrossPay; /* Employees Gross Pay*/
struct employee *next;
};
/
************************************************************************/
/* Function:
CalcOvertimeHours */
/
*
*/
/* Purpose: Calculates the number of overtime hours given the
number*/
/* of hours worked. Overtime hours are the number of
hours */
/* worked after 40
hrs. */
/
*
*/
/* Parameters: ClockedHours- Number of hours worked by
employee */
/
*
*/
/* Returns: The number of overtime
hours. */
/
************************************************************************/
float CalcOvertimeHours ( float ClockedHours)
{
/* Test if hours worked is more than 40hrs, and if so return overtime
hours worked*/
if (ClockedHours > STD_WORK_WEEK)
return (ClockedHours - STD_WORK_WEEK); /*return overtime hours*/
else
return (0.0); /*if no overtime worked return zero*/
}
/
***************************************************************************/
/* Function: CalcGrossPay */
/* Purpose: Calculates gross pay including overtime given the
number */
/* of hours an employee works and wage rate. */
/* */
/*Parameters: ClockedHours - Number of hours worked by employee
*/
/* WageRate - Hourly rates of employees. */
/* GrossPay - Array of gross pay amounts for
employees. */
/* OvertimeHours - Overtime hours worked by employee */
/* */
/*Returns: Gross pay */
/* */
/
****************************************************************************/
float CalcGrossPay (float ClockedHours,float OvertimeHours,float
WageRate)
{
/*Local variable declaration*/
float GrossPay; /* Holds Grosspay calculated*/
/* Calculate GrossPay including overtime pay*/
if (ClockedHours > STD_WORK_WEEK)
{
OvertimeHours = ClockedHours - STD_WORK_WEEK;
GrossPay = WageRate*STD_WORK_WEEK + (OvertimeHours *
(WageRate*OVERTIME_RATE));
return (GrossPay);
}
/*calculate Gross Pay where there is no overtime*/
else
{
GrossPay = WageRate*ClockedHours;
return (GrossPay);
}
}
/
*----------------------------------------------------------------------
*/
/
*
*/
/* 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(emp1)
struct employee *emp1;
{
/* Declare variables */
FILE *outputfileptr; /* Pointer to the output file */
struct employee *tmp; /* tmp pointer value to current node */
int i = 0; /* counts the nodes printed */
/* open a file called Payroll.txt */
if ((outputfileptr = fopen("Payroll.txt", "w")) == (FILE *)
NULL)
{
fprintf(stderr, "Error, Unable to open file\n");
exit(1);
}
/* 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++;
/* print out header information to file */
fprintf (outputfileptr, "\nPAYROLL\n\n\n");
fprintf (outputfileptr, "\n
\n--------------------------------------------------------------------
\n");
fprintf (outputfileptr, "\nName \t\tEmployee ID \tWage \tHours \tOT
\tGross Pay\n");
fprintf (outputfileptr,
"------------------------------------------------------------------------
\n");
fprintf(outputfileptr,"%s \t\t%06i \t\t%5.1f \t%5.1f \t%8.2f\n",tmp-
Name, tmp->id_number,tmp->WageRate, tmp->ClockedHours,tmp-
OvertimeHours, tmp->GrossPay );
}
printf("\n\nTotal Number of Employees = %d\n", 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];
int more_data = 1;
char value;
float OvertimeHours; /* Overtime hours worked by employee*/
float ClockedHours; /* Hours worked by employee */
float WageRate; /* Employees Wage rate*/
float GrossPay; /* Employees Gross Pay*/
struct employee *current_ptr, /* pointer to current node */
*head_ptr; /* always points to first node */
/* Set up storage for first node */
head_ptr = (struct employee *) malloc (sizeof(struct employee));
current_ptr = head_ptr;
/* Print program information to the screen*/
printf ("This is a program to calculate gross pay.\n");
printf ("Please enter employee data when prompted.\n\n");
while (more_data)
{
/*printf ("\n");
printf ("Please enter the employee's name: ");
scanf ("\n");
gets ("%s", ¤t_ptr->Name); CHECK THIS LATER*/
printf("\nEnter employee Name: ");
scanf("%s", ¤t_ptr->Name);
printf("\nEnter employee ID: ");
scanf("%d", ¤t_ptr->id_number);
printf("\nEnter Hours Worked: ");
scanf("%f", ¤t_ptr->ClockedHours);
printf("\nEnter employee hourly wage: ");
scanf("%f", ¤t_ptr->WageRate);
printf("\n\nWould 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;
}
}
/* Calc overtime hours Function call */
OvertimeHours = CalcOvertimeHours (ClockedHours);
/* Calc Gross Pay Function call */
GrossPay = CalcGrossPay ( ClockedHours, OvertimeHours, WageRate);
/* print out listing of all employee id's and wages that were
entered */
print_list(head_ptr);
printf("\n\nEnd of program\n");
return 0;
}