Thread: custom header file can someone help?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    custom header file can someone help?

    I need to make a custom header file for a program which calls for various information about employees. such as ID, First Name, Last Name, Street Address, City, Zip Code, and Phone Number.

    If i could fix the header file im pretty sure i could do the rest. But heres what I have so far...

    heres the header file.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    #define employee
    
    void ClearInputBuffer();
    employee CollectData();
    void RaiseSalary(employees * p, int N, double PercentOfArrays);
    employee FindHighestPaid(employees * p, int N);
    void DisplayEmployee(employee e);
    typedef struct {
      
    void ClearInputBuffer()
    {
    	while(getchar() ! = '\n')
    	      continue;
    }
    employee CollectData()
    {
    	long ID;
    	char FirstName[20];
    	char LastName[30];
    	double Salary;
    	char StreetAddress[50];
    	char City[30];
    	char State[2];
    	char ZipCode[10];
    	char Telephone[13];
    	
    	
    	printf("Enter ID:");
    	fflush(stdout);
    	scanf("%ld", &p.ID);
    	ClearInputBuffer();
    	printf("Enter first name:");
    	fflush(stdout);
    	scanf("%s", &p.FirstName[20]);
    	printf("Enter last name:");
    	fflush(stdout);
    	scanf("%s", &p.LastName[30]);
    	printf("Enter the Street Address:");
    	fflush(stdout);
    	scanf("%s", &p.StreetAddress[50]);
    	ClearInputBuffer();
    	printf("Enter the city:");
    	fflush(stdout);
    	scanf("%s", &p.City[30]);
    	printf("Enter the state:");
    	fflush(stdout);
    	scanf("%s", p.State[2]);
    	printf("Enter the zipcode:");
    	fflush(stdout);
    	scanf("%s", &p.ZipCode[10]);
    	printf("Enter the telephone number:");
    	fflush(stdout);
    	scanf("%s", &p.Telephone[13]);
    	printf("Enter the salary:");
    	fflush(stdout);
    	scanf("%lf", &p.Salary);
    	return 0;
    }
    void RaiseSalary(employees * p, int N, double PercentOfArrays)
    {
    	int i;
    	for(i = 0; i < N; i++)
    	{
    		PercentOfArrays = (&p -> Salary) * .06;
    	}
    	return 0;
    }
    employee FindHighestPaid(employees * p, int N)
    {
    	employee q;
    	int i;
    	q = *p
    	for(i = 1; i < N; i++)
    	{
    		if(((p + i) -> Salary > q.Salary))
    		{
    			q = *(p + i);
    		}
    	}
    	return q;
    }
    void DisplayEmployee(employee e)
    {
    	printf("----------------------------------------------\n");
    	printf("\t %s \t %ld \t %.2lf \t\n");
    	return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Only put declarations in a header, not definitions.

    What is this supposed to do?
    Code:
    #define employee
    Are employee and employees supposed to be the same?

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://www.daniweb.com/code/snippet278.html
    Last edited by Dave_Sinkula; 11-14-2005 at 09:15 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    i know i should of used "gets" with the ClearInputBuffer function. As far as employee and employees go i believe they are supposed to be the same.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by shaftinafrica
    i know i should of used "gets" with the ClearInputBuffer function.
    NOOOOOO!!! Follow the links.
    Quote Originally Posted by shaftinafrica
    As far as employee and employees go i believe they are supposed to be the same.
    And they are supposed to be a structure of some sort... passed as a parameter to the function? Before you reference the structure (like in a prototype)?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    Its supposed to be a number of arrays representing employees. And then a number of addresses within that array. Like e -> Id, e -> LastName, e -> FirstName... etc. for each employee.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Whee. You need to back up a little bit.

    For reading strings, see my previous links.
    For reading numbers, see these links.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://www.daniweb.com/code/snippet266.html
    http://www.daniweb.com/code/snippet357.html

    Define the structure, something like this.
    Code:
    typedef struct employee{
    	long ID;
    	char FirstName[20];
    	char LastName[30];
    	double Salary;
    	char StreetAddress[50];
    	char City[30];
    	char State[2];
    	char ZipCode[10];
    	char Telephone[13];
    } employee;
    Put that before your function definitions.

    Move your code to a module rather than a header.

    And then post again. That won't cure everything, but it's a start.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    thanx i appreciate ur help. ill do that and work from there.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    An overly large hint of a module that has no header:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *mygetline(char *line, int size)
    {
       if ( fgets(line, size, stdin) )
       {
          char *newline = strchr(line, '\n'); /* check for trailing '\n' */
          if ( newline )
          {
             *newline =  '\0'; /* overwrite the '\n' with a terminating null */
          }
       }
       return line;
    }
    
    long mygetli(long *result)
    {
       char buff [ 13 ]; /* signed 32-bit value, extra room for '\n' and '\0' */
       return fgets(buff, sizeof buff, stdin) && sscanf(buff, "%ld", result) == 1;
    }
    
    int mygetd(double *result)
    {
       char buff [ 32 ];
       return fgets(buff, sizeof buff, stdin) && sscanf(buff, "%lf", result) == 1;
    }
    
    typedef struct employee
    {
    	long ID;
    	char FirstName[20];
    	char LastName[30];
    	double Salary;
    	char StreetAddress[50];
    	char City[30];
    	char State[4];
    	char ZipCode[10];
    	char Telephone[13];
    } employee;
    
    employee CollectData(void)
    {
       employee p;
    
       do {
          printf("Enter ID:");
          fflush(stdout);
       } while ( !mygetli(&p.ID) );
    
       printf("Enter first name:");
       fflush(stdout);
       mygetline(p.FirstName, sizeof p.FirstName);
    
       printf("Enter last name:");
       fflush(stdout);
       mygetline(p.LastName, sizeof p.LastName);
    
       printf("Enter the Street Address:");
       fflush(stdout);
       mygetline(p.StreetAddress, sizeof p.StreetAddress);
    
       printf("Enter the city:");
       fflush(stdout);
       mygetline(p.City, sizeof p.City);
    
       printf("Enter the state:");
       fflush(stdout);
       mygetline(p.State, sizeof p.State);
    
       printf("Enter the zipcode:");
       fflush(stdout);
       mygetline(p.ZipCode, sizeof p.ZipCode);
    
       printf("Enter the telephone number:");
       fflush(stdout);
       mygetline(p.Telephone, sizeof p.Telephone);
    
       do {
          printf("Enter the salary:");
          fflush(stdout);
       } while ( !mygetd(&p.Salary) );
    
       return p;
    }
    
    int main(void)
    {
       employee e = CollectData();
       return 0;
    }
    
    /* my output
    Enter ID:12345679
    Enter first name:Dave
    Enter last name:Sinkula
    Enter the Street Address:123 Main St.
    Enter the city:Anytown
    Enter the state:XX
    Enter the zipcode:54321
    Enter the telephone number:123-4567
    Enter the salary:1000000
    */
    The State member could be smaller if it didn't work with my functions, but 2 is too small.
    Last edited by Dave_Sinkula; 11-14-2005 at 09:52 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    the problem is that i need to use a custom header file.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A bigger problem is that you don't know what a header file is supposed to do.

    What is it that you think a header file should do?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    save time and store certain peices of code for later use in other programs/modules.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    A bigger problem is that you don't know what a header file is supposed to do.
    1

    shaftinafrica,
    A header file should basically contain only function, structure and variable declaration and the other macros n typedefs.That means, the function prototypes, and not the actual function should be inside the header file. The convention is to write the function in a different library and then link the module during program execution.
    Last edited by PING; 11-15-2005 at 09:10 AM. Reason: as pointed out by dave.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by PING
    A header file should basically contain only function, structure and variable definitions and the other macros n typedefs.That means, the function prototypes, and not the actual function should be inside the header file. The convention is to write the function in a different library and then link the module during program execution.
    You've got all that right, but you've mixed up declaration and definition.
    Quote Originally Posted by c99:6.7p5
    A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
    • for an object, causes storage to be reserved for that object;
    • for a function, includes the function body;98)
    • for an enumeration constant or typedef name, is the (only) declaration of the identifier.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    ok so i got the program to run and display the employees on the screen. But im having a hard time displaying the new information after i call the "RaiseSalary" and "FindHighestPaid" function. When i call the results onto the screen with the "DisplayEmployee" function i just get zeros. If anyone can tell me why i would appreciate it. thanx. heres the code and header.

    .c file:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include "employee.h" 
    
    int main()
    {
    	double PercentOfRaise;
    	employee * p;
    	int N;
    	int i;
    	PercentOfRaise = .06;
    	printf("How many employees are there?");
    	fflush(stdout);
    	scanf("%d", &N);
    	p = (employee *)malloc(N * sizeof (employee));
    	for(i = 0;i < N; i++)
    	{
    	printf("Enter the data for employee %d\n:", i);
    	fflush(stdout);
    	*(p + i) = CollectData();      
    	}
    	for(i = 0; i < N; i++)
    	{
    		DisplayEmployee (&p[i]);
    	}
    	   RaiseSalary (&p[i], N, PercentOfRaise);
    	   DisplayEmployee (&p[i]);
    	   FindHighestPaid (&p[i], N);
    	   DisplayEmployee (&p[i]);
    	    
    	
    return 0;
    }

    Header file (employee.h):
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct employee
    {
    	long ID;
    	char FirstName[20];
    	char LastName[30];
    	double Salary;
    	char StreetAddress[50];
    	char City[30];
    	char State[4];
    	char ZipCode[10];
    	char Telephone[13];
    } employee;
      
    
    void ClearInputBuffer();
    employee CollectData();
    void RaiseSalary(employee * p, int N, double PercentOfRaise);
    employee FindHighestPaid(employee * p, int Length);
    void DisplayEmployee(employee * p);
    
    
    void ClearInputBuffer()
    {
    	while(getchar() != '\n')
    	      continue;
    }
    employee CollectData()
    {
    employee p;
    	
    	
    	printf("Enter ID:");
    	fflush(stdout);
    	scanf("%ld", &p.ID);
    	ClearInputBuffer();
    	printf("Enter first name:");
    	fflush(stdout);
    	scanf("%s", &p.FirstName[20]);
    	printf("Enter last name:");
    	fflush(stdout);
    	scanf("%s", &p.LastName[30]);
    	printf("Enter the Street Address:");
    	fflush(stdout);
    	gets(&p.StreetAddress[50]);
    	ClearInputBuffer();
    	printf("Enter the city:");
    	fflush(stdout);
    	scanf("%s", &p.City[30]);
    	printf("Enter the state:");
    	fflush(stdout);
    	scanf("%s", &p.State[2]);
    	printf("Enter the zipcode:");
    	fflush(stdout);
    	scanf("%s", &p.ZipCode[10]);
    	printf("Enter the telephone number:");
    	fflush(stdout);
    	scanf("%s", &p.Telephone[13]);
    	printf("Enter the salary:");
    	fflush(stdout);
    	scanf("%lf", &p.Salary);
    	return p;
    }
    void RaiseSalary(employee * p, int N, double PercentOfRaise)
    {
    	int i;
    	for(i = 0; i < N; i++)
    	{
    		(p + i) -> Salary += ((p + i) -> Salary) * PercentOfRaise;
    	}
    }
    employee FindHighestPaid(employee * p, int N)
    {
    	employee q;
    	int i;
    	q = *p;
    	for(i = 1; i < N; i++)  
    	{
    		if(((p + i) -> Salary > q.Salary))
    		{
    			q = *(p + i);
    		}
    	}
    	return q;
    }
    void DisplayEmployee(employee * p)
    {
    	printf("----------------------------------------------\n");
    	printf("\t %s \t %ld \t %.2lf \t\n", p -> LastName, p -> ID, p -> Salary);
    }

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Before you go too far, realize what we've been trying to say and understand that this stuff does not belong in the header:
    Code:
    void ClearInputBuffer()
    {
    	while(getchar() != '\n')
    	      continue;
    }
    employee CollectData()
    {
    employee p;
    	
    	
    	printf("Enter ID:");
    	fflush(stdout);
    	scanf("%ld", &p.ID);
    	ClearInputBuffer();
    	printf("Enter first name:");
    	fflush(stdout);
    	scanf("%s", &p.FirstName[20]);
    	printf("Enter last name:");
    	fflush(stdout);
    	scanf("%s", &p.LastName[30]);
    	printf("Enter the Street Address:");
    	fflush(stdout);
    	gets(&p.StreetAddress[50]);
    	ClearInputBuffer();
    	printf("Enter the city:");
    	fflush(stdout);
    	scanf("%s", &p.City[30]);
    	printf("Enter the state:");
    	fflush(stdout);
    	scanf("%s", &p.State[2]);
    	printf("Enter the zipcode:");
    	fflush(stdout);
    	scanf("%s", &p.ZipCode[10]);
    	printf("Enter the telephone number:");
    	fflush(stdout);
    	scanf("%s", &p.Telephone[13]);
    	printf("Enter the salary:");
    	fflush(stdout);
    	scanf("%lf", &p.Salary);
    	return p;
    }
    void RaiseSalary(employee * p, int N, double PercentOfRaise)
    {
    	int i;
    	for(i = 0; i < N; i++)
    	{
    		(p + i) -> Salary += ((p + i) -> Salary) * PercentOfRaise;
    	}
    }
    employee FindHighestPaid(employee * p, int N)
    {
    	employee q;
    	int i;
    	q = *p;
    	for(i = 1; i < N; i++)  
    	{
    		if(((p + i) -> Salary > q.Salary))
    		{
    			q = *(p + i);
    		}
    	}
    	return q;
    }
    void DisplayEmployee(employee * p)
    {
    	printf("----------------------------------------------\n");
    	printf("\t %s \t %ld \t %.2lf \t\n", p -> LastName, p -> ID, p -> Salary);
    }
    Forgive the C++ example, but something like this is how you're supposed to do it.

    And if you're going to ignore the advice you get here, I'll just bow out of this thread.

    [edit]And, by the way, these lines are wrong:
    Code:
    employee CollectData()
    {
       employee p;
    
    
       printf("Enter ID:");
       fflush(stdout);
       scanf("%ld", &p.ID);
       ClearInputBuffer();
       printf("Enter first name:");
       fflush(stdout);
       scanf("%s", &p.FirstName[20]);
       printf("Enter last name:");
       fflush(stdout);
       scanf("%s", &p.LastName[30]);
       printf("Enter the Street Address:");
       fflush(stdout);
       gets(&p.StreetAddress[50]);
       ClearInputBuffer();
       printf("Enter the city:");
       fflush(stdout);
       scanf("%s", &p.City[30]);
       printf("Enter the state:");
       fflush(stdout);
       scanf("%s", &p.State[2]);
       printf("Enter the zipcode:");
       fflush(stdout);
       scanf("%s", &p.ZipCode[10]);
       printf("Enter the telephone number:");
       fflush(stdout);
       scanf("%s", &p.Telephone[13]);
       printf("Enter the salary:");
       fflush(stdout);
       scanf("%lf", &p.Salary);
       return p;
    }
    They all put data beyond where it is supposed to go.
    Last edited by Dave_Sinkula; 11-15-2005 at 09:03 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM