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

> void main()
Has nobody mentioned that main returns in in your previous 60 messages?
Or did you just ignore them.

Where do you
- set frequency to zero?
- determine that the word is already in the tree, and thus increment the frequency

> else(start->item==freakword);
I'd be surprised if this even compiles.
You say you've run it, is this your actual code?

You have too many nodes. You want one node per UNIQUE word, not one node per word.

I would rename your class to word or node. You should have one tree and several nodes. You don't want to name something "tree" if there is one "tree" per word, in my opinion. If you have something called tree, make sure there is only one of them.

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

Nowhere do you compare the value of item to aword to see if they are equal and doing something if they are. You should be adjusting frequency as you insert, not later. New words should have a frequency of 1. Words already in the tree should have their frequencies incremented.

Also, I don't think you can use < with strings. String isn't an ordinal type. Consider using compare if you are trying to alphabetize here:

http://www.cplusplus.com/reference/string/string/compare.html

class node
{
    // data members below
     node* right;
     node* left;
     string word;
     int frequency;

     // one member function below
     insert (string aword)
     {
          // check whether word is the same as aword
          // if so, increment frequency, don't create new node.
          // if not, check whether to go right or left and if
          // child is null, create a new node.  Otherwise, 
          // recurse as you do.
     }

     // more member functions/constructor
};
class tree
{
     // data member
     node* top;

     // constructor 
     tree ()
     {
          top = NULL;
     }

     void insert (string aword)
     {
           if (top == NULL)
                // create new top node with call to node constructor
           else
                top->insert (aword);
     }
};

yes this does run, and setting frequency to zero and determining if a word is already in the tree then incrementing it, is what I need help on.

yes this does run, and setting frequency to zero and determining if a word is already in the tree then incrementing it, is what I need help on.

It ran for me too and it alphabetized, which surprised me. I didn't think the < operator would work. Learn something new every day.

The naming of the struct mytree threw me. You can keep the single class if you like and call it TreeOfWords as you have it. There's no need to have a node class with an insert method. You can keep the insert method in TreeOfWords like you have it. Keeping node as a struct with no methods will work. But rename it.

I'd get rid of the frequency methods. Handle adjusting frequency in insert. Again, don't create a new word if the word already exists in the tree. Just increment frequency in that case.

edit: I had to change void main to int main to get it to run. Salem is right. Change it even if your compiler lets you get away with it.

Just a sample..Easy way!!..

// member of TreeofWords
static int size=0;
void TreeofWords::setsize()
{
   size++;
}
int TreeofWords::getsize()
{return size;}

void main()
{
   // ...
   while(file>>word)
   {
      display.setsize();
      // ...
   }
   std::cout<<"freak word count : "<<display.getsize();
   // ...
}// done
This question has already been answered. Start a new discussion instead.