Thread: search string/compare

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    search string/compare

    I,m seaching the typedef struct on surname comparing the surname I wish to search on compare the strings then print the information out on the screen. it always find s the letter d and no other information is pinted to screen. It doesn't matterwhat surname I plugin it is always the same answer.

    /*Search the records to find a surname*/
    void search(void)
    {
    char sn[25];
    int flagcmp=0;

    printf("\n Enter the surname you wish to find: ");
    gets(sn);

    for(slot=0; slot<MAX; slot++)
    if(strcmp(people[slot].surname,sn)==0);
    flagcmp=1;
    if(flagcmp==1)
    {
    printf("Your name %s %s\n",people[slot].firstname,people[slot].surname);
    printf("Home telephone is %s\n",people[slot].home_number);
    printf("Mobile telephone is %s\n",people[slot].mobile_number);
    }
    else
    printf("\nThey are not in the list");

    } /*end search function*/

  2. #2
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Can we see:
    a) The people structure.
    b) The values you input to test this program.

    Also I don't see a declaration for slot in this function is it declared elsewhere?? (which would be a bad programming practice as many ppl will soon tell you)
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. use code tags in future

    > gets(sn);
    2. Never use gets();
    See the FAQ for details

    > if(strcmp(people[slot].surname,sn)==0);
    3. The end ; is doing something you don't want it to. In particular, flagcmp=1; always happens

    Try
    flagcmp = strcmp(people[slot].surname,sn) == 0;
    And cut out the if() altogether
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    Yes we can see people struct and so is slot.
    I've tried the
    <code tag>
    flagcmp=strcmp(people[slot].surname,sn)==0;
    </code tag>

    still no difference.
    took out the flagcmp=1;
    and I get the else statement true not in list
    I am at a loss at what I'm, doing wrong!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by fergy
    Yes we can see people struct and so is slot.
    You misunderstood. Can we see your structure. We don't care if you can see it. They were asking you to post the code for it. Next off, don't say it doesn't work. Post your newest code and your problem. Otherwise we have to guess what changes you actually made.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    string comparsion

    Sorry for the misunderstanding. I am new to this.
    I've delete and/or commented out what isn't required. Thanks
    <code>
    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>

    #define MAX 100

    //delcare prototypes

    void init_list(void);
    void enter(void);
    int find_free(void);
    void search(void);
    int menu_select(void);

    //variables
    typedef struct /*define data structure*/
    {
    char surname[20]; /*surname*/
    char firstname[15]; /*firstname*/
    char address1[25]; /*address line 1*/
    char address2[20]; /*address line 2*/
    char address3[20]; /*address line 3*/
    char postcode[10]; /*postcode*/
    char home_number[12]; /*home phone number*/
    char mobile_number[12]; /*mobile phone number*/
    char email[40]; /*email address*/
    char personID[40]; /*person identifer*/
    int mobile_indcator; /*indicator*/
    } CONTACT;

    CONTACT people[MAX]; /*variable array of the type defination CONTACT*/
    //int n; /*number of records*/
    int slot; /* array slot*/

    //main program
    int main(void)
    {
    char choice;

    printf("Welcome to the text-based menu driven electronic\n");
    printf("organiser. Please make a selection from the below menu.\n\n\n");
    load(); /* load masterfie*/

    init_list(); /* initialize the structure array */
    for(;
    {
    choice = menu_select();
    switch (choice)
    {
    case 1: {
    enter();
    }
    break;
    /* case 2: {
    delete();
    }
    break;
    case 3: {
    list();
    }
    break;*/
    case 4: {
    search();
    }
    break;
    /* case 5: {
    save();
    }
    break;
    case 6: {
    load();
    }
    break;*/
    case 7: {
    exit(0);
    }
    break;
    default: {
    printf("You have entered an invalid choose please try again\n");
    }

    } // end switch

    } //end for loop
    return (0);
    }
    //************************************************** *********************************


    /* Initialize the list. */
    void init_list(void)
    {
    int t;

    for(t=0; t<MAX; ++t) people[t].surname[0] = '\0';
    }

    //************************************************** *********************************

    /* Get a menu selection. */
    int menu_select(void)
    {
    char s[80];
    int c;

    printf("\n");
    printf("1. Enter a name and address\n");
    printf("2. Delete a name\n");
    printf("3. List the file\n");
    printf("4. Search the file\n");
    printf("5. Save the file\n");
    printf("6. Load the file\n");
    printf("7. Quit\n");
    do {
    printf("\nEnter your choice: ");
    gets(s);
    c = atoi(s); /*converts string to an integer*/
    } while(c<0 || c>7);
    return c;
    }
    //************************************************** ********************************

    /* Input addresses into the list. */
    void enter(void)

    {
    // variables

    char s[80];

    //main body of function

    slot = find_free();

    if(slot==-1)
    {
    printf("\nList Full");
    return;
    }

    printf("Please enter your firstname?\n"); /* gets first name*/
    gets(people[slot].firstname);

    printf("Please enter your surname.\n"); /*gets surname*/
    gets(people[slot].surname);

    printf("Please enter your 1st line of address.\n"); /* gets 1st line of address*/
    gets(people[slot].address1);

    printf("Please enter your 2nd line of address.\n"); /*gets 2nd line of addresse*/
    gets(people[slot].address2);

    printf("Please enter your 3rd line of address.\n"); /*gets 3rd line of addresse*/
    gets(people[slot].address3);

    printf("Enter your postcode.\n");
    gets(people[slot].postcode);

    printf("Enter your home telephone.\n");
    gets(people[slot].home_number);

    printf("Enter your mobile number.\n");
    gets(people[slot].mobile_number);

    printf("Enter your email address.\n");
    gets(people[slot].email);

    strcat(people[slot].personID,people[slot].firstname);
    strcat(people[slot].personID,people[slot].surname);



    } /*end function*/
    //************************************************** ******************************************
    /* Find an unused structure. */
    int find_free(void)
    {
    int t;

    for(t=0; people[t].surname[0] && t<MAX; ++t) ;

    if(t==MAX) return -1; /* no slots free */
    return t;
    }
    //************************************************** ******************************************
    //************************************************** **********************************************
    /*Search the records to find a surname*/
    void search(void)
    {
    char sn[20];
    int flagcmp=0;

    printf("\nEnter the surname you wish to find: ");
    gets(sn);

    for(slot=0; slot<MAX; slot++)
    if(strcmp(sn,&people[slot].surname)==0);
    flagcmp=1;
    if(flagcmp==1)
    {
    printf("Your name %s %s\n",people[slot].firstname,people[slot].surname);
    printf("Home telephone is %s\n",people[slot].home_number);
    printf("Mobile telephone is %s\n",people[slot].mobile_number);
    }
    else
    printf("\nThey are not in the list");

    } /*end search function*/
    //************************************************** **********************************************

    </code>

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    remember the code tags [ code] and [ /code] without the leading space and once again don't use
    Code:
     gets(s);
    try
    Code:
     fgets(s, sizeof s, stdin);
    much safer
    Last edited by chrismiceli; 04-06-2004 at 06:57 AM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You haven't made the changes you've been told about:
    Code:
    for(slot=0; slot<MAX; slot++)
        if(strcmp(sn,&people[slot].surname)==0);
            flagcmp=1;
    
    if(flagcmp==1)
    {
        printf("Your name %s %s\n",people[slot].firstname,people[slot].surname);
        printf("Home telephone is %s\n",people[slot].home_number);
        printf("Mobile telephone is %s\n",people[slot].mobile_number);
    }
    else
        printf("\nThey are not in the list");
    Problem. As stated, this is in effect, making your search not work right. Fix that, then see how it works.
    Problem. You don't need the address-of operator there, since what you're passing is in fact a pointer (the name of the array, surname) already.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    hey fergy are you from Liverpool John Moores Uni?
    -Ti22-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM