Thread: Search through a struct.

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    Smile Search through a struct.

    Hi Guys,

    Im using Borland 4.5 on WinXP

    I have a fuction i want to call to search through the records in a struct.

    If the string passed was there then Return 1; if the string is not there or there are no records then return 0;


    At the moment the program just hangs/crashes so im not realy sure where in the code the error lies.

    If anyone can have a look through my code and point me in the right direction Id be very gratefull.
    Regards as ever
    C

    My Code:

    The code in the calling function

    Code:
    while (reg_available(record_id)!=1);
       {
       printf("Please RE-Enter A Reg No\n");
       fgets(record_id,sizeof(record_id),stdin);
       }
    
    strcpy(students_database[index].reg_no, record_id);
    printf("\nThis Registration ID Was Added\n");

    The search function that called on:

    Code:
    /* Function to check the if the  ID/string has been added already */
    
    char  reg_available(char*string1)
    {
     int loop=0;
     int found=0;
    
         while(found == 0 && loop < index)
         {
                 if( strcmp(students_database[loop].reg_no,string1) == 0)
                {
                   printf("RECORD FOUND");
                   return 0;
                   found=1;
               }
                  loop++;
    
        }
    
     if(found !=0 && loop == index)
       {
         printf("NOT FOUND\n");
       }
    return 1;
    }
    Last edited by colinuk; 02-02-2005 at 03:04 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char  reg_available(char*string1) // You should be returning an int
    {
     int loop=0;
     int found=0;
    
         while(found == 0 && loop < index)  // Unnecessary because of return below
         {
                 if( strcmp(students_database[loop].reg_no,string1) == 0)
                {
                   printf("RECORD FOUND");
                   return 0;  // You said it should return 1 if found
                   found=1;  // Pointless since you've already returned by this point
               }
                  loop++;
    
        }
    
     // If we have reached this point we already know you haven't found the record,
     // the whole if test doesn't need to be done, just do the printf and return
     if(found !=0 && loop == index)
       {
         printf("NOT FOUND\n");
       }
    return 1;  // You said it should return 0 if not found
    }
    [edit]Perhaps a more useful value to return would be an index where the match was found, a value in the range [0,index), and perhaps some negative value indicating error.[/edit]
    Last edited by hk_mp5kpdw; 02-02-2005 at 03:24 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    If it is from a file, I make like this in my code and is workig fine for me
    Code:
     struct book {
         char name[30];
    }data;
    FILE *t;
    t = fopen("book.txt", "rb+");
    while(fread(&data, sizeof(data), 1, t) == 1)
        {
            if(strcmp(data.name, bookname, n) == 0)
                printf("%s\n", &data.name);
        }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Thanks Ive made the alterations and now when a record is found it gets stuck in the loop and prints "Record Found" over and over till it crashes.


    Here is what I have:

    Code:
    int reg_available(char*string1)
    {
     int loop=0;
    
    
    while(loop < index)
    {
          if( strcmp(students_database[loop].reg_no,string1) == 0)
          {
          printf("RECORD FOUND");
                      return 0;
    
          }
        loop++;
    
    }
        if(loop == index)
        {
        printf("NOT FOUND\n");
        }
        return 1;

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Post more code. This works:
    Code:
    struct struct_type
    {
        char reg_no[5];
    } students_database[1024];
    
    int upperBound;
    
    char reg_available(char* string1)
    {
        int loop=0;
        
        while(loop < upperBound)
        {
            if (strcmp(students_database[loop].reg_no,string1) == 0)
            {
                printf("RECORD FOUND\n");
                return 0;
            }
            loop++;
            
        }
    
    //You don't really need this if-statement    
        if(loop == upperBound)
        {
            printf("NOT FOUND\n");
        }
        return 1;
    }
    
    int main()
    {
        char record_id[5];
        char ch;
    
    //give me some test data
        strcpy(students_database[0].reg_no,"abcd");
        strcpy(students_database[1].reg_no,"efgh");
        strcpy(students_database[2].reg_no,"ijkl");
        strcpy(students_database[3].reg_no,"mnop");
        strcpy(students_database[4].reg_no,"qrst");
        upperBound = 5;
    
    //go looking
        fgets(record_id,sizeof(record_id),stdin);
        while ((ch = getchar()) != '\n' && ch != EOF);
    
        while (reg_available(record_id)!=1)
        {
            printf("Please RE-Enter A Reg No\n");
            fgets(record_id,sizeof(record_id),stdin);
            while ((ch = getchar()) != '\n' && ch != EOF);
        }
    
        strcpy(students_database[upperBound].reg_no, record_id);
        printf("\nThis Registration ID Was Added\n");
        upperBound++;
    }
    I renamed your index variable to upperBound, but that should be the only change.
    Last edited by pianorain; 02-02-2005 at 03:33 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Quote Originally Posted by xxxrugby
    If it is from a file, I make like this in my code and is workig fine for me
    Code:
     struct book {
         char name[30];
    }data;
    FILE *t;
    t = fopen("book.txt", "rb+");
    while(fread(&data, sizeof(data), 1, t) == 1)
        {
            if(strcmp(data.name, bookname, n) == 0)
                printf("%s\n", &data.name);
        }

    Thanks, the data is just held by the struct, i have no external file.

    Thanks again
    C

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    More Code

    Hey Guys,

    Here is the full 2 functions code, Its running but its not quite right.

    Perhaps you guys will see what im missing.

    Thanks again

    Code:
    /*functions for adding a record... */
    
    void add_new_student(void)
    
    {
    int key;
    char record_id[40];
    clrscr();
    
    /* Add the name & ID to the database*/
    
    printf("\n\n\t\t   Enter New Name And Reg No\n\n");
    printf("Enter First Name: ");
    gets(students_database[index].fname);
    fflush(stdin);
    
    printf("Enter Last Name: ");
    gets(students_database[index].sname);
    fflush(stdin);
    
    printf("Please enter an 4 letter Reg No eg: w123 \n");
    fgets(record_id,sizeof(record_id),stdin);
    
    while(check_reg(record_id)!=1)
       {
       printf("Please RE-Enter A Reg No In The Correct Format eg: w123 \n");
       fgets(record_id,sizeof(record_id),stdin);
       }
    
    printf("Reg ID %s Is In The Correct Format Now checking for avaliability \n",record_id);
    
    
    /*check reg is available*/
    
    while(reg_available(record_id)==0);
       {
       printf("Please RE-Enter A Reg No\n");
       fgets(record_id,sizeof(record_id),stdin);
       }
    
    /*The string has passed so Add the string to the structs records*/
    
    strcpy(students_database[index].reg_no, record_id);
    printf("\nThis Registration ID Was Added\n");
    
    
    printf("\nPlease Enter The Credits \n");
    scanf("%d",&students_database[index].credit);
    fflush(stdin);
    
    
    while(students_database[index].credit <10)
    {
    printf("Credits are between 10-15 please choose again!");
    scanf("%d",&students_database[index].credit);
    fflush(stdin);
    }
    
    while(students_database[index].credit >15)
    {
    printf("Credits are between 10-15 please choose again!");
    scanf("%d",&students_database[index].credit);
    fflush(stdin);
    }
    
    key = getchar();
    
    /* Increment the array */
    index++;
    
    }
    
    /*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
    
    /* Function to check the if the  ID/string has been added already */
    
    char reg_available(char*string1)
    {
     int loop=0;
    
    
       while(loop < index)
       {
          if( strcmp(students_database[loop].reg_no,string1) == 0)
          {
          printf("RECORD FOUND");
          return 0;
    
          }
          loop++;
    
       }
    
    if(loop == index)
      {
      printf("NOT FOUND\n");
      }
      return 1;
    }
    /*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    while(reg_available(record_id)==0); //Lose the semi-colon
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Quote Originally Posted by pianorain
    Code:
    while(reg_available(record_id)==0); //Lose the semi-colon

    Thanks, I never saw that...

    It seems to be working great now.
    Thanks a lot guys.
    C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Binary Search on an Array of Struct
    By curwa1 in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2002, 02:02 PM