Count the number of times each word occurs in a file

Hi I am writing a program that counts the number of times each word occurs in a file. Then it prints a list of words with counts between 800 and 1000, sorted in the order of count. I am stuck on keeping a counter to see if the first word matches the next until a new word appears. In the main I am trying to open the file, read each word by word and call sort in the while loop to sort the vector. Then, in the for loop go through all the words and if the first word equals the second count++. I don't think that is how you keep a counter.

Here is the code:
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
72
73
74
75
76
77
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
 
using namespace std;

vector<string> lines;
vector<int> second;
set<string> words;
multiset<string> multiwords;

void readLines(const char *filename)
{
    string line;
    ifstream infile;
    infile.open(filename);
    if (!infile)
    {       
		cerr << filename << " cannot open" << endl; 
          return; 
    }       
    getline(infile, line);
    while (!infile.eof())
    {
		lines.push_back(line);
		getline(infile, line);
    }  
    infile.close();
}

int binary_search(vector<string> &v, int size, int value)
{
    int from = 0;
	int to = size - 1;
	while (from <= to)
	{  
		int mid = (from + to) / 2;
		int mid_count = multiwords.count(v[mid]);
		if (value == mid_count) 
			return mid;
		if (value < mid_count) to = mid - 1;
		else from = mid + 1;
	}
   return from;
}

int main() 
{
	vector<string> words;
	string x;
	ifstream inFile;
	int count = 0;

	inFile.open("bible.txt");
	if (!inFile) 
	{
		cout << "Unable to open file";
		exit(1);
	}
	while (inFile >> x){
		sort(words.begin(), words.end());
	}

	for(int i = 0;i < second.size();i++)
	{
		if(x == x+1)
		{
			count++;
		}
		else
			return;
	}
	inFile.close();
}
Last edited on
closed account (zwA4jE8b)
1
2
3
4
5
6
7
struct words
{
  string word;
  int occurrences;
};

vector<words*> Vstruct;


read the first word into a new struct and set occurrences to 1, push into vector.
read the second word, if it is in the vector update that words occurrences else, new struct, push.
repeat.

When done sort by the field, occurrences.

That is how I would attempt it.
Last edited on
Are you saying to do this in the loop?
closed account (zwA4jE8b)
well, it would be different than what you have.

the algorithm would be like...

Open the file.

While (not end of file)
{
read a word into a string variable.
-start an inner loop to iterate through the vector and check if the word is in there.
if the word is not in the list. (it will not be for the first word)
{
create new struct and set occurrences to 1, push into vector.
1
2
3
4
5
        words* _temp = new words;
        _temp->word = (string variable);
        _temp->occurrences = 1;
       Vstruct.push(_temp);
    

}
else
add 1 to the occurrences field for the found word.
}
Last edited on
Topic archived. No new replies allowed.