If I run the below code with scanf, it returns a string if it's in the tracks array. According to the book Head First C, this should work with fgets but returns nothing for me:
#include <stdio.h>
#include <string.h>
#define MAX 80
char tracks[][MAX] = {
"The Girl from Ipanema",
"Here Comes the Sun",
"Wonderwall",
"You Belong To Me",
"Everlong",
};
void find_track(char search_for[])
{
int i = 0;
for(i = 0; i < 5; i++) {
if(strstr(tracks[i], search_for)) {
printf("Track %d: %s\n", i, tracks[i]);
}
}
}
int main()
{
char search_for[MAX];
printf("Search for: ");
//scanf("%79s", search_for);
fgets(search_for, MAX, stdin);
find_track(search_for);
return 0;
}
Input looks like the following:
./tracks
Search for:
Here
Nothing happens