Thread: Help with strcmp.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Help with strcmp.

    Hi I'm new to C (and programming in general). Bascially I want the code to print out all id values where the name matches the one entered by the user. At the moment it works only if there isn't duplicate entries. If there are two people called John for example then it only displays one result.

    Code:
      while (students[index] != NULL)
        {
           if (!strcmp(students[index]->name, name))
         {
           printf("The id is: %s\n", students[index]->id);
           
         }  
           index++;
           
        }
    Any help would be appreciated!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Greetings, Liam and welcome to the forum!

    Are both johns valid strings (with an end of string char), and not have the common newline char added to the end of the name?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The answer is presumably in how you are populating the students array.

    Note also that strcmp() will compare the strings not equal (i.e. return non-zero) if their only difference is case or whitespace. So "John" is different from "john" and is also different from "Jo hn".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    I've just c&p everything involved in creating an entry:

    Code:
    void addEntry(){
      printf("\nAdd student\n");
      int index = -1;
      while(students[++index] != NULL);
      if (index > LISTSIZE-2)
      {
        printf("list is full\n");
        return;
      }
      
      char *name = readString("Enter the student's name: ");
      char *id = readString("Enter the student's id: ");
      struct entry *entry = createEntry(name, id);
      students[index] = entry;
      students[index+1] = NULL;
      free(name);
      free(id);
    }
    Code:
    char* readString(char* prompt)
    {
      char buffer[MAXLINELENGTH];
      printf("%s", prompt);
      fgets(buffer, MAXLINELENGTH, stdin);
      size_t inputLength = strlen(buffer);
      char *input = calloc(sizeof(char), inputLength);
      strncpy(input, buffer, inputLength-1);
      input[inputLength] = '\0';
      return input;
    }
    Code:
    struct entry *createEntry(char* name, char* id) {
      struct entry *entry = calloc(sizeof(struct entry),1);
      entry->name = calloc(sizeof(char), strlen(name) + 1);
      strcpy(entry->name,name);
      entry->id = calloc(sizeof(char), strlen(id) + 1);
      strcpy(entry->id, id);
      return entry;
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In some circumstances, fgets() leaves a trailing '\n' on the string.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Okay when I just use sample data eg:

    Code:
      students[0] = createEntry("Jake", "12345");
      students[1] = createEntry("John", "459967");
      students[2] = createEntry("John", "4567");
    then it works?

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by grumpy View Post
    In some circumstances, fgets() leaves a trailing '\n' on the string.
    Thanks, is there an alternative that I could be using?

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by Aero2011 View Post
    ..
    Umm you might be better off starting a new thread..

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,674
    No, just replace the \n with a \0:

    Code:
    fgets(buffer, MAXLINELENGTH, stdin);
    // strlen(buffer) - 1 will point to the character immediately
    // preceding the null terminator character, '\0', so if 
    // that character is a newline, '\n', replace it with a null 
    // terminator.
    if (buffer[strlen(buffer) - 1] == '\n')
        buffer[strlen(buffer) - 1] = '\0';
    size_t inputLength = strlen(buffer);

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Fixed, thanks everyone! Have wasted a few hours of my life on that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with strcmp()
    By shel5210 in forum C Programming
    Replies: 8
    Last Post: 04-17-2010, 11:06 AM
  2. strcmp
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 03-23-2010, 04:36 PM
  3. What does strcmp actually do?
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 01:32 PM
  4. strcmp
    By digy in forum C Programming
    Replies: 7
    Last Post: 05-15-2003, 11:49 AM
  5. strcmp
    By leebean in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2003, 05:53 PM