I hate to ask a "what is wrong here?" question, but I'm working through the Head First C book and I came across an issue while attempting to compile an example which I took directly from the book. The aim of the code is simply to take input from the user and search the "Track List" (the strings in the track array) for any copy of the user's exact input. Since the code is taken directly from the book, it should compile and work perfectly with no issues. However, it is not recognizing when the input is a part of any of the strings. All of my own fruitless attempts to determine the source of the problem seem to point to the if state where the strstr function is used- but it's not overly complicated, and I don't see the issue. Here is the 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 for 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;
}