Thread: Head First C book example

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    Head First C book example

    I don't get something from the book to run
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char tracks[][80] = {
            "I left my heart in Harvard Med School",
            "Newark, Newark - a wonderful town",
            "Dancing with a Dork",
            "From here to maternity",
            "The girl from Iwo Jima",
    };
    
    
    void find_track(char search_for[])
    {
        int i;
        for (i = 0; i < 5; i++) {
            if (strstr(tracks[i], search_for))
                printf("Track %i: '%s'\n", i, tracks[i]);
        }
    
    
    }
    
    
    int main(void)
    {
        char search_for[80];
        printf("Search for: ");
        fgets(search_for, 80, stdin);
        find_track(search_for);
    
    
        return 0;
    }
    if anyone could help I'd be grateful
    thanks
    Mike

    edit: What I meant was: it runs, but it doesn't produce the correct results. It shows nothing, where I expect to get something for writing a substring, for example "town"
    Last edited by keisu; 04-09-2012 at 07:27 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What errors or warnings are you receiving?

    The first thing I see is that search_for is being sent to find_track, but when it arrives, find_track will be receiving a pointer to search_for: char *search_for.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by Adak View Post
    What errors or warnings are you receiving?

    The first thing I see is that search_for is being sent to find_track, but when it arrives, find_track will be receiving a pointer to search_for: char *search_for.
    Sorry, should have phrased myself a little differently. I don't get any errors/warnings(from my IDE at least). It just doesn't produce the correct result, when I run the program and type "town", I'm supposed to get
    "Newark, Newark - a wonderful town"
    But I dont get anything.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well disregard my comment above. I tend to just do these things, automatically.

    OK. Add this to main(), right after the fgets(), to remove the newline that is the monkey wrench in the code.

    Code:
      int len;
      len = strlen(search_for);
       if(search_for[len-1]=='\n')
          search_for[len-1]='\0';
    And all is well.
    Last edited by Adak; 04-09-2012 at 08:00 PM.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    fgets leaves the newline character at the end of the string. A common idiom to remove it is:
    Code:
    char *p;
    // ...
    fgets(buffer, sizeof buffer, stdin);
    if ((p = strchr(buffer, '\n')) != NULL)
        *p = 0;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by oogabooga View Post
    fgets leaves the newline character at the end of the string. A common idiom to remove it is:
    Code:
    char *p;
    // ...
    fgets(buffer, sizeof buffer, stdin);
    if ((p = strchr(buffer, '\n')) != NULL)
        *p = 0;
    Thanks it works. But just to see if I understand that code snippet correctly. strchr returns a pointer to the character's location, if found, otherwise it returns null.
    And *p = 0 removes it?

    It would seem the author got the example wrong anyway. Guess thats to be expected if one purchases the early release of a book.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by keisu View Post
    But just to see if I understand that code snippet correctly. strchr returns a pointer to the character's location, if found, otherwise it returns null. And *p = 0 removes it?
    Yes. If you entered hello, then the array right after fgets would contain h e l l o \n \0 x x x x .... (where x represents arbitrary byte values). fgets retains the newline (if there is space for it) and ensures that the string is zero-terminated. If p = strchr(buffer, '\n') returns non-null then p points to the newline character and *p = 0 sets that array element to 0, giving h e l l o \0 \0 x x x x ....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doing my head in...
    By Matty_Alan in forum C Programming
    Replies: 3
    Last Post: 04-21-2010, 05:16 AM
  2. X11 head to i2c bus
    By maes in forum Linux Programming
    Replies: 0
    Last Post: 07-25-2007, 05:28 AM
  3. this is doing my head in!!
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2005, 05:45 AM
  4. K&R is doing my head in!
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 03-06-2002, 10:32 PM
  5. Head Nod...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-16-2002, 12:58 PM