What's went wrong?? Here is the program what count most repeated word in a file. I has problem in my output : the code count word containing number or spaces . #include #include #include #include #include #include #include #include using namespace std; //function to sort the words based on their occurences bool sortByVal(const pair &word1, const pair &word2) { return (word1.second > word2.second); } int main() { //name of file string filename; //input the name of file cout << "Enter the name of the text file: "; cin >> filename; //open the filestream ifstream infile("hitchhikersguide.txt", ios::in); //if file opening fails if (infile.fail()) { cout << "Error, The file isn't opening" << endl; exit(1); } //map to store word and its frequency map frequencyMap; //read words from file and store it in map string word; while (!infile.eof()) { infile >> word; //transform word to all uppercase transform(word.begin(), word.end(), word.begin(), ::toupper); //remove punctuations for (int i = 0, len = word.size(); i < len; i++) { if (ispunct(word[i])) { word.erase(i--, 1); len = word.size(); } } remove(word.begin(), word.end(), ' '); //insert in map if (frequencyMap.find(word) != frequencyMap.end()) { frequencyMap[word]++; } else { frequencyMap[word] = 1; } } //vector to sort words based on their frequncies vector> vec; for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++) { vec.push_back(make_pair(it->first, it->second)); } //sort the vector sort(vec.begin(), vec.end(), sortByVal); //output 10 words for (int i = 0; i <= 30; i++) { cout << vec[i].first << ": " << vec[i].second << endl; } infile.close(); }
What's went wrong??
Here is the program what count most repeated word in a file. I has problem in my output : the code count word containing number or spaces .
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <
#include <sstream>
#include <algorithm>
#include <cctype>
using namespace std;
//function to sort the words based on their occurences
bool sortByVal(const pair<string, int> &word1,
const pair<string, int> &word2)
{
return (word1.second > word2.second);
}
int main()
{
//name of file
string filename;
//input the name of file
cout << "Enter the name of the text file: ";
cin >> filename;
//open the filestream
ifstream infile("hitchhikersguide.txt", ios::in);
//if file opening fails
if (infile.fail())
{
cout << "Error, The file isn't opening" << endl;
exit(1);
}
//map to store word and its frequency
map<string, int> frequencyMap;
//read words from file and store it in map
string word;
while (!infile.eof())
{
infile >> word;
//transform word to all uppercase
transform(word.begin(), word.end(), word.begin(), ::toupper);
//remove punctuations
for (int i = 0, len = word.size(); i < len; i++)
{
if (ispunct(word[i]))
{
word.erase(i--, 1);
len = word.size();
}
}
remove(word.begin(), word.end(), ' ');
//insert in map
if (frequencyMap.find(word) != frequencyMap.end())
{
frequencyMap[word]++;
}
else
{
frequencyMap[word] = 1;
}
}
//vector to sort words based on their frequncies
vector<pair<string, int>> vec;
for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++)
{
vec.push_back(make_pair(it->first, it->second));
}
//sort the vector
sort(vec.begin(), vec.end(), sortByVal);
//output 10 words
for (int i = 0; i <= 30; i++)
{
cout << vec[i].first << ": " << vec[i].second << endl;
}
infile.close();
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images