1

The following program was made according to the book 'Head First C'. It is supposed to be able to find and return music tracks containing a search-word, but it returns nothing. Oh, and I tried to make a 'code' space - indented it by four, but forgive me if it doesn't work.

#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("Match found.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;
}
  • It is not a bad question per se, but it doesn't contain anything to do with Ubuntu - stackoverflow.com would probably be a more suitable location. – chronitis Oct 24 '13 at 8:10
  • I posted it here since I saw similar questions before, sorry about that. – drakinosh Oct 24 '13 at 9:34
  • So go head and close the thread. – drakinosh Oct 24 '13 at 9:43
  • I thought development questions were not considered off-topic? – chaskes Oct 24 '13 at 14:06
  • 2
    Since when are dev question off-topic for Ask Ubuntu, its OPs choice if he wants to post them here or in SO, if he is using Ubuntu then its fine to post here. What a hell is wrong with you people? – Bruno Pereira Oct 24 '13 at 14:52
4

You have 2 typos that will keep this code from compiling:

  1. an extra comma after Jima in the array;
  2. a missing semicolon at the end of the printf statement

If you did some printf debugging, you would see that strstr is returning NULL everytime, so a match is never found.

printf("%s", strstr( tracks[i], search_for ) );

Why is this? The input from stdin is not being terminated properly. You need to press ctrl+d (= EOF) after typing your search word.

In fact, you need to press it twice in order to end input and flush the buffer; then it runs as intended. Try it out. Example (it's case sensitive):

Iwo ctrl+dctrl+d

| improve this answer | |
  • Thanks! It finally worked. But the extra comma was as given in the book, twice. And I have the semicolon in the original code. But after pressing ctrl+d twice, it works. – drakinosh Oct 24 '13 at 9:25
  • @user206919 The question has been re-opened. Please accept the answer if it solved your problem. :) – chaskes Oct 24 '13 at 15:17
  • 1
    Btw, if it compiled with the extra comma, use gcc -Wall to turn on (most) compiler warnings next time. Letting things like that through will cause bugs that are next to impossible to catch later. – chaskes Oct 24 '13 at 15:20
  • Finally found the 'accept' button, the tick mark, but how can I mark this thread as solved? Searches tell me to find a small triangle, which I can't seem to. – drakinosh Oct 25 '13 at 13:17
  • @user206919 Afaik, all you need to do is accept. Solved is for the forums. Thanks. – chaskes Oct 25 '13 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.