arrays/find frequent terms

Hello,

I am trying to writing a C++ code that will count the words from a text file.
I want the code to output the top 5 most and least most frequent words and how many times it appears.
This the code i have stared on and i keep getting an error saying
Error 1 error C2872: 'count' : ambiguous symbol
why?
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX = 1000;
string words[MAX];
int instances[MAX];
int count = 0;

void insert(string input) {
	//check first, add if not present
	for (int i = 0; i < count; i++)
	if (input == words[i]) {
		instances[i]++;
		return;
	}

	if (count < MAX) {
		words[count] = input;
		instances[count] = 1;
		count++;
	}
	else
		cerr << "Too many unquie words in the file";
}

int findTop(string &word) {
	//int topIndex = 0;
	int topCount = instances[0];
	int topIndex = 0;

	for (int i = 1; i<count; i++)
	if (instances[i] > topCount) {
		topCount = instances[i];
		topIndex = i;
	}

	instances[topIndex] = 0;
	word = words[topIndex];
	//topIndex = i;
	return topCount;
}

int main()
{
	string word;
	ifstream data("story.txt");
	while (data >> word)
		insert(word);

	int topCount = 0;
	for (int i = 0; i<5; i++)
		//cout << words[i] << " " << instances[i] << endl;
		cout << findTop(word) << " " << word << endl;
	return 0;

}



- On which line are you getting the error?
- Also, why are you using an array to count the instances of match.
You can just use an integer variable and increment its value every time a matching word is found.
Its starts from line 14 through 34 where i am getting the same error
You code build OK for me, so I'm not sure...

Usually if the compiler complains about an ambiguous symbol it will point you at the two symbols which are colliding. One is your global variable. The other might be std::count(), which you're bring into the global namespace as you're using using namespace std;
http://www.cplusplus.com/reference/algorithm/count/

But as I said, I don't get the error.

Which version of Visual Studio are you using? (Going by the error code C2872 I guess it's Visual Studio you're using?)

Andy

PS Compiling the example code from here:
Compiler Error C2872
https://msdn.microsoft.com/en-us/library/t57wswcs.aspx

1
2
3
4
5
6
7
8
9
10
11
12
// C2872.cpp
namespace A {
   int i;
}

using namespace A;
int i;
int main() {
   ::i++;   // ok
   A::i++;   // ok
   i++;   // C2872 ::i or A::i?
}


I get

1>------ Build started: Project: temp_test, Configuration: Debug Win32 ------
1>  temp_test.cpp
1>temp_test.cpp(11): error C2872: 'i' : ambiguous symbol
1>          could be 'temp_test.cpp(7) : int i'
1>          or       'temp_test.cpp(3) : int A::i'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on
Topic archived. No new replies allowed.