Thread: look_up function

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    34

    look_up function

    can someone figure out why this function is not working. It keeps printing out the LAST NAME "Mikihl", and its supposed to print out the FIRST NAME of the number that matches.
    Any help would be appreciated.
    Thanx

    Code:
    /* Main used to TEST functions. */
    void main (void)
    {
     personal employee[15]=
     {
    
    	 {0,{"Mikhil"},{"Peter"},male,125000,{10,10,1987},
    {{"6 Lisa Lane"},{"Acton"},{"MA"},{"01720"}}},
    
    	{1,{"Sanchez"},{"Ray"},male,15000,{10,15,1964},
    {{"4 Foster Lane"},{"Boston"},{"MA"},{"01766"}}},
    
    	{2,{"Filmore"},{"Claire"},female,35000,{10,11,1981},
    {{"8 Hayword Road"},{"Concord"},{"MA"},{"01777"}}},
    
    	{3,{"Marshal"},{"Megan"},female,45000,{10,15,1989},
    {{"2 Chapel Road"},{"Boxborough"},{"MA"},{"01654"}}},
    
    	{4,{"Hartwick"},{"Ryan"},male,34000,{10,14,1957},
    {{"6 Short Road"},{"Bolton"},{"MA"},{"01729"}}},
    
     };
    
    
    /*****************************************************************/
    /*LOOKUP*/
     char name[25];
     int entry_number;
     int entries = 5;
     int look_up(personal employee[], char empsearch[], int entries);
    	
    	printf(" Enter Last name of Employee:  ");
    	scanf ("%9s", name);
    	entry_number = look_up(employee, name, entries);
    
    		if (entry_number != -1)
    			printf ("%s\n",employee[entry_number].F_name);
    		else
    			printf("Sorry, that employee doesn't work here!\n");
    /****************************************************************/
    }
    and the function...

    Code:
    int look_up(char empsearch[] , personal employee[],int entries)
    {
    	int i;
    	
    	for (i = 0; i<entries; i++)
    		if (myStrCmp (empsearch, employee[i].L_name))
    			return (i);
    
    	return (-1);
    }

    and the structures...
    Code:
    typedef struct
    {
     short ID_Number;
     char F_name[25];
     char L_name[25]; 
     gender gender;
     int salary;
     DOB birth;
     address stuff;
    } personal;

  2. #2
    Hello,

    Let's take a close look:
    Code:
    {0,{"Mikhil"},{"Peter"},
    First is ID_Number, second is First name, and third is Last name according to your structure:
    Code:
    typedef struct {
    	short ID_Number;
    	char F_name[25];
    	char L_name[25];
    If that makes sense either reverse the two in your structure, or reverse the initialization sequence.


    - Stack Overflow
    Last edited by Stack Overflow; 12-13-2004 at 06:22 PM. Reason: Colored for looks.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Look at your initalization:
    Code:
    {0,{"Mikhil"},{"Peter"},
    Given the structure:
    Code:
     short ID_Number;
     char F_name[25];
     char L_name[25];
    The last name of "Mikhil" is being put into the first name spot and "Peter" is being put into the last name spot.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Swap these:
    Code:
    char F_name[25];
    char L_name[25];
    Or these:
    Code:
    {"Mikhil"},{"Peter"}
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    34
    Thanks for the tip.
    Stupid me, I should of figured that out on my own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM