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 ...


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"=""><>
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;="">
{
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;="">
{
cout < vec[i].first="">< ":="" "="">< vec[i].second=""><>
}


infile.close();
}


64<br>ubuntu [Running] - Oracle VM VirtualBox<br>Activities<br>Visual Studio Code<br>Feb 15 05:04 •<br>SV<br>Labb4.cpp - labb4 - Visual Studio Code<br>File Edit selection View Go<br>Run Terminal Help<br>EXPLORER<br>TERMINAL<br>1: Code<br>> OPEN EDITO... 1 UNSAVED<br>cd

Extracted text: 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
Jun 03, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here