Thread: C Functions help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Question C Functions help

    I am having issues getting my find employee function to work if someone could take a look and guide me to what needs to be changed that would be good. Thanks,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    typedef struct employee
    {
    	char* name;
    	char ssn[9];
    	int YearBorn;
    	int salary;
    }employee;
    employee* emps[20];
    
    employee e = {"Kyle", "658493867", 1900, 100000};
    
    void display(employee* e)
    {
    	printf("\n%s , %3.3s-%2.2s-%4.4s , %d\n", e->name, &e->ssn, &e->ssn[3], &e->ssn[5], e->YearBorn);
    	printf("$%d,%.3d\n", e->salary/1000, e->salary%1000);
    	
    }
    
    void readEmployee(employee* e)
    {
    	//e.name = (char*)malloc(100);
    	char buffer[100];
    	printf("\nEmployee Name: ");
    	gets(buffer);
    	e->name = strdup(buffer);
    	
    	printf("\nEmployee SSN: ");
    	gets(buffer);
    	strcpy(e->ssn, buffer);
    	
    	printf("\nEnter Year you were born: ");
    	scanf("%d", &e->YearBorn);
    	
    	printf("\nPlease enter Employee Salary: ");
    	scanf("%d", &e->salary);
    	
    	
    }
    
    employee* createEmployee()
    {
    	employee* emps;
    	emps = (employee*) malloc (sizeof(employee));
    	readEmployee(emps);
    	return emps;
    	
    }
    
    releaseEmployee(employee* emp)
    {
    	free (emp->name);
    	free (emp);
    }
    
    listEmployees(employee* emps)
    {
    display(emps);
    }
    
    employee *findEmployee(const char *name)
    {
    	size_t sz = employee_length - 1;
        for (; sz < employee_length; --sz)
        {
            if (strcmp(employee_list[sz]->name, employee_name))
                { return employee_list[sz]; } /* an employee was found */
        }
        return NULL; /* no employees were found matching employee_name */	
    }
    int main(int argc, char** argv)
    {
    	int select = 0;
    	while(select != 3)
    	{
    	printf("Type one of the following\n\n");
    	printf("1 for Hire\n");
    	printf("2 for List\n");
    	printf("3 for Find Employee");
    	printf("4 for Quit\n");
    	scanf("%d",select);
    	
    	if(select = 1)
    	{
    			createEmployee();
    			getchar();
    	}
    	if(select = 2)
    	{
    		listEmployees(emp);
    	}
    	if(select = 3)
    	{
    		findEmployee(emp);
    	}
    	if(select = 4)
    	{
    		releaseEmployee(emps);
    	}
    	return 0;
    	}

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    strcmp(employee_list[sz]->name, employee_name)
    returns zero if it's a match, you are checking if it returns non-zero, and then return that employee, which means that if you'll always get back the first (or rather last) element of your array - except when that's what you want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	gets(buffer);
    Never, ever use gets.
    http://cpwiki.sf.net/Gets

    Code:
    if (strcmp(employee_list[sz]->name, employee_name))
    strcmp returns 0 if strings are equal, not > 0.

    Code:
    if(select = 1)
    if(select = 2)
    if(select = 3)
    if(select = 4)
    Assign (=) instead of compare (==).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    I am getting an error saying "A Symbol 'employee_length' has not been declared" What does that mean i cant figure out the error.

  5. #5
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by mtber View Post
    I am getting an error saying "A Symbol 'employee_length' has not been declared" What does that mean i cant figure out the error.
    Show me where you have employee_length defined and initialized because I can't see it anywhere.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it means that you are using this variable without declaration. Each var in C shouldbe declared before usage
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or, in better words, you are trying to use a variable that doesn't exist. Every variable needs to be created first, which is what you do with a declaration or definition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, I don't know if you'd say that a declaration "creates" a variable, but we know what you mean.

    The ssn member of your structure needs to be one element larger, to account for the NULL.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    Well, I don't know if you'd say that a declaration "creates" a variable, but we know what you mean.
    Indeed, but some say declare and some say define.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, there's actually a difference. These are definitions:
    Code:
    void function(int, char);
    extern int x;
    And these are declarations:
    Code:
    void function(int, char) { /* ... */ }
    int x = 3;
    That's what I meant. A definition is like "extern int x", and it says that somewhere, there is a variable called int x. It doesn't "create" a variable in the sense that it doesn't allocated space for it, it just uses the space already allocated for that variable elsewhere.

    Most definitions or declarations of variables are declarations. For example, these are all declarations:
    Code:
    int x;
    {
        auto char c;
        for(int x = 0; /* ... */) {}
    }
    Declarations allocate space for variables, and thus do "create" them. I would argue that definitions do not "create" variables, however.

    You may remember that in Chapter 1 we said that you have to declare the names of things before you can use them (the only exceptions to this rule are the names of functions returning int, because they are declared by default, and the names of labels). You can do it either by using a declaration, which introduces just the name and type of something but allocates no storage, or go further by using a definition, which also allocates the space used by the thing being declared.

    The distinction between declaration and definition is an important one, and it is a shame that the two words sound alike enough to cause confusion.
    From http://publications.gbdirect.co.uk/c...claration.html

    [edit] Technically, however, every definition is also a declaration, which I didn't know. http://bytes.com/forum/thread560781.html [/edit]
    Last edited by dwks; 04-09-2008 at 01:31 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A declaration is something that brings a keyword into the existance for the compiler.
    And definition is something that describes something, in how it works, or so.
    That was how the standard worded it if I don't remember wrong. There was a discussion about that long ago.
    I say variable definitions since basically anything that takes up memory is a definition, or thus was the conclusion anyway. But you could say either define or declare for variables.
    But there are some things that are only declarations, such as variables with the extern keyword, which is a declaration. But "int x" is a definition if you ask me, but you could call it declaration, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM