Thread: Stuck; Beginning C Programming

  1. #1
    Registered User Phoebe's Avatar
    Join Date
    Aug 2012
    Posts
    11

    Stuck; Beginning C Programming

    Hello World. I am teaching myself C programming with the book Head First C by David and Dawn Griffiths. I'm stuck on a particular example exercise that is supposed to search a list of song tracks. Here's the code I have so far:

    Code:
    //
    //  main.c
    //  Page85
    //
    //  Created by Weston Koyama on 8/31/12.
    //
    
    
    #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()
    
    
    {
        char search_for[80];
        printf("Search for: ");
        fgets(search_for, 80, stdin);
        find_track(search_for);
        
        return 0;
    }
    Each of the tracks in the array "tracks" represents a song. The program is supposed to ask the user to what she's looking for. The program then loops through the track names. If the track name matches the search inquiry, it should display the track name.

    Instead the program crashes.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Part of your problem is that your search_for string contains the end of line character. You will need to remove this character.

    Jim

  3. #3
    Registered User Phoebe's Avatar
    Join Date
    Aug 2012
    Posts
    11
    I'm not sure what you mean by "end of line character." I'm very novice at this so far, having only about five days of experience with programming.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    fgets appends the terminating '\n' to the returned string

    Code:
    int main() {
        char search_for[80];
        printf("Search for: ");
        fgets(search_for, 80, stdin);
    
        char * eol = strchr(search_for, '\n');
        if ( eol != 0 ) *eol= '\0';
    
        find_track(search_for);
         
        return 0;
    
    }
    Runs fine for me with that change.
    Kurt

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Phoebe View Post
    I'm not sure what you mean by "end of line character." I'm very novice at this so far, having only about five days of experience with programming.
    fgets reads characters from stdin, and it stops when you hit enter, the problem is that it includes the enter (new line) symbol in the string so it won't match your list. You can remove it with strchr() for exampel like this:

    Code:
        char *nl = strchr(search_for, '\n');
        if(nl) {
            *nl = '\0';
        }
    after you call fgets. strchr() returns a pointer to the position in the string if the character is found, else NULL. Since nl points to the character, it can be dereferenced and '\0' be assigned instead, which terminates the string at that point.
    Last edited by Subsonics; 08-31-2012 at 02:15 PM.

  6. #6
    Registered User Phoebe's Avatar
    Join Date
    Aug 2012
    Posts
    11
    Thanks for the help!!! strchr solved the problem!

  7. #7
    Registered User
    Join Date
    Aug 2012
    Location
    Lagos, Nigeria
    Posts
    17
    I have been teaching myself C programming too with books for a couple of weeks. I started with Head First C but I later had to change to another book because Head First C didn't explain programming concepts clearly.
    I'm currently reading Deitel C How To Program. I suggest you get this book, it is good for beginners.

  8. #8
    Registered User Phoebe's Avatar
    Join Date
    Aug 2012
    Posts
    11
    I think the teaching style of Head First C is for a particular type of learner. I'm having difficulty with the book as well, but I can supplement it with material on this website, and wikipedia. I don't think the authors of Head First C intended to explain all of the concepts in great detail, because they expect the reader to learn the concepts by doing them, and perhaps also with some outside research.

    What frustrates me about the book is not all the examples work, which can make you feel really stupid until you realize that the book made a mistake. This may or may not be the author's fault, as typographical errors are a fact of life, but I agree with you that some things could be made a lot clearer. I will definitely look into the book you mentioned. Thanks for the suggestion!

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    All books have errata, all of them.
    Head First C Confirmed Errata | O'Reilly Media

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've heard good things about Dietel's books.

    When you have some C under your belt, or you are really ready to dive head first down the C rabbit hole - get a copy of "The C Programming Language" Second edition or later, by Kernighan and Ritchie.

    It's not for the timid learner, but it is an amazing book - it SMELLS of C. Current testing reveals it may RADIATE C, beyond the length of your arms. THIS is the ONE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help beginning programming
    By lbillys in forum C Programming
    Replies: 15
    Last Post: 05-17-2009, 04:26 AM
  2. Beginning Windows Programming
    By Newbeee in forum Windows Programming
    Replies: 30
    Last Post: 06-28-2006, 02:16 PM
  3. Beginning Game Programming
    By jr2007 in forum Game Programming
    Replies: 1
    Last Post: 12-31-2003, 03:06 PM
  4. Beginning in programming
    By Moffesto in forum Windows Programming
    Replies: 25
    Last Post: 07-20-2002, 11:44 AM
  5. Beginning C++ Programming
    By Visual Develope in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2002, 06:11 AM

Tags for this Thread