Strcpy causing crash

Hi there. I'm just learning about binary trees so I apologize for the lack of comments. Basically, this program is supposed to be a sort of A.I. learning program that learns how to guess an animal you might think of. The problem I'm running into is on line 71 and onward.

Something about how I've written the strcpy() is making the compiler upset. But not upset enough to spit out an error message. Any idea on what might be wrong about it? Thanks!

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
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <cstring>

struct animal{
  char text[100];
  animal* yes;
  animal* no;
};

void deallocateTree(animal* root){
  if (!root)
    return;
  deallocateTree(root->yes);
  deallocateTree(root->no);
  delete root;
}

int main()
{
  animal* root= new animal;
  strcpy(root->text, "elephant");
  root->yes=NULL;
  root->no=NULL;

  char a[100];
  char q[100];
  char yesNo;

  while(true){
    cout << "Think of an animal. Enter Y to continue or N to exit." << endl;
    char playResponse;
    cin >> playResponse;
    if (toupper(playResponse)=='N')
      break;

    animal* p=root;
    while (true){
      if (p->yes==NULL){// if p->yes is 0
        for (int i=0; p->text[i]!='\0'; i++)
          cout << p->text[i];

        cout << "\nIs this the animal you were thinking of? (y/n)" << endl;
        char YNResponse;
        cin >> YNResponse;
        cin.ignore();

        if (toupper(YNResponse)=='Y'){
          cout << "Got it!" << endl;
          break;
        }

        cout << "What animal was it?" << endl;
        cin.getline(a, sizeof(a));

        cout << "What Yes or No question differentiates ";
        for (int i=0; p->text[i]!='\0';i++){ cout << p->text[i];}
        cout << " from ";
        for (int i=0; a[i]!='\0';i++){cout << a[i];}
        cout << "?" << endl;
        cin.getline(q, sizeof(q));//...store in "char q[100]"

        cout << "Is the answer Yes or No? (y/n)" << endl;
        cin >> yesNo;

        animal* y=NULL;
        animal* n=NULL;

        if (toupper(yesNo)=='Y'){/*problem starts here*/
          strcpy(y->text, a);// copy "a" into y->text
          strcpy(n->text, p->text);// copy p->text into n->text

        }
        else if(toupper(yesNo)=='N'){/*problem also here*/
          strcpy(n->text, a);// copy "a" into n->text
          strcpy(y->text, p->text);// copy p->text into y->text
        }
        else{
          cout << "Not a valid answer. Starting over." << endl;
          break;
        }
        strcpy(p->text, q);// copy "q" into p->text
        y->yes=NULL;
        n->yes=NULL;
        y->no=NULL;
        n->no=NULL;

        p->yes=y;
        p->no=n;
        break;
      }
      else if (!p->yes){
        cout << p->text << " ?" << endl;
        cout << "Answer y/n" << endl;
        cin >> yesNo;

        if (toupper(yesNo)=='Y')
          p=p->yes;
        else if (toupper(yesNo)=='N')
          p=p->no;
      }
    }
  }
  deallocateTree(root);
}
Instead of setting the objects to NULL (line 68/69) you should allocate the appropriate memory (like lines 23 to 26).

I recommend to use constructors/destructor to initialize the members of the struct.
Gotcha. Thanks.
New problem, after one cycle of the first while loop, I get stuck in an infinite loop. I'm really not sure what could cause that to happen. My guess is that somewhere, one of my conditions is screwed up. Any thoughts? Thanks!
Show your updated code.
It's all the same except for the initialization you mentioned

1
2
3
4
animal* y=new animal;// create two new nodes, names "y" and "n"
  strcpy(y->text,"");
animal* n=new animal;
  strcpy(n->text, "");
The problem is line 94: if (!p->yes) is the same as if (p->yes==NULL) and hence the else branch will not be processed. The result is an infinite loop.

I suggest that you use the debugger. You learn the most when you go through the code step by step
Thank you! I'll definitely take some time to practice learning how to debug.
closed account (E0p9LyTq)
If you are using Visual Studio 2015, here's a very simplified guide to the debugger:

https://blogs.msdn.microsoft.com/visualstudio/2015/08/11/introduction-to-debugging/
Topic archived. No new replies allowed.