Problem finding words in text file

Hi everyone, I was given a task where we were instructed to find words in a text file via functions being called from the main form by sending the word that has to be found to the function.

Here is an explanation of what the functions do:

fileOpen - Opens the file specified

fileClose - Closes the specified file pointer

countWords - Accepts a word 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.

countWordsCase - Accepts a word and file pointer and counts the number of times the given word appears (case sensitive) within the file associated with the pointer

We were told not to make any changes to the main file which looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include "WordCount.h"

int main()
{
	WordCount* wrd = new WordCount;
	int count;
	int countCase;
	wrd->fileOpen("Input.txt");
	count = wrd->countWords("the");
	countCase = wrd->countWordsCase("the");
	if(count == 96) 
	{
		cout<<5<<endl;
	}
	if(countCase==72)
	{	
		cout<<5<<endl;
	}
		
	
	return 0;
}


Should our code be correct the output should be:

5
5


Here is the WordCount header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef WORDCOUNT_H
#define WORDCOUNT_H

#include <string>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <cstdlib>

using namespace std;


class WordCount
{
	public:
		WordCount();
		void fileOpen(string fileName);
		void fileClose();
		int countWords(string wrd);
		int countWordsCase(string word);
	private:
		ifstream* file;
};

#endif 


The problem I am facing is that only one of my two functions are working, my countWords function works perfectly and counts a total number of 96 occurrences for the word (upper and lower case) "the", thus outputting a 5.

Here is it's code inside of WordCount .cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int WordCount :: countWords(string wrd)
{
	int counter=0;
	while (!file->eof())
	{
	  string temp = "";
	  getline(*file,temp);
	  
	  for (int i = 0; i < wrd.length();i++)
	  {
	    if (temp[i]==wrd[i])
	    {
	      counter++;
	    }
	    if (toupper(temp[i])==toupper(wrd[i]))
	    {
	     counter++; 
	    }
	  }
	}
	
	return counter;
}


However my second function, countWordsCase, which is only supposed to find all the lower case occurrences of the word "the", does not work at all. For some reasone the getline does not work and the temp string is empty when I test it, but when I test it inside the countWords function it actually has a value, and that is why this function is not working:

Here is it's code inside of WordCount .cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int WordCount :: countWordsCase(string word)
{
	int counter=0;
	while (!file->eof())
	{
	  string tempo = "";
	  getline(*file,tempo);
	  
	  cout << tempo;
	  
	  for (int i = 0; i < word.length();i++)
	  {
	    if (tempo[i]==word[i])
	    {
	     counter++; 
	    }
	  }
	}
	
	return counter;
}



Why does getline return an empty value in the second function but works perfectly in the first function?

Sorry for the long explanation! If you have any idea why this is happening any help would be appreciated.

Last edited on
I fail to understand how even your first function is working.

As regards your problem, IMO after the first function is executed, the file pointer has reached the end of file. So there is nothing left to be read for the second function. You will also need to clear the error flag.

1
2
file -> clear(); // clear the error flag from previous function
file -> seekg(0, file -> beg); // move the pointer to beginning of the file. 


Also, you are reading file the wrong way. Modify the read operations to:

1
2
3
4
5
string temp;
while (getline(*file,temp))
{   
       // do whatever with temp
}


Last edited on
Thank you, clearing the error flag fixed the second function. However, I would like your advice on how to fix my first function?
Topic archived. No new replies allowed.