From: Hani Sharabash on
I'm writing a program in which I read text from a file, store all of
the words into a binary search tree, and then print the words out to
the screen in order from that tree.

For some reason, opening and using an input filestream is causing
problems for my binary tree functions. When I try to use them
together, my program crashes at runtime. For example:

This works just fine.

#include <iostream>
#include <string>
#include <fstream>
#include "binarytree.h"
#include "treenode.h"
#include "datanode.h"

using namespace std;

int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;

currentWord = "abcd";
Tree.insert(currentWord);

Tree.walk();

system("PAUSE");
return 0;
}



However, this does not work.

#include <iostream>
#include <string>
#include <fstream>
#include "binarytree.h"
#include "treenode.h"
#include "datanode.h"

using namespace std;

int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;

readIn.open("file.txt");
readIn >> currentWord;

Tree.insert(currentWord);

Tree.walk();


system("PAUSE");
return 0;
}


It seems that the input stream works if there is no tree inserting
going on, and the tree insertion seems to work when there is no input
stream. Now heres the really strange thing. They DO work together in
this case:

int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;

string currentWord;
readIn.open("file.txt");
readIn >> currentWord;

string y = "abc";
Tree.insert(y);

Tree.walk();


system("PAUSE");
return 0;
}



but for some reason NOT in this case!

int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;

string currentWord;
readIn.open("file.txt");
readIn >> currentWord;

string y = "abcd"; // The only difference is that I'm inserting a
string four letters or more!
Tree.insert(y);

Tree.walk();


system("PAUSE");
return 0;
}


I've tested it thoroughly, for some reason, it only crashes when the
filestream is open and the string I'm inserting is four letters or
more, or is the word from the file (even if its less than four)! What
could possibly be going on?

I can post the code from all the other files if anyone wants to see
it. Also, I apologize in advance if this turns out to be a stupid or
simple question, I'm sort of a novice programmer.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Michael.Boehnisch on
On 16 Apr., 19:38, Hani Sharabash <HaniB...(a)gmail.com> wrote:
> For some reason, opening and using an input filestream is causing
> problems for my binary tree functions. When I try to use them
> together, my program crashes at runtime.

"Crashes at runtime" is a very vague description. Use a symbolic
debugger to track down what your program does right before it crashes.
On Unix/gcc compile with option -g to enable debugging and use e.g.
ddd (very powerful tool!) to step through your code until the crash
happens. On Windows / Visual Studio, the debugger is integrated in the
IDE.

Either option should make it easy to locate your problem yourself. If
you never used a debugger before, appreciate the opportunity to learn
- its worth the effort.

best,

Michael

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Hani Sharabash wrote:
> #include "binarytree.h"
> #include "treenode.h"
> #include "datanode.h"

I read your code and I'm pretty sure the problem is in these files
somewhere. Please provide a minimal but complete example, i.e. one that
doesn't contain any unnecessary stuff but still contains everything that
one needs to reproduce the problem.

Uli

--
Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Carl Barron on
In article
<73c37290-e418-4d12-934a-c330752108c0(a)m36g2000hse.googlegroups.com>,
Hani Sharabash <HaniBash(a)gmail.com> wrote:

