Hello, I am fairly new to programming, so any help would be much appreciated.
Basically this program gets a file with the contents like:
hello
one
one
Tt then sorts these into a binary tree, i can do that fine, however what i am struggling with is converting this to another binary tree sorted by a different property.
I'm getting a "Unhandled exception at 0x104a942c (msvcp90d.dll) : 0xC0000005: Access violation reading location 0xcdcdcde5." error in like line 1671 of xstring :|

> return (_Mysize);
Your help would be much appreciated <3 p.s. don't be too harsh on me, im only learning ;_;

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct textStats
{
	int totalWords;
	int distinctWords;
	int	uniqueWords;
};

struct text
{
	string word;
	int count;
};

struct Node
{
	text value;
	Node* pLeft;
	Node* pRight;
};

//alpha
Node* alphaAdd(Node* tree, text value);
Node* alphaAddNode(Node* tree, Node* toAdd);
Node* alphaMake (string fin, Node* tree);

// Freq
Node* freqAdd(Node* tree, text value);
Node* freqAddNode(Node* tree, Node* toAdd);
Node* freqMake(Node* tree); 

// Other
Node* find(Node* tree, string word);
void display(Node* tree);
void main()
{
	string fin;
	Node* alphaTree = 0;
	Node* freq = 0;
	text value;
	bool fileLoaded = false;
	while (!fileLoaded)
	{
		cout << "Enter the input file (including the .ptx file extension)" << endl;
		cin >> fin;
		ifstream _fin(fin.c_str());
		if (!_fin)
		{
			cout << "Error opening input file, does the file exsist?" << endl;
		}
		else
		{
			cout << "File Loaded" << endl;
			fileLoaded = true;

		}
		_fin.close();
	}
	alphaTree = alphaMake (fin, alphaTree);
	display(alphaTree);


	freq = freqMake(alphaTree);
	display(freq);
}
Node* alphaMake (string fin, Node* tree)
{
	string str;
	ifstream _fin(fin.c_str());
	Node* found;
	text value;
	value.count = 0;

	while(_fin >> str)
	{

		value.word = str;
		found = find(tree, str);
		if (found != 0)
		{
			found->value.count++;
		}
		else
		{
			value.word =str;
			value.count = 1;
			tree = alphaAdd(tree, value);

		}
	}
	return tree;

}

Node* alphaAdd(Node* tree, text value)
{
	Node* tempTree = new Node;
	tempTree->value = value;
	tempTree->pLeft = 0;
	tempTree->pRight = 0;

	return alphaAddNode(tree, tempTree);

}
Node* alphaAddNode(Node* tree, Node *toAdd)
{

	if (toAdd == 0)
	{
		return tree;
	}
	else if (tree ==0)
	{
		return toAdd;
	}

	else
	{

		if (toAdd->value.word < tree->value.word)
		{
			tree->pLeft = alphaAddNode(tree->pLeft, toAdd);
			return tree;
		}
		else
		{
			tree->pRight = alphaAddNode(tree->pRight, toAdd);
			return tree;
		}
	}
}

Node* find(Node* tree, string word)
{
	if (tree == 0)
	{
		return tree;
	}
	else if (tree->value.word == word)
	{
		return tree;
	}
	else if (word < tree->value.word)
	{
		return find(tree->pLeft, word);
	}
	else
	{
		return find(tree->pRight, word);
	}
}
void display(Node* tree)
{
	if (tree !=0)
	{
		display(tree->pLeft);
		cout << tree->value.word << "( " << tree->value.count << " )" << endl;
		display(tree->pRight);
	}
}
Node* freqMake(Node* tree)
{
	text value;
	Node* freqTree = new Node;
	if (tree != 0)
	{	
		freqMake(tree->pLeft);
		value = tree->value;
		freqTree = freqAdd(freqTree, value);
		freqMake(tree->pRight);
	}
	return freqTree;
}
Node* freqAdd(Node* tree, text value)
{
	Node* temp = new Node;
	temp->value = value;
	temp->pLeft = 0;
	temp->pRight = 0;

	return alphaAddNode(tree, temp);

}
Node* freqAddNode(Node* tree, Node *toAdd)
{

	if (toAdd == 0)
	{
		return tree;
	}
	else if (tree ==0)
	{
		return toAdd;
	}

	else
	{


		if (toAdd->value.word > tree->value.word)
		{
			tree->pLeft = freqAddNode(tree->pLeft, toAdd);
			return tree;
		}
		else
		{
			tree->pRight = freqAddNode(tree->pRight, toAdd);
			return tree;
			
		}
	
	}
}

In freqMake(...), you allocate a new Node but fail to initialize its pLeft and pRight pointers (when tree != 0 ). Later on you end up using these pointers, which gives you the error.

whenever I try to intialize freqTree->pLeft or freqTree->pRight it throws a Unhandled exception :(

When you display your tree in line 66, is there any problem there ?

nope it displays the alphaTree just fine.

Okay, I got it partially working.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct textStats
{
	int totalWords;
	int distinctWords;
	int	uniqueWords;
};

struct text
{
	string word;
	int count;
};

struct Node
{
	text value;
	Node* pLeft;
	Node* pRight;
};

//alpha
Node* alphaAdd(Node* tree, text value);
Node* alphaAddNode(Node* tree, Node* toAdd);
Node* alphaMake (string fin, Node* tree);

// Freq
Node* freqAdd(Node* tree, text value);
Node* freqAddNode(Node* tree, Node* toAdd);
Node* freqMake(Node* freqTree, Node* tree); 

// Other
Node* find(Node* tree, string word);
void display(Node* tree);
void main()
{
	string fin;
	Node* alphaTree = 0;
	Node* freq = 0;
	text value;
	bool fileLoaded = false;
	while (!fileLoaded)
	{
		cout << "Enter the input file (including the .ptx file extension)" << endl;
		cin >> fin;
		ifstream _fin(fin.c_str());
		if (!_fin)
		{
			cout << "Error opening input file, does the file exsist?" << endl;
		}
		else
		{
			cout << "File Loaded" << endl;
			fileLoaded = true;

		}
		_fin.close();
	}
	alphaTree = alphaMake (fin, alphaTree);
	display(alphaTree);

cout << endl;

	freq = freqMake(freq, alphaTree);
	display(freq);
}
Node* alphaMake (string fin, Node* tree)
{
	string str;
	ifstream _fin(fin.c_str());
	Node* found;
	text value;
	value.count = 0;

	while(_fin >> str)
	{

		value.word = str;
		found = find(tree, str);
		if (found != 0)
		{
			found->value.count++;
		}
		else
		{
			value.word =str;
			value.count = 1;
			tree = alphaAdd(tree, value);

		}
	}
	return tree;

}

Node* alphaAdd(Node* tree, text value)
{

	Node* tempTree = new Node;
	tempTree->value = value;
	tempTree->pLeft = 0;
	tempTree->pRight = 0;

	return alphaAddNode(tree, tempTree);

}
Node* alphaAddNode(Node* tree, Node *toAdd)
{

	if (toAdd == 0)
	{
		return tree;
	}
	else if (tree ==0)
	{
		return toAdd;
	}

	else
	{

		if (toAdd->value.word < tree->value.word)
		{
			tree->pLeft = alphaAddNode(tree->pLeft, toAdd);
			return tree;
		}
		else
		{
			tree->pRight = alphaAddNode(tree->pRight, toAdd);
			return tree;
		}
	}
}

Node* find(Node* tree, string word)
{
	if (tree == 0)
	{
		return tree;
	}
	else if (tree->value.word == word)
	{
		return tree;
	}
	else if (word < tree->value.word)
	{
		return find(tree->pLeft, word);
	}
	else
	{
		return find(tree->pRight, word);
	}
}
void display(Node* tree)
{
	if (tree !=0)
	{
		display(tree->pLeft);
		cout << tree->value.word << "( " << tree->value.count << " )" << endl;
		display(tree->pRight);
	}
}
Node* freqMake(Node* freqTree, Node* tree)
{
	text value;
	freqTree;

	string str;
	int intt;
	if (tree != 0)
	{	

		freqMake(freqTree, tree->pLeft);
		value = tree->value;
		freqTree = freqAdd(freqTree, value);
		freqMake(freqTree, tree->pRight);

	}
	return freqTree;
}
Node* freqAdd(Node* tree, text value)
{	
	Node* temp = new Node;
	temp->value = value;
	temp->pLeft = 0;
	temp->pRight = 0;

	return freqAddNode(tree, temp);

}
Node* freqAddNode(Node* tree, Node *toAdd)
{

	if (toAdd == 0)
	{
		return tree;
	}
	else if (tree ==0)
	{
		return toAdd;
	}

	else
	{


		if (toAdd->value.count > tree->value.count)
		{
			tree->pLeft = freqAddNode(tree->pLeft, toAdd);
			return tree;
		}
		else if (toAdd->value.count < tree->value.count)
		{
			tree->pRight = freqAddNode(tree->pRight, toAdd);
			return tree;

		}
		else {
			tree->pRight = 0;
			tree->pLeft = 0;
			return tree;
		}

	}
}

but it seems to miss off the first value of the first tree, when converting to the 2nd.

anyone got any insight? still having this problem...

Inside freqMake() you ignore its own return value twice. Then, lines 218 through 228 (the latest code) look like needing attention.

PS. void main() should be int main() .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.