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(); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 <vector>
#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
Transcribed Image 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education