How to display top ten words

I need to display the top ten words that the program reads from the file.
but I have no idea how to start printing the top ten words and tell the user how many times the word has been used. can anyone help me? thanks!

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 <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cctype>
#include <algorithm>

using namespace std;

string toLower (string str)
{
	for (int i=0; i < str.length(); i++)
	{
		if (str[i] >='A' && str[i] <= 'Z')
			str[i] += 32;
	}
	return str;
}
string normalWord(string str)
{
	for (int i=0; i<str.length(); i++)
	{
		if(!((str[i]>='A' && str[i] <= 'Z')|| (str[i] >= 'a' && str[i] <= 'z'))) 
		{str.erase(i,1); --i;}
	}
	return str;
}
int main ()
{
	string readWord;
	string *string_pointer;
	ifstream infile;
	vector<string*> word;
	vector<int> count;
	cout<<"Input the file you want to open : " << endl;
	string nameoffile;
	cin>>ws;
	getline (cin, nameoffile);

	infile.open(nameoffile.c_str());
		if (infile.fail())
		{
			cout << "File failed to open!" << endl;
			system ("Pause");
			return 0;
		}
			while (!infile.eof())
			{
				infile >> readWord;			

				string_pointer = new string;
				*string_pointer = toLower(normalWord(readWord));
				int x = -1;
				for (int pos = 0; pos < word.size(); pos++)
				{if (*word[pos] == *string_pointer) {x= pos;}}				
					
				if ( x == -1)
				{word.push_back(string_pointer); count.push_back(1);}
				

						
				
			}
			
			cout << "Number of different words : " << word.size() << endl;
			sort (count.begin(), count.end());
			
			infile.close();		

			
				
		

	system ("Pause");
	return 0;
}
What I don't see you doing is incrementing the corresponding instance of count if you find the word.

Line 60:
1
2
3
4
else
{ // found word
   count[pos]++;
}


Now the interesting part is going to be to find the top 10 words.
One way to do that would be to keep a multimap<int,int>.
The first int (key) is the number of times a word occurs.
A multimap allows for more than one word to have the same number of occurrances.
The second int is the position of the word in your vector.
You want to build the multimap after you've passed the entire file and know the counts of each word. To print the top 10 words, simply iterate from the end of the multimap for 10 occurrances.

i applied your code, thank you. but i have another question, why does when i implement this code, it gives me such high numbers i dont even know how it got it. shouldnt it supposed to sort from smallest to biggest and print the numbers?
1
2
3
sort (count.begin(), count.end());
			for (int a=0; a<9; a++)
			{cout << count [a] << endl;}
The sort statement is going to reorder your count array.
That will cause you to lose the positional relationship with the word vector.
i.e. Assume that "Fred" is the 5th word and occurs once. Sorting count will cause the count in the 5th position to go to the front of the count vector. Now, how do you now that refers to Fred?
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98




#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string toLower (string str)
{
	for (int i=0; i < str.length(); i++)
	{
		if (str[i] >='A' && str[i] <= 'Z')
			str[i] += 32;
	}
	return str;
}
string normalWord(string str)
{
	for (int i=0; i<str.length(); i++)
	{
		if(!((str[i]>='A' && str[i] <= 'Z')|| (str[i] >= 'a' && str[i] <= 'z'))) 
		{str.erase(i,1); --i;}
	}
	return str;
}
int main ()
{
	vector<int>::const_iterator iter;
	string readWord;
	string *string_pointer;
	ifstream infile;
	vector<string*> word;
	vector<int> count;

	cout<<"Input the file you want to open : " << endl;
	string nameoffile;
	cin>>ws;
	getline (cin, nameoffile);

	infile.open(nameoffile.c_str());
		if (infile.fail())
		{
			cout << "File failed to open!" << endl;
			system ("Pause");
			return 0;
		}
			while (!infile.eof())
			{
				infile >> readWord;			

				string_pointer = new string;
				*string_pointer = toLower(normalWord(readWord));
			
				int x = -1;
				for (int pos = 0; pos < word.size(); pos++)
				{
					if (*word[pos] == *string_pointer) 
					{
						x= pos;
						count[pos]++;
						
					}					

				}
								
				if ( x == -1)
				{
					word.push_back(string_pointer);
					count.push_back(1);					
				}					
			}

			
		

			sort (word.begin() ,  word.end());
			sort (count.begin(), count.end());
			cout<< endl;
			for (iter = count.end() - 1; iter != count.end() - 11; iter--)					
				cout << *word[*iter] << " is used for " << *iter << " times." << endl; // sisa keluarin word nya doang trus udah


			

			

			cout<< endl;
			infile.close();	

	system ("Pause");
	return 0;
}


but now the problem is, i cant print the right words, i got the number right and in the right order, how do i get the right words?
You're still sorting count and losing the association between the count's position in the vector and the position of the word in the word vector.

Additionally, you're now sorting the word vector. What do you think the result of that will be? It will order the word vector in alphabetical order, which will also have no relation to the count vector.

I gave you a solution above using multimap.

ohh okay, I got it! thanks AbstractionAnon!
Topic archived. No new replies allowed.