Im trying to count the frequency of words from a file and cant seem to get it to work. My frequency function recognizes the word is there but doesnt return a count for each word yet. The text file would just hold a bunch of random words...please help.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
struct mytree
{
string item;
mytree* left;
mytree* right;
int frequency;
};
class TreeofWords
{
private:
mytree *start;
public:
TreeofWords();
void inserted(string aword);
void print();
int Frequency(string freakword);
};
void main()
{
ifstream file;
string filename;
string word;
TreeofWords display;
cout<<"Enter file name.\n";
cin>>filename;
file.open(filename.c_str());
while(!file)
{
cout << "Unable to open file. Enter a different name: ";
file.clear();
cin >> filename;
file.open(filename.c_str());
}
while(file>>word)
{
cout<<display.Frequency(word);
display.inserted(word);
}
display.print();
}
TreeofWords::TreeofWords()
{
start=NULL;
}
void insert(mytree*& start, string aword)
{
if(start == NULL)
{
start = new mytree;
start->right = NULL;
start->left = NULL;
start->item = aword;
}
else if(aword < start->item)
insert(start->left, aword);
else
insert(start->right, aword);
}
void TreeofWords::inserted(string aword)
{
insert(start, aword);
}
void printme(mytree*& start)
{
if(start != NULL)
{
printme(start->left);
cout<<start->item<<endl;
printme(start->right);
}
}
void TreeofWords::print()
{
printme(start);
}
int searchFrequency(mytree *start, string freakword)
{
if(start == NULL)
{
return 0;
}
else if(start->item<freakword)
{
searchFrequency(start->left, freakword);
}
else if(start->item>freakword)
{
searchFrequency(start->right, freakword);
}
else(start->item==freakword);
return 1;
}
int TreeofWords::Frequency(string freakword)
{
return searchFrequency(start, freakword);
}