Hello,
I'm running into problems with a little program I'm writing for a homework assignment. I need to count the number of times 'beer' is used in this song:
It looks quite simple and looks correct in my eyes, count however always stays 0.99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.
96 bottles of beer on the wall, 96 bottles of beer.
Take one down and pass it around, 95 bottles of beer on the wall.
Any suggestions?Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 500 char line[MAX]; FILE * pf; int count = 0; int main() { pf = fopen("beer.txt" , "r"); /* open file for reading */ while (fgets(line, MAX, pf) != NULL) /* while there's another line */ { if (strcmp(line, "beer") == 0) /* if the pattern matches */ { ++count; /* add 1 to count */ } } printf ("%d", count); /* print the result */ fclose(pf); return 0; }
Thank you.