Find same Words

I have some problem with my Code.In my code , i want my program can read the file .txt and i finish with that,but for ouput i want can
calculates and displays the amount of each of all words contained therein.Can you help me ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

int main ()
{
string read;
string line;
ifstream data;
data.open ("taks.txt"); 
while(!data.eof()) 
{
getline(data, line);
read += line+"\n";

}
data.close();
cout << read; 
getch();
}
Use std::map to map each word to the number of times that it occurs
http://www.cplusplus.com/reference/map/map/

Right now you're reading data one line at a time. You should read individual words instead:
1
2
3
4
string word;
while (data >> word) {
    ...
}

Once you have the code working like this, you'll find that it doesn't quite represent a "word" correctly. For example, this will say that "word" and "Word" are different. It will also think that these are all different words:
hello
hello,
hello.
"hello"
Hello

You could fix that problem by writing a function to normalize a word by stripping out punctuation and converting to all upper (or lower) case.
Topic archived. No new replies allowed.