> I'm writing a program in which I read text from a file, store all of
> the words into a binary search tree, and then print the words out to
> the screen in order from that tree.
>
> For some reason, opening and using an input filestream is causing
> problems for my binary tree functions. When I try to use them
> together, my program crashes at runtime. For example:
> \
first I would make sure where the error is in this case I would
imput the data and store it in a stanard sorted assoc. container
say std::set<std::string> and print the results from
std::set<std::string> before I blame <fstream> probably there is
a bug in your binary tree code. But if reading it into an
std::set<string> works then its not <fstream>.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Hani Sharabash on
On Apr 17, 3:59 am, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote:
> Hani Sharabash wrote:
> > #include "binarytree.h"
> > #include "treenode.h"
> > #include "datanode.h"
>
> I read your code and I'm pretty sure the problem is in these files
> somewhere. Please provide a minimal but complete example, i.e. one that
> doesn't contain any unnecessary stuff but still contains everything that
> one needs to reproduce the problem.
>
> Uli
>
> --
> Sator Laser GmbH
> Gesch�ftsf�hrer: Michael W�hrmann, Amtsgericht Hamburg HR B62 932
>
> [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
> [ comp.lang.c++.moderated. First time posters: Do this! ]

Here are the contents of binarytree.cpp.

#include "binarytree.h"


BinaryTree::BinaryTree()
{
root = NULL;
}

void BinaryTree::insert(string s)
{
Treenode* newnode = new Treenode(s);
root = recursiveInsert(root, newnode);
}

Treenode* BinaryTree::recursiveInsert(Treenode* subroot, Treenode*
newnode)
{
if (subroot == NULL)
return newnode;
else if (newnode->getWord() < subroot->getWord())
subroot->left = recursiveInsert(subroot->left, newnode);
else if (newnode->getWord() > subroot->getWord())
subroot->right = recursiveInsert(subroot->right, newnode);
else
subroot->increaseWordCount();
return subroot;
}

void BinaryTree::walk()
{
if (root == NULL)
cout << "Empty Tree";
else
recursiveWalk(root);

cout << endl;
}


void BinaryTree::recursiveWalk(Treenode* subroot)
{
if (subroot == NULL)
return;
recursiveWalk(subroot->left);
cout << subroot->getWord() << " ";
recursiveWalk(subroot->right);

}

binarytree.h:
#ifndef BINARYTREE_H
#define BINARYTREE_H

#include "treenode.h"

using namespace std;

class Treenode;

class BinaryTree
{

public:
BinaryTree();

void insert(string s);
Treenode* recursiveInsert(Treenode* subroot, Treenode* newnode);

void walk();
void recursiveWalk(Treenode* subroot);


private:
Treenode* root;



};

#endif



treenode.cpp:

#include "treenode.h"


Treenode::Treenode(string s)
{
data->word = s;
data->wordCount = 1;
left = right = NULL;
}

string Treenode::getWord()
{
return data->word;
}

int Treenode::getWordCount()
{
return data->wordCount;
}

void Treenode::increaseWordCount()
{
data->wordCount++;
}


treenode.h:

#ifndef TREENODE_H
#define TREENODE_H

#include <iostream>

#include "binarytree.h"
#include "datanode.h"

using namespace std;

class Treenode
{
friend class BinaryTree;

public:
Treenode(string s);

string getWord();
int getWordCount();
void increaseWordCount();

private:
Datanode* data;
Treenode *left, *right;
};

#endif

datanode.h (there is no datanode.cpp file):

#ifndef DATANODE_H
#define DATANODE_H


using namespace std;

class Datanode
{
friend class Treenode;

private:
string word;
int wordCount;
};

#endif

Thank you so much for your help.


On Apr 17, 3:56 am, Carl Barron <cbarron...(a)adelphia.net> wrote:
> In article
> <73c37290-e418-4d12-934a-c33075210...(a)m36g2000hse.googlegroups.com>,
>
> Hani Sharabash <HaniB...(a)gmail.com> wrote:
> > I'm writing a program in which I read text from a file, store all of
> > the words into a binary search tree, and then print the words out to
> > the screen in order from that tree.
>
> > For some reason, opening and using an input filestream is causing
> > problems for my binary tree functions. When I try to use them
> > together, my program crashes at runtime. For example:
> > \
>
> first I would make sure where the error is in this case I would
> imput the data and store it in a stanard sorted assoc. container
> say std::set<std::string> and print the results from
> std::set<std::string> before I blame <fstream> probably there is
> a bug in your binary tree code. But if reading it into an
> std::set<string> works then its not <fstream>.
>

I don't understand. Could you clarify a little more?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]