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();
}
![64
ubuntu [Running] - Oracle VM VirtualBox
Activities
Visual Studio Code
Feb 15 05:04 •
SV
Labb4.cpp - labb4 - Visual Studio Code
File Edit selection View Go
Run Terminal Help
EXPLORER
TERMINAL
1: Code
> OPEN EDITO... 1 UNSAVED
cd "/home/arwakhaddour/Documents/C++/labb4/L4_wc/" && g++ Lab
b4.cpp -o Labb4 && "/home/arwakhaddour/Documents/C++/labb4/L4
wc/"Labb4
arwakhaddour@osboxes:~/Documents/C++/labb4$ cd "/home/arwakha
ddour/Documents/C++/labb4/L4_wc/" && g++ Labb4.cpp -o Labb4 &
& "/home/arwakhaddour/Documents/C++/labb4/L4_wc/"Labb4
Enter the name of the text file: hitchhikersguide.txt
v LABB4
v L4_wc
= hitchhikersguide.txt
E Labb4
G- Labb4.cpp
E main
G- main.cpp
THE: 2537
OF: 1258
A: 1197
TO: 1185
M makefile
AND: 1184
e wc.pdf
O L4_wc.zip
C- lab4.cpp
IT: 798
НЕ: 788
SAID: 681
IN: 630
WAS: 607
YOU: 564
THAT: 536
: 440
I: 428
ON: 364
HIS: 348
ARTHUR: 332
AT: 328
FOR: 317
FORD: 315
IS: 294](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe8260224-635c-426e-a960-28f2cf28de1f%2Ff2e8db1b-5ae3-49f5-b59c-dc9fd97d4d30%2Fts8inzh_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images









