Word Counting with 2 input files & Array

My first post here!
For this problem, I have to have two text files: one for list of words that need to be found and other containing the document that needs to be search. The results would placed in a output "results" file later one.

What I've Done: I've already figured out a way to store all of the word list onto an array.

Current Problem: How to find words that are the same and to increment the counter every time it is found.

Any help would be greatly appreciated!
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
   using namespace std;

   int main()
   {
      ifstream inFile1;
      ifstream inFile2;
      ofstream outFile;
   
      string wordList[100];
		int wordCount = 0;
   
      inFile1.open("list.txt");
      if( inFile1.fail() )
      {
         cout << "Could not open input file - Aborting program!\n";
         exit(1);
      }
      else
      {
         for(int i = 0; i < 100; i++)
         {
            inFile1 >> wordList[i];
         }
      }
   
      inFile2.open("doc.txt");
		if( inFile2.fail() )
      {
         cout << "Could not open input file - Aborting program!\n";
         exit(1);
      }
		
      outFile.open("results.txt");
		if( outFile.fail() )
    	{
        	cout << "Could not open output file - Aborting program!\n";
        	exit(1);
    	}
   
      inFile1.close();
      inFile2.close(); // Close all files explicitly
      outFile.close();
      return 0;  
   }
Last edited on
- Do you need to worry about punctuation?
- Do you need to worry about capitalization?
- How should the results file be formatted?
- For this, a “word” is simply a string of characters delimited by white space. Punctuation shouldn't matter.
- Capitalization does matter
- Format should be like this:
1
2
3
4
5
6
7
8
9
10
11
- - - - - - - - - - - -
Word        	Count
- - - - - - - - - - - -
if    		1
else    	1
for    		2
int    		2
return    	2
double    	1
void    	0
- - - - - - - - - - - -
By "capitalization does matter", do you mean that "HELLO" is a different word from "hello"? If you mean that they are the same word, then capitalization does not matter.

Are you allowed to use std::map? This would make your life a lot easier: just have the words be the keys and the count be the values - e.g. std::map<std::string, int>
"HELLO" or "Hello" is different from "hello".
I've seen sample like that on other online sources but I'm not familiar with it nor was I taught that.
If you're allowed to use it, you should learn it. If you aren't sure whether you're allowed, you'll have to make a judgement call yourself.

You can find documentation here:
http://www.cplusplus.com/reference/map/map/
http://en.cppreference.com/w/cpp/container/map

Example usage:
1
2
3
4
5
6
std::map<std::string, int> words;
std::string word;
while(word_file >> word)
{
    ++words[word]; //or: words[word] = words[word] + 1;
}
If you can't use it, you will probably be forced to use parallel arrays. In that case you will need to consider what happens when there are more unique words than slots in your arrays.
Last edited on
This is what I have now:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
   using namespace std;

   int main()
   {
      ifstream inFile1;
      ifstream inFile2;
      ofstream outFile;
   
      string wordList[100];
		string words;
		int wordCount = 0;
   
      inFile1.open("list.txt");
      if( inFile1.fail() )
      {
         cout << "Could not open input file - Aborting program!\n";
         exit(1);
      }
      else
      {
         for(int i = 0; i < 100; i++)
         {
            inFile1 >> wordList[i];
         }
      }
		
		outFile.open("results.txt");
		if( outFile.fail() )
    	{
        	cout << "Could not open output file - Aborting program!\n";
        	exit(1);
    	}
   
      inFile2.open("doc.txt");
		if( inFile2.fail() )
      {
         cout << "Could not open input file - Aborting program!\n";
         exit(1);
      }
		else
		{
		inFile2 >> words;
			for (int i = 0; i < 100; i++)
			{
				while(!inFile2.eof())
				{
					if(words == wordList[i]) wordCount++;
					inFile2 >> words;
				}
			}
		}
   	outFile << "-----------------------" << endl;
		outFile << "Word		Count" << endl;
		outFile << "-----------------------" << endl;
		for (int i = 0; i < 100; i++)
		{
  			outFile.width(2);
  			outFile << wordList[i] << endl;;
 		}
		outFile << wordCount;

		
      inFile1.close();
      inFile2.close(); // Close all files explicitly
      outFile.close();
      return 0;  
   }


Have any idea on how to compare the words on the doc.txt to the words in the array from list.txt?
Do not loop on eof, like you do on line 49. Loop on the input operation: while(inFile2 >> words)

To search for words in the other file:
- read individual words from the file
- loop through your stored words and compare to the word you just read
- if they match, you have a match
Topic archived. No new replies allowed.