|
 |
|
 |
11-30-2006, 01:56 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Nov 2006
Posts: 4
|
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;
}
|
|
|
11-30-2006, 06:45 AM
|
#2 (permalink)
|
|
Code Monkey
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
|
Were are you lost and what do you need help with? I see your title says help with pointers, but in what respect? Are you getting errors, or are your pointers pointing to the wrong data?
-toe
__________________
|
|
|
11-30-2006, 06:49 AM
|
#3 (permalink)
|
|
Recruit
Join Date: Nov 2006
Posts: 4
|
->name' must point to struct/union
Is the common error
|
|
|
11-30-2006, 07:08 AM
|
#4 (permalink)
|
|
Code Monkey
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
|
Were do you declare the variable or should I say pointer called pointer?
used here: gets (pointer->name);
I also notice that you have name as a char[20], but when the user input the name you do not checking for size. I could be wrong, but I think that could cause a buffer over flow?
__________________
|
|
|
11-30-2006, 10:36 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Sorry, but you never declare pointer, you use it alot in your comments, but at no point in your code, does it say anything like
Code:
struct employee pointer;
Or anything similar which would make the name member available in your gets()call.
Oh, and by the way, not using [.code] and [/code] tags around your actual code, is a sure way to make me not go any deeper into the code itself, for that amount it's simply too much of a hassle when you can't follow any obvius indentation.
|
|
|
12-03-2006, 08:49 PM
|
#6 (permalink)
|
|
Recruit
Join Date: Nov 2006
Posts: 9
|
Quote:
Originally Posted by Blondie
struct employee
{
char name[20];
long id_number;
float wage;
float hours;
float overtime;
float gross;
struct employee *next;
};
void print_list(emp1)
struct employee *emp1;
{
struct employee *tmp;
int i = 0;
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);
}
|
basically, in this program, you are playing with pointers, i am not sure about what i am saying, but anyway just expressing my ideas, i can learn from it too.
first, in struct employee declaration, you have nested struct pointer def, is that legal?
second, void print_list(emp1) i assume it's a function not a prototype, then you declare another two pointer struct inside it, aren't you supposed to put them inside block? also, you are passing emp1 as an argument, and then you declare it again.
third, tmp = tmp->next, struct pointer point to another struct pointer?
teach me if i said something wrong!
|
|
|
12-04-2006, 07:28 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Quote:
|
first, in struct employee declaration, you have nested struct pointer def, is that legal?
|
Yes it is perfect legal, it's just a pointer and since you'd declared it as struct name { items;}; instead of typedef struct { items;}name; you are able to reference the name of the struct within the struct eventho it is not yet completely declared.
Quote:
|
second, void print_list(emp1) i assume it's a function not a prototype, then you declare another two pointer struct inside it, aren't you supposed to put them inside block?
|
The declaration of the print_list() is correct aswell, since theres no definition of what emp1 within the function arg list, you declare it emidiate after, before the body of the function, thus the struct employee *emp1; emidiately following the function definition, it's K&R style definitions..
Considder this
Code:
redhead@fenris{56} ~> cat test.c
#include <stdio.h>
int blah(s) const char* s;
{
printf("%s\n", s);
return 0;
}
int main()
{
blah("this is a test");
return 0;
}
redhead@fenris{57} ~> gcc -Wall -ansi test.c
redhead@fenris{58} ~> ./a.out
this is a test
redhead@fenris{59} ~>
Quote:
|
third, tmp = tmp->next, struct pointer point to another struct pointer?
|
No, not if you follow the scheme of the function, tmp is pointing to the parsed argument emp1 to the function, or just using tmp as a temporary placeholder so teh parsed argument wont be messed up.
Quote:
|
teach me if i said something wrong!
|
I belive I just did 
|
|
|
12-04-2006, 01:55 PM
|
#8 (permalink)
|
|
Recruit
Join Date: Nov 2006
Posts: 9
|
wow! i see my knowledge is shallow on c, time to go back reading reference books carefully
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:38 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|