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 occurencesbool sortByVal(const pair &word1,const pair &word2){return (word1.second > word2.second);}int main(){//name of filestring filename;//input the name of filecout < "enter="" the="" name="" of="" the="" text="" file:="">cin >> filename;//open the filestreamifstream infile("hitchhikersguide.txt", ios::in);//if file opening failsif (infile.fail()){cout < "error,="" the="" file="" isn't="" opening"=""><>exit(1);}//map to store word and its frequencymap frequencyMap;//read words from file and store it in mapstring word;while (!infile.eof()){infile >> word;//transform word to all uppercasetransform(word.begin(), word.end(), word.begin(), ::toupper);//remove punctuationsfor (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 mapif (frequencyMap.find(word) != frequencyMap.end()){frequencyMap[word]++;}else{frequencyMap[word] = 1;}}//vector to sort words based on their frequnciesvector<>> vec;for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++){vec.push_back(make_pair(it->first, it->second));}//sort the vectorsort(vec.begin(), vec.end(), sortByVal);//output 10 wordsfor (int i = 0; i <= 30;="">=>{cout < vec[i].first="">< ":="" "="">< vec[i].second=""><>}infile.close();}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
using namespace std;
//function to sort the words based on their occurencesbool sortByVal(const pair &word1,const pair &word2){return (word1.second > word2.second);}int main(){//name of filestring filename;//input the name of filecout < "enter="" the="" name="" of="" the="" text="" file:="">cin >> filename;//open the filestreamifstream infile("hitchhikersguide.txt", ios::in);//if file opening failsif (infile.fail()){cout < "error,="" the="" file="" isn't="" opening"=""><>exit(1);}//map to store word and its frequencymap frequencyMap;//read words from file and store it in mapstring word;while (!infile.eof()){infile >> word;//transform word to all uppercasetransform(word.begin(), word.end(), word.begin(), ::toupper);//remove punctuationsfor (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 mapif (frequencyMap.find(word) != frequencyMap.end()){frequencyMap[word]++;}else{frequencyMap[word] = 1;}}//vector to sort words based on their frequnciesvector<>> vec;for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++){vec.push_back(make_pair(it->first, it->second));}//sort the vectorsort(vec.begin(), vec.end(), sortByVal);//output 10 wordsfor (int i = 0; i <= 30;="">=>{cout < vec[i].first="">< ":="" "="">< vec[i].second=""><>}infile.close();}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
int main(){
//name of filestring filename;
//input the name of filecout < "enter="" the="" name="" of="" the="" text="" file:="">cin >> filename;
//open the filestreamifstream infile("hitchhikersguide.txt", ios::in);
//if file opening failsif (infile.fail()){cout < "error,="" the="" file="" isn't="" opening"=""><>exit(1);}
//map to store word and its frequencymap frequencyMap;//read words from file and store it in mapstring word;while (!infile.eof()){infile >> word;//transform word to all uppercasetransform(word.begin(), word.end(), word.begin(), ::toupper);//remove punctuationsfor (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 mapif (frequencyMap.find(word) != frequencyMap.end()){frequencyMap[word]++;}else{frequencyMap[word] = 1;}}//vector to sort words based on their frequnciesvector<>> vec;for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++){vec.push_back(make_pair(it->first, it->second));}//sort the vectorsort(vec.begin(), vec.end(), sortByVal);//output 10 wordsfor (int i = 0; i <= 30;="">=>{cout < vec[i].first="">< ":="" "="">< vec[i].second=""><>}infile.close();}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
//read words from file and store it in mapstring word;while (!infile.eof()){infile >> word;
//transform word to all uppercasetransform(word.begin(), word.end(), word.begin(), ::toupper);
//remove punctuationsfor (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 mapif (frequencyMap.find(word) != frequencyMap.end()){frequencyMap[word]++;}else{frequencyMap[word] = 1;}}
//vector to sort words based on their frequnciesvector<>> vec;for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++){vec.push_back(make_pair(it->first, it->second));}//sort the vectorsort(vec.begin(), vec.end(), sortByVal);//output 10 wordsfor (int i = 0; i <= 30;="">=>{cout < vec[i].first="">< ":="" "="">< vec[i].second=""><>}infile.close();}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
for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++){vec.push_back(make_pair(it->first, it->second));}
//sort the vectorsort(vec.begin(), vec.end(), sortByVal);
//output 10 wordsfor (int i = 0; i <= 30;="">=>{cout < vec[i].first="">< ":="" "="">< vec[i].second=""><>}
infile.close();}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here