Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
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
Old 11-30-2006, 06:45 AM   #2 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
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
__________________
toe_cutter is offline   Reply With Quote
Old 11-30-2006, 06:49 AM   #3 (permalink)
Blondie
Recruit
 
Join Date: Nov 2006
Posts: 4
Blondie is on a distinguished road
->name' must point to struct/union

Is the common error
Blondie is offline   Reply With Quote
Old 11-30-2006, 07:08 AM   #4 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
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?
__________________
toe_cutter is offline   Reply With Quote
Old 11-30-2006, 10:36 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 12-03-2006, 08:49 PM   #6 (permalink)
albtross
Recruit
 
Join Date: Nov 2006
Posts: 9
albtross is on a distinguished road
Quote:
Originally Posted by Blondie View Post

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!
albtross is offline   Reply With Quote
Old 12-04-2006, 07:28 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
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
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 12-04-2006, 01:55 PM   #8 (permalink)
albtross
Recruit
 
Join Date: Nov 2006
Posts: 9
albtross is on a distinguished road
wow! i see my knowledge is shallow on c, time to go back reading reference books carefully
albtross is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
empty pointers are not empty using 'new' operator DJMaze Platform/API C++ 3 04-16-2005 12:11 AM
Problem with multi-dimensional array of pointers glennandrewcoop Standard C, C++ 4 03-30-2005 01:00 PM
A day late and pointers short saline Standard C, C++ 2 10-04-2003 10:33 AM
Pointers w00t Standard C, C++ 14 03-02-2003 12:15 PM
pointers .. sde Standard C, C++ 2 06-24-2002 12:54 PM


All times are GMT -8. The time now is 04:38 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting