Binary Tree pointers printing out memory address?
Apr 16, 2011 at 9:16pm UTC
I am having trouble with a program that I am writing. It is supposed to play the 20 questions game, and save the data in a binary tree. It also learns if it is wrong. I have to read the data in from a file to the tree when the program runs and save it back when it exits. The problem I am having, is that when it prints out the contents of any part of the tree, it is printing out what looks like a memory address, or a random spot in memory as if the pointer has been set to a random memory address. Any help?
tree.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*
* tree.cpp
* Program 4
*
* Created by Sam on 4/6/11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#include "tree.h"
Tree::Tree()
{
root = new Node;
loc = root;
};
void Tree::play()
{
while (!loc->isLeaf)
{
cout << loc->content << "(y/n)" << endl;
string ans;
cin >> ans;
while (ans != "y" && ans != "Y" && ans != "n" && ans != "N" )
{
cout << "Please choose 'y' or 'n'" << endl;
cin >> ans;
}
if (ans == "y" || ans == "Y" )
loc = loc->yes;
else
loc = loc->no;
}
cout << "Is your animal a " << loc->content << "?" << endl;
cout << "Correct? (y/n)" << endl;
char ans1;
cin >> ans1;
while (ans1 != 'y' && ans1 != 'Y' && ans1 != 'n' && ans1 != 'N' )
{
cout << "Please choose 'y' or 'n'" << endl;
cin >> ans1;
}
if (ans1 != 'y' || ans1 != 'Y' )
insert();
};
void Tree::resetLoc()
{
loc = root;
};
void Tree::saveTree()
{
ofstream save;
save.open("tree.dat" , ios::app);
//loc = root; --put this right beore this function is called
save << loc->isLeaf << loc->content << endl;
if (!loc->isLeaf)
{
parent = loc;
loc = loc->yes;
saveTree();
loc = parent;
loc = loc->no;
saveTree();
}
save.close();
}
void Tree::fillTree()
{
string temp;
ifstream read;
read.open("tree.dat" );
getline(read, temp);
if (temp[1]=='0' )
loc->isLeaf = false ;
else
loc->isLeaf = true ;
loc->content=temp.substr(1);
if (!loc->isLeaf)
{
loc->yes = new Node;
parent = loc;
loc = loc->yes;
fillTree();
loc = parent;
loc = loc->no;
fillTree();
loc = parent;
}
read.close();
};
void Tree::insert()
{
cout << "What were you thinking of?" << endl;
string newName;
cin >> newName;
cout << "What would distinguish a " << newName;
cout << "from a " << loc->content << "?" << endl;
string newQuestion;
cin >> newQuestion;
cout << "What is the answer to that question for a " << newName <<"? (y/n)" << endl;
string answer;
cin >> answer;
while (answer != "y" && answer != "Y" && answer != "n" && answer != "N" )
{
cout << "Please choose 'y' or 'n'" << endl;
cin >> answer;
}
if (answer == "y" || answer == "Y" )
{
loc->yes = new Node;
loc->yes->content = newName;
loc->yes->isLeaf = true ;
loc->no = new Node;
loc->no->content = loc->content;
loc->no->isLeaf = true ;
}
else
{
loc->no = new Node;
loc->no->content = newName;
loc->no->isLeaf = true ;
loc->yes = new Node;
loc->yes->content = loc->content;
loc->yes->isLeaf = true ;
}
loc->isLeaf = false ;
loc->content = newQuestion;
};
tree.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
* tree.h
* Program 4
*
* Created by Sam on 4/6/11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Tree
{
public :
void play();
void saveTree();
void insert();
void fillTree();
void resetLoc();
Tree();
private :
//_______Tree data and nodes_________
class Node
{
public :
string content;
Node *no;
Node *yes;
bool isLeaf;
Node(): no(0), yes(0)
{}
Node(string item): content(item), no(0), yes(0)
{}
};
typedef Node *nodePtr;
nodePtr root, parent, loc;
//___________________________________
};
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include "tree.h"
int main ()
{
Tree go;
char goAgain;
cout << "loading, please wait..." << endl;
go.fillTree();
go.resetLoc();
cout << "Welcome to Guess The Animal!" << endl;
cout << "You think of an animal, and I will try to guess it!" << endl;
do
{
go.play(); // manipulate location from within function before calling again.
cout << "Play again? (y/n)" << endl;
cin >> goAgain;
while (goAgain != 'y' || goAgain != 'Y' || goAgain != 'n' || goAgain != 'N' )
{
cout << "Please choose 'y' or 'n'" << endl;
cin >> goAgain;
}
} while (goAgain == 'y' || goAgain == 'Y' );
go.saveTree();
return 0;
}
any help is appreciated!
Topic archived. No new replies allowed.