I had to change system(pause) to getchar()
Also, you need to remove the '\n' from the input string after fgets
I changed the indentation style to Allman, but your way is fine.
Code:
#include <stdio.h>
#include <string.h>
char tracks[][80] =
{
"I left my Heart at Harvard Medical School",
"Newark, Newark, You suck balls",
"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]);
break;
}
else if (4 == i) // So the "nothing found" is only printed when everything is searched.
{
printf("Nothing found\n");
}
}
}
int main(void)
{
char search_for[80];
char *ch;
printf("Search for: ");
fflush(stdout); // Because string doesn't end in \n
fgets(search_for, 80, stdin);
if ((ch = strchr(search_for, '\n')) != NULL)
{
*ch = '\0';
}
printf("search_for = '%s'\n", search_for);
find_track(search_for);
//system("pause");
puts("Press enter to continue...");
getchar();
return 0;
}
[edit]
laserlight has a really good way of removing the \n from the end of a fgets string - I just can't remember it at the moment...
[/edit]