-6

I am following head first C book and now I am following the string theory part. There was a program written that return the track no and the full name of the track when a part of the track's name is searched and is found out. But the code is not working properly. Can someone please tell me what is wrong with this C program:

#include <stdio.h>
#include <stdlib.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;i5;i++){
        if (strstr(tracks[i], search_for)){
        printf("Track %i: '%s'\n", i, tracks[i]);}
    }

}

int main()
{
    printf("Hello World!\n");
    char search_for[80];
    printf("Search for: ");
    fgets(search_for, 80, stdin);
    find_track(search_for);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
pro neon
  • 253
  • 1
  • 12
  • 2
    There is a possibility that the newline may be included in the input with `fgets`. – BLUEPIXY Jul 19 '17 at 11:09
  • 2
    Not sure if a typo or the actual code, but on your `for` loop, it should be `for(i=0;i<5;i++)` – woz Jul 19 '17 at 11:10
  • 1
    for(i=0;i5;i++){ change i<=5 – EsmaeelE Jul 19 '17 at 11:10
  • 1
    Please [read this](https://stackoverflow.com/a/28462221/4142924). – Weather Vane Jul 19 '17 at 11:16
  • Your very next step should be to learn how to use your debugger. It is an essential tool that you must become good at using before you can progress:) – Martin James Jul 19 '17 at 11:17
  • change find track function to void find_track(char search_for[]) { int i; for(i=0;i<=15;i++){ if (strstr(tracks[i], search_for)){ printf("Track %i: '%s'\n", i, tracks[i]); return; } } printf("Track Not found"); } – EsmaeelE Jul 19 '17 at 11:17
  • 1
    @EsmaeelE why `i <= 15`? `i < 5` is perfectly fine. I strongly believe that the `i` error is a typo otherwise the program wouldn't even compile. The real problem was pointed by Weather Vane and BLUEPIXY. – woz Jul 19 '17 at 11:20
  • @woz Yes this is a typo i correct it in my edit proposal to Question. – EsmaeelE Jul 19 '17 at 11:22
  • Apart from the typo, the program doesn't works correctly – pro neon Jul 19 '17 at 11:23
  • @proneon add this: `search_for[strcspn(search_for, "\n")] = 0;` before calling `find_track(search_for);` on your `main` function. It's the solution based on Weather Vane's comment. – woz Jul 19 '17 at 11:25
  • 1
    @proneon the program works properly after correcting the typo and removing the newline from the input string. – Weather Vane Jul 19 '17 at 11:41
  • @EsmaeelE Your edit introduced a logic bug and also caused mismatched curly braces. – Blastfurnace Jul 19 '17 at 11:45
  • It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight Jul 19 '17 at 11:46
  • @Blastfurnace I correct curly brace. what is the logic bug? – EsmaeelE Jul 19 '17 at 11:48
  • @EsmaeelE The array has 5 elements. The loop condition should be `i < 5` because the valid indices are 0..4 – Blastfurnace Jul 19 '17 at 11:50
  • @Blastfurnace Yes thanks for catching my mistake! – EsmaeelE Jul 19 '17 at 11:54
  • @EsmaeelE One last thing, just in my opinion adding the `return` and `printf` go beyond fixing a typo into changing the author's intent. They might want to see multiple matches. – Blastfurnace Jul 19 '17 at 11:58
  • @And by change it we must catch condition of this string not found, for example by defining a flag or etc. – EsmaeelE Jul 19 '17 at 12:04
  • One of the things that's wrong with it is that you didn't read the directions on how to ask a good question. What testing have you done? In what way does it not function properly? What are you expecting the output to be and why? – David Hoelzer Jul 19 '17 at 12:18
  • I find out the fgets() function that is not work true in this example. if i try to search with constant string that i put in hard coding in source "search string" strstr works good – EsmaeelE Jul 19 '17 at 12:20

1 Answers1

-1

I find out using fgets() is the cause of error in getting user input and use this string on strstr() function.

By perform some good advice from Blastfurnace, woz and other guys in Question comment section I can fix this problem.

Code

#include <stdio.h>
#include <stdlib.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;
    char * pch;
    int flag = 0;

    for(i=0;i<5;i++){
        pch = strstr(tracks[i], search_for);
        if (pch){
            printf("find in Track   %i: '%s'\n", i, tracks[i]);
            flag = 1;//find

        }
    }
    
    if (flag == 0)// not found
        printf("Track Not found\n");

}

int main()
{
    char search_for[80];
    printf("Search for: ");
    fscanf (stdin, "%s", search_for);
    find_track(search_for);
     
    return 0;
}
Community
  • 1
  • 1
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31