Program Failing when trying to assign value to structure pointer

I'm creating a binary tree to guess an animal that a user is thinking of. The last problem I seem to have before the program is finished is involved with my traversal function. The function is intended to traverse the tree of questions with yes/no responses. Once reaching a leaf node, the program will ask the user if that is their animal. If it is, the function concludes. If not, the function will ask which animal the user has chosen, and for a question to delineate between the two animals.

The program fails when reaching its last lines where the new animal and question are assigned to new nodes added to the tree. I've tried several methods for trying to assign this process differently, but they all seem to incorporate some sort of runtime error that I have not been able to identify. The header file, testDriver file, and infile are included below. I've indicated the traverse function as well as the location of where the runtime error is occurring with copious /'s. Advice would be immensely appreciated.

HEADER----------------------------------
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef BINTREE_H
#define BINTREE_H

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

class TaxonomyTree 
{
    public:
        struct TreeNode {
            string info;          // The question at the node
            TreeNode *yes;    // Pointer to left child node
            TreeNode *no;   // Pointer to right child node
        };

        TreeNode* taxonomy_root_ptr;
    
        TaxonomyTree() {
            taxonomy_root_ptr = nullptr;
        }

        bool isLeaf(TreeNode* node) {
            if ((node->yes == nullptr || node->yes->info == "@ ") && (node->no == nullptr|| node->no->info == "@ ")) {
                return true;
            }
            else {
                return false;
            }
        }

        
        void swap(TreeNode* &x, TreeNode* &y) {
            TreeNode* temp = x;
            x = y;
            y = temp;
        }

        TreeNode* addNode(string info) {
            TreeNode* newNode = new TreeNode;
            newNode->info = info;
            newNode->yes = nullptr;
            newNode->no = nullptr;
            add(taxonomy_root_ptr, newNode);
            return newNode;
        };

        void add(TreeNode* &node, TreeNode* &newNode) {
            if (node == nullptr) {
                node = newNode;
            }
        }

        void traverseTree() {
            traverseTree(taxonomy_root_ptr);
        }

        void traverseTree(TreeNode* &node) { //////////////////////////
            TreeNode* locPtr = node;
            string response;

            string newInfo1;
            string newInfo2;

            TreeNode* newEntry1;
            TreeNode* newEntry2;

            while(!(isLeaf(locPtr))) {
                cout << locPtr->info << endl;
                cin >> response;
                cout << endl;

                if (response == "y") {
                    locPtr = locPtr->yes;
                }
                else if (response == "n") {
                    locPtr = locPtr->no;
                }
                else {
                    cout << "\nPlease enter one of the offered responses.\n";
                }
            }
            if (isLeaf(locPtr) == true) {
                cout << "My guess is " << locPtr->info << ". Am I right? Please answer [y or n]\n";
                cin >> response;
                cin.ignore();
                cout << endl;

                while (response != "y" && response != "n") {
                    cout << "\nPlease enter one of the offered responses.\n";
                    cout << "My guess is " << locPtr->info << ". Am I right? Please answer [y or n]\n";
                    cin >> response;

                }
                if (response == "y") {
                    cout << "\nI knew it! What a great animal! One of my favorites!\n\n";

                }
                else {
                    cout << "I give up. What are you?\n";
                    getline(cin, newInfo1);
                    cout << "\nPlease type a yes/no question that will distinguish a " << locPtr->info << " from a " << newInfo1;
                    cout << ".\nYour question: (ending with ?)\n"; 
                    getline(cin, newInfo2);

                    cout << "\nAs a " << newInfo1 << ", " << newInfo2 << " Please answer [y or n]\n";
                    cin >> response;

                    while (response != "y" && response != "n") {
                    cout << "\nPlease enter one of the offered responses.\n";
                    cout << "As a " << newInfo1 << ", " << newInfo2 << " Please answer [y or n]\n";
                    cin >> response;
                    }

                    newInfo2 += " Please answer [y or n]";


                    swap(newEntry2, locPtr);
                    if (response == "y") {
                        //locPtr = locPtr->yes;
                        newEntry2->yes = newEntry1;
                        newEntry2->no = locPtr;
                    }
                    else {
                        //locPtr = locPtr->no;
                        newEntry2->yes = locPtr;
                        newEntry2->no = newEntry1;
                    }

                    newEntry1->info = newInfo1;//////////////////// RUNTIME ERROR/PROGRAM CRASHING ///////////////////////////////////////////////
                    newEntry2->info = newInfo2;//////////////////// RUNTIME ERROR/PROGRAM CRASHING ///////////////////////////////////////////////

                }

            }

        };


        void readIn(TreeNode* &node, ifstream &infile) {
            string entry;

            if (!(getline(infile, entry)) || entry == "@ ") {
                return;
            }
            node = addNode(entry);
            readIn(node->yes, infile);
            readIn(node->no, infile);
       };

        void saveTo(TreeNode* node, ofstream &outfile) {
            //symbol for distinguishing leaf
            //complete tree with un-populated leaves
            //if leaf node, node IS an answer
        
            if (node == nullptr) {
                outfile << "@ " << endl;
            }
            else {
                outfile << node->info << endl;
                saveTo(node->yes, outfile);
                saveTo(node->no, outfile);
            }

        };

        void deleteNode() {

        };

        void printIt() {
            printTree(taxonomy_root_ptr, 1);
        }

        void printTree(TreeNode* node, int treeLevel) {
            if (node != nullptr) {
                printTree(node->no, treeLevel + 1);
                cout << endl;

                if (node == taxonomy_root_ptr) {
                    cout << "ROOT->";
                }
                for (int i = 0; i < treeLevel && node != taxonomy_root_ptr; i++) {
                    cout << "          ";
                }
                
                cout << (node->info);

                printTree(node->yes, treeLevel + 1);
            }
        };


};



#endif 


TESTDRIVER-----------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "binTree.h"

using namespace std;

int main() {
    TaxonomyTree animalTree;


    ifstream infile;
    infile.open("animalTreeInfile.txt");
    animalTree.readIn(animalTree.taxonomy_root_ptr, infile);
    infile.close();

    animalTree.traverseTree();




    return 0;
}



infile text--------------------------
Are you a mammal? Please answer [y or n]
Are you bigger than a cat? Please answer [y or n]
Are you a marsupial? Please answer [y or n]
Kangaroo
@
@
Are you a primate? Please answer [y or n]
Monkey
@
@
Do you have a really long neck? Please answer [y or n]
Giraffe
@
@
Raccoon
@
@
Mouse
@
@
Do you live underwater? Please answer [y or n]
Trout
@
@
Robin
@
@
Last edited on
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

You knew how to use code tags with your first post back in January. What happened between then and now?

FYI, there are additional tags available you can use, like the "Program output" tags.
Was in a rush to post and forgot, my apologies.
Lines 68, 69, pointers are not initialized by the time they are used on line 133.
would lines 133/134 not serve as their initialization? since that is the first time it is assigned a value for the string of the structure.
Lines 133 and 134 are dereferencing newEntry1 and newEntry2, respectively, then assigning to the 'info'.

newEntry1->info is the same as (*newEntry1).info. The problem is that, (*newEntry1) is junk [and illegal], because newEntry1 doesn't point to anything valid.
Last edited on
Not tried, but don't you want to remove L68,69 and after L118 insert:

1
2
TreeNode* newEntry1 {new TreeNode};
TreeNode* newEntry2 {new TreeNode};


Note that the memory allocated isn't freed at the end as TaxonomyTree doesn't have a destructor that deletes the allocated memory.


yes that solved it; the deletion of L68,69 and the insertion of the following after L118 relieved my issue. Thank you.

1
2
TreeNode* newEntry1 = {new TreeNode};
TreeNode* newEntry2 = {new TreeNode};

Topic archived. No new replies allowed.