need solution on count word in C++

need help .. below is my code ...!!



#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <sstream>
#include <iterator>
using namespace std;


size_t count_words(const std::string& s)
{
std::istringstream is(s);
return distance( std::istream_iterator<std::string>(is),
std::istream_iterator<std::string>());
}

void main()
{
string read;
int threshold;
map<string, int> mymap;

ifstream myfile("test.txt"); //open text file
ofstream myoutput1;
ofstream myoutput2;

cout << "Please enter threshold :";
cin >>threshold;

if(!myfile)
{
cout << "Cannot open input file.\n";
exit(0);
}
else
{
while (myfile >> read)
{
++mymap[read]; //Read in the words in text file and count the freq of the word appearing
}
}
myoutput1.open("index.txt");
myoutput2.open("common.txt");
map<string, int>::iterator b = mymap.begin();
map<string, int>::iterator e = mymap.end();

//ofstream myoutput;
//myoutput.open ("example.txt");

std::string s; // word count
unsigned int wordno = 0;

while(getline(myfile, s))
{
++wordno;
if(s.empty())++ wordno;
else wordno += count_words(s);
std::cout << s << '\n';
}

myoutput1<< "Number of words : " << wordno << '\n';
myoutput1 << "\nWord" <<" " <<" Occurrence" <<endl;
myoutput2<< "Number of words : " << wordno << '\n';
myoutput2 << "\nWord" <<" " <<" Occurrence" <<endl;

while ( b != mymap.end() )
{

if (b->second >= threshold)
{
myoutput2 << b->first << "\t\t" << b->second << endl;
}
else
{
myoutput1 << b->first << "\t\t" << b->second << endl;
}
b++;
}

myfile.close();
myoutput1.close();
myoutput2.close();
}

closed account (EzwRko23)
Search the forum. It was discussed many times.
but this one i need to read from external source~
i need help on this area.

size_t count_words(const std::string& s)
{
std::istringstream is(s);
return distance( std::istream_iterator<std::string>(is),
std::istream_iterator<std::string>());
}


......


std::string s; // word count
unsigned int wordno = 0;

while(getline(myfile, s))
{
++wordno;
if(s.empty())++ wordno;
else wordno += count_words(s);
std::cout << s << '\n';
}

myoutput1<< "Number of words : " << wordno << '\n';
myoutput1 << "\nWord" <<" " <<" Occurrence" <<endl;
myoutput2<< "Number of words : " << wordno << '\n';
myoutput2 << "\nWord" <<" " <<" Occurrence" <<endl;
Topic archived. No new replies allowed.