Looking for help for a coding assignment in C++. I would like some help with cod
ID: 3626427 • Letter: L
Question
Looking for help for a coding assignment in C++.I would like some help with coding a simple program with comments to go along with the code for ease of understanding. I am not looking for anything complex just something as simple as possible in order for me to understand what is going on.
Here is the question:
Looking for a program that counts how often, each word occur in a text file. All while using multiset<string>.
So from reading the question if one where to write a simple sentence it would count how many times each word was used. I would say a minimum of 5 words per sentence to keep it a small code.
Thanks in advance
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <set>
using namespace std;
int main ()
{
multiset<string> mymultiset; //multiset to store words
multiset<string>::iterator it; //iterator for multiset
//filestream to open a file for input
ifstream inFile;
inFile.open("input.txt");
//if file not found then program exits from here
if(inFile.fail())
{
cout<<"Cannot open the file input.txt";
system("pause");
exit(0);
}
//reads all words from the file and
//stores them in a multiset
string word;
while(!inFile.eof())
{
inFile>>word;
mymultiset.insert(word);
}
//counting the words in multiset
for (it=mymultiset.begin(); it!=mymultiset.end(); it++)
cout << *it<<" = "<<mymultiset.count(*it)<<endl;
//closing the file
inFile.close();
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.