View Single Post
Old 11-30-2006, 01:56 AM   #1 (permalink)
Blondie
Recruit
 
Join Date: Nov 2006
Posts: 4
Blondie is on a distinguished road
Help with pointers

Hi- I am lost. I am supposed to make an employee pay chart that prints out all the necessary info, but I find I am lost. Please help.


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define NUM_EMP 5 /*constant for the number of employees */

#define OT_RATE 1.5 /*constant for overtime rate */

#define STND_HOURS 40.0 /*constant for standard work week */
*----------------------------------------------------------------------*/
/* Function: Get_Hours */
/* */
/*Purpose: This function obtains input from the user; the number of */
/* hours worked per employee and stores the results in an */
/* array that is passed back to the calling program by */
/* reference. */
/* */
/*Parameters: clock_number-array of employee clock numbers */
/* hours-array of the number of hours worked by an employee */
/* */
/*Returns: Nothing, since clock_number and hours arrays are passed by */
/* reference. */
/* */
/*----------------------------------------------------------------------*/

void Get_Hours (long int clock_number[],float hours[])
{
int indx; /*variable used as a loop counter*/

for (indx = 0; indx < NUM_EMP; ++indx)
{
printf ("Enter the number of hours worked by employee # %06li: ",
clock_number[indx]);
scanf ("%f", &hours[indx]);
}
printf("\n\n");
}


/*----------------------------------------------------------------------*/
/* Function: OT_Hours */
/* */
/*Purpose: This function will calculate the overtime hours (greater */
/* than 40) for an employee if necessary and store the results */
/* in an array that is passed back to the calling program by */
/* reference. */
/* */
/*Parameters: hours-array of the number of hours worked by an employee */
/* OT-array of overtime hours worked for an employee if any */
/* */
/*Returns: Nothing, since the overtime hours array is passed by */
/* reference. */
/* */
/*----------------------------------------------------------------------*/

void OT_Hours (float hours[], float OT[])
{
int indx; /*variable used as a loop counter*/

for (indx = 0; indx < NUM_EMP; ++indx)
{
if (hours[indx] > STD_HOURS)
{
OT[indx] = hours[indx] - STD_HOURS;
}
else
{
OT[indx] = 0.0;
}
}
}


/*----------------------------------------------------------------------*/
/* Function: Gross_pay_calc */
/* */
/*Purpose: This function will calculate the gross pay for an employee */
/* including overtime if any and store the results in an */
/* array that is passed back to the calling program by */
/* reference. */
/* */
/*Parameters: OT-array of overtime hours for an employee */
/* wage-array of wage rates for an employee */
/* hours-array of the number of hours worked by an employee */
/* gross_pay-array of calculated gross pay for an employee */
/* */
/*Returns: Nothing, since gross_pay array is passed by reference. */
/* Overtime hours are determined by a call to the function */
/* OT_Hours. */
/* */
/*----------------------------------------------------------------------*/

void Gross_pay_calc(float OT[],float wage[],float hours[],float gross_pay[])
{
int indx; /*variable used as a loop counter*/

OT_Hours (hours, OT);
for (indx = 0; indx < NUM_EMP; ++indx)
{
if (OT[indx] >0.0)
{
gross_pay[indx]=(OT[indx]*wage[indx]*OT_RATE)+(STD_HOURS*wage[indx]);
}
else
{
gross_pay[indx] = wage[indx] * hours[indx];
}
}


/*----------------------------------------------------------------------*/
/* Function: Output_screen */
/* */
/*Purpose: This function outputs to a screen in table format the */
/* following values: */
/* Employee clock number */
/* Wage */
/* Hours */
/* OT */
/* Gross pay */
/* */
/*Parameters: clock_number-array of employee clock numbers */
/* wage-array of wage rates for an employee */
/* hours-array of the number of hours worked by an employee */
/* OT-array of overtime hours for an employee */
/* gross_pay-array of calculated gross pay for an employee */
/* */
/*Returns: Nothing. */
/* */
/*----------------------------------------------------------------------*/

void Output_screen(long clock_number[],float wage[],float hours[],float OT[],float gross_pay[])
{

int indx; /*variable used as a loop counter*/

printf ("\n");
printf ("--------------------------------------------------\n");
printf ("Clock # Wage Hours OT Gross\n");
printf ("--------------------------------------------------\n");

for (indx = 0; indx < NUM_EMP; ++indx)
{
printf ("%06li %5.2f %5.2f %5.2f %6.2f\n",
clock_number[indx],wage[indx],hours[indx],OT[indx],gross_pay[indx]);

}
}


/*----------------------------------------------------------------------*/
/* Function: Output_file */
/* */
/*Purpose: This function outputs to a text file, "home5.txt" in table */
/* Employee clock number */
/* Wage */
/* Hours */
/* OT */
/* Gross pay */
/* */
/*Parameters: clock_number-array of employee clock numbers */
/* wage-array of wage rates for an employee */
/* hours-array of the number of hours worked by an employee */
/* OT-array of overtime hours for an employee */
/* gross_pay-array of calculated gross pay for an employee */
/* */
/*Returns: Nothing. */
/* */
/*----------------------------------------------------------------------*/


void Output_file(long clock_number[],float wage[],float hours[],float OT[],float gross_pay[])
{

int indx; /*variable used as a loop counter*/
FILE *outfileptr; /*pointer to an output file*/

/*Open a file called home5.txt*/
if ((outfileptr=fopen("home5.txt", "w")) == (FILE*) NULL)
{
fprintf(stderr, "Error, Unable to open file.\n");
return;
}



fprintf (outfileptr,"--------------------------------------------------\n");
fprintf (outfileptr,"Clock # Wage Hours OT Gross\n");
fprintf (outfileptr,"--------------------------------------------------\n");

for (indx = 0; indx < NUM_EMP; ++indx)
{
fprintf (outfileptr,"%06li %5.2f %5.2f %5.2f %6.2f\n",
clock_number[indx], wage[indx], hours[indx], OT[indx], gross_pay[indx]);

}
}



struct employee
{
char name[20];
long id_number;
float wage;
float hours;
float overtime;
float gross;

struct employee *next;
};

/*----------------------------------------------------------------------*/
/* */
/* 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;
{
struct employee *tmp; /* tmp pointer value to current node */
int i = 0; /* counts the nodes printed */

/* 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++;
printf("Employee ID: %6d, Wage: %8.2f\n",tmp->id_number,
tmp->wage);
}

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 ()
{struct employee emp [3]= {

{ "Mary Sullivan",98601, 10.60},

{"Mary Martin ",99982, 10.75},

{"Mary Jones",125600, 10.45},


};/*This ends the structure*/




char answer[80];
int more_data = 1;
char value;

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;


while (more_data)
{/* prompt user to enter the employee's name, id #, and wage */
/* Note that name is an array name, and id and wage are long */
/* int and float, so they need the &. */
printf ("\n");
printf ("Please enter the employee's name: ");
scanf ("\n");
gets (pointer->name);
printf ("\nPlease enter %s's id number: ", pointer->name);
scanf ("%ld", &pointer->id_number);
printf ("\nPlease enter %s's wage: ", pointer->name);
scanf ("%f", &pointer->wage);
printf ("\n");

/* 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;
}

}

/* 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;
}
Blondie is offline   Reply With Quote