Counting number of occurences of a word in a text file [UPDATED]

I have to write a function that accepts a word (in my case the word is "the") and file pointer, and counts the number of times the given word appears (case insensitive) within the file associated with the pointer. This means the two words "the" and "THE" should both be counted.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int WordCount :: countWords(string wrd)
{
	string temp;
	int counter;
	while (!file->eof())
	{
	    *file >> temp;
	    if (temp == wrd)
	    {
		counter++;
	    }
	    else if (temp == toupper[wrd])
	    {
		counter++;
	    }
	}
	
	return counter;
}


Unfortunately this does not work, any ideas?



That shouldn't compile. Fix that then try again.
BTW, std::toupper() only works . on a single char. There is no function to convert a string to majuscule. (You'll have to write one.)
Topic archived. No new replies allowed.