title: Adding multiple nodes to a tree C++
body: <p>So I've been working on a project for school for sometime, and I've run up against a wall. My add_node function isn't working correctly, and I know why. What I'm trying to do is take in a file with multiple randomly generated letters, and create trees out of them, then make a confirmation. </p>
  
  <p>The thing is that it overwrites the same node, instead of making multiple nodes. I figured this out using Visual studios debugger, but I have no idea what to implement to fix it. What happens is that instead of having multiple nodes create a tree (like gagtttca), it makes one node and overwrites it. The node becomes g, then a, etc. How would I go about adding more nodes to the tree without overwriting it? The add_node function is the very last one.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;stack&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class myTreeNode
  {
  public:
      char Data;
      myTreeNode *childA;   //A's always go in child1
      myTreeNode *childT;   //T's always go in child2
      myTreeNode *childC;   //c's always go in child3
      myTreeNode *childG;   //G's always go in child4
  };
  
  class Tree
  {
  public:
      myTreeNode * Root;
      Tree()
      {
          Root = new myTreeNode;
          Root-&gt;Data = '-';
          Root-&gt;childA = Root-&gt;childC = Root-&gt;childG = Root-&gt;childT = NULL;
      }
      bool add_a_word(string word);
      bool is_this_word_in_the_tree(string word);
      bool add_node(myTreeNode * parent, char letter);
      bool add_words(vector&lt;string&gt; w);
  };
  
  bool get_words_from_the_file(char * my_file_name, vector&lt;string&gt; &amp;vector_of_words);
  bool get_the_reads_from_file(char * my_file_name, vector&lt;string&gt; &amp;reads);
  bool write_out_the_vector_to_screen(vector&lt;string&gt; my_vector);
  bool write_out_the_vector_to_file(vector&lt;string&gt; my_vector, char * my_file_name);
  
  ofstream out;
  
  int main()
  {
  
      out.open("my_results.txt");
  
      vector&lt;string&gt; words_in_genome;
      char * genome_file_name = "my_genome.txt";//make certain to place this file in the correct folder. Do not change path.
      if (!get_words_from_the_file(genome_file_name, words_in_genome))
          return 1;
      Tree * trees = new Tree();
      trees-&gt;add_words(words_in_genome);
  
      char * reads_file_name = "reads.txt";                 //make certain to place this file in the correct folder. Do not change path.
      if (!get_the_reads_from_file(reads_file_name, reads_to_be_tested))
          return 1;
      for (int i = 0; i &lt; reads_to_be_tested.size(); i++)
      {
          out &lt;&lt;reads_to_be_tested[i] &lt;&lt; " " &lt;&lt;    trees-&gt;is_this_word_in_the_tree(reads_to_be_tested[i]);
      }
      cout &lt;&lt; "All done" &lt;&lt; endl;
  
      //Write out a file named "myResults.txt".
      //For each read, list its sequence and either "Yes" or "No".
      //This will indicate if it does or doesn't map to the genome.
  
      /** Used for debugging
       cout &lt;&lt; "words" &lt;&lt; endl;
       write_vector_to_screen(words);
       write_vector_to_file(words,"testing.txt");
       cout &lt;&lt; "reads" &lt;&lt; endl;
       write_vector_to_screen(reads);
       ***/
      out.close();
  }
  
  
  
  bool get_words_from_the_file(char * my_file_name, vector&lt;string&gt; &amp;vector_of_words)
  {
      int i, j;
      int len = 0;
      ifstream in;
      in.open(my_file_name);
      if (!in.is_open())
      {
          cout &lt;&lt; "I could not find " &lt;&lt; my_file_name &lt;&lt; endl;
          cout &lt;&lt; "Check the location.\n";
          return false;
      }
  
      char * my_word = new char[11];
      while (in.peek() != EOF) { in &gt;&gt; my_word[0]; len++; }
      in.clear(); in.close(); in.open(my_file_name);
  
      for (i = 0; i&lt;10; i++)
      {
          in &gt;&gt; my_word[i];
          if (my_word[i]&lt;97) my_word[i] += 32;     //makes it lowercase
      }
      my_word[10] = '\0';
      vector_of_words.push_back(my_word);
  
      for (i = 1; i&lt;(len - 10 - 1); i++)   //read until the end of the file
      {
          //shift
          for (j = 0; j&lt;9; j++) my_word[j] = my_word[j + 1];
          in &gt;&gt; my_word[9];
          if (my_word[9]&lt;97) my_word[9] += 32;     //makes it lowercase
          my_word[10] = '\0';
          cout &lt;&lt; i &lt;&lt; "\t" &lt;&lt; my_word &lt;&lt; endl; cout.flush();
          vector_of_words.push_back(my_word);
      }
      in.clear(); in.close();
  
      return true;
  }
  
  
  bool get_the_reads_from_file(char * my_file_name, vector&lt;string&gt; &amp;reads)
  {
      int i;
      ifstream in;
      in.open(my_file_name);
      if (!in.is_open())
      {
          cout &lt;&lt; "The read file " &lt;&lt; my_file_name &lt;&lt; " could not be opened.\nCheck the location.\n";
          return false;
      }
  
      char * word = new char[20];                              //this is a default, we'll be looking at words of size 10
  
      while (in.peek() != EOF)
      {
          in.getline(word, 20, '\n');
          for (i = 0; i&lt;10; i++) { if (word[i]&lt;97) word[i] += 32; }     //makes it lowercase
          reads.push_back(word);
      }
      in.clear(); in.close();
      delete word;
      return true;
  }
  
  bool write_out_the_vector_to_screen(vector&lt;string&gt; my_vector)
  {
      int i;
      for (i = 0; i &lt; my_vector.size(); i++)
      {
          cout &lt;&lt; my_vector[i].c_str() &lt;&lt; endl;
      }
      return true;
  }
  
  
  bool write_out_the_vector_to_file(vector&lt;string&gt; my_vector, char * my_file_name)
  {
      ofstream out;
      out.open(my_file_name);
      int i;
      for (i = 0; i&lt;my_vector.size(); i++)
          out &lt;&lt; my_vector[i].c_str()&lt;&lt; endl;
      out.clear();
      out.close();
      return true;
  }
  
  
  bool Tree::add_words(vector&lt;string&gt; w)
  {
      for (int i = 0; i &lt; w.size(); i++)
          add_a_word(w[i]);
  
      return true;
  }
  
  
  bool Tree::add_a_word(string word)
  {
      myTreeNode * tempNode = new myTreeNode;
      tempNode = Root;
  
  
  
      if (tempNode == NULL)
      {
          cout &lt;&lt; "The tree is empty" &lt;&lt; endl;
      }
      else
      {
          while (tempNode != NULL)
          {
              for (int i = 0; i &lt; word.size(); i++)
              {
                  if (word[i] == 'a')
                  {
                      if (tempNode-&gt;childA != NULL)
                          tempNode = tempNode-&gt;childA;
                      else
                      {
                          add_node(tempNode, word[i]);//add a node: what letter, who's my parent
                          tempNode = tempNode-&gt;childA;
                      }
                  }
                  else if (word[i]== 'g')
                  {
                      if (tempNode-&gt;childG != NULL)
                          tempNode = tempNode-&gt;childG;
                      else
                      {
                          add_node(tempNode, word[i]);
                          tempNode = tempNode-&gt;childG;
                      }
                  }
                  else if (word[i] == 'c')
                  {
                      if (tempNode-&gt;childC != NULL)
                          tempNode = tempNode-&gt;childG;
                      else
                      {
                          add_node(tempNode, word[i]);
                          tempNode = tempNode-&gt;childC;
                      }
                  }
                  else if (word[i] == 't')
                  {
                      if (tempNode-&gt;childT != NULL)
                          tempNode = tempNode-&gt;childT;
                      else
                      {
                          add_node(tempNode, word[i]);
                          tempNode = tempNode-&gt;childT;
                      }
  
                  }
                  else
                  {
                      cout &lt;&lt; "The tree is full, or can't find data" &lt;&lt; endl;
                      return NULL;
                      break;
                  }
              }
  
          }
      }
  }
  
  bool Tree::is_this_word_in_the_tree(string word)
  {
      myTreeNode * tempNode = new myTreeNode;
      tempNode = Root;
  
  
      char com1, com2, com3, com4;
  
  
      if (tempNode == NULL)
      {
          cout &lt;&lt; "The tree is empty. Sorry" &lt;&lt; endl;
  
      }
      else
      {
          while (tempNode != NULL)
          {
              for (int i = 0; i &lt; word.size(); i++)
              {
                  if (word[i] == 'a')
                  {
                      if (tempNode-&gt;childA != NULL)
                      {
                          if (tempNode-&gt;childA)
                          {
                              tempNode = tempNode-&gt;childA;
                              com1 = 'y';
                          }
                      }
                      else
                      {
  
                          com1 = 'n';
                      }
                  }
  
                  if (word[i] == 'g')
                  {
                      if (tempNode-&gt;childG != NULL)
                      {
                          if (tempNode-&gt;childG)
                          {
                              tempNode = tempNode-&gt;childG;
                              com2 = 'y';
                          }
                      }
                      else
                      {
  
                          com2 = 'n';
                      }
                  }
  
  
                  if (word[i] == 't')
                  {
                      if (tempNode-&gt;childT != NULL)
                      {
                          if (tempNode-&gt;childT)
                          {
                              tempNode = tempNode-&gt;childG;
                              com3 = 'y';
                          }
                      }
                      else
                      {
  
                          com3 = 'n';
                      }
                  }
                  if (word[i] == 'c')
                  {
                      if (tempNode-&gt;childC != NULL)
                      {
                          if (tempNode-&gt;childC)
                          {
                              tempNode = tempNode-&gt;childC;
                              com4 = 'y';
                          }
                      }
                      else
                      {
  
                          com4 = 'n';
                      }
                  }
  
              }
              out &lt;&lt; com1 &lt;&lt; com2 &lt;&lt; com3 &lt;&lt; com4 &lt;&lt; endl;
              if (com1 == com2 == com3 == com4)
              {
                  out &lt;&lt; "The test passed" &lt;&lt; endl;
              }
              else
              {
                  out &lt;&lt; "The test failed" &lt;&lt; endl;
                  return false;
              }
          }
      }
      return true;
  
  }
  
  bool Tree::add_node(myTreeNode * parent, char letter)
  {
      //Can't figure out how to fix error. Run-Time error is that it overwrites the node instead of adding it.
      //How would i make it so it's a new node every time?//
      myTreeNode * tempNode = new myTreeNode;
      tempNode = Root;
      tempNode-&gt;Data = letter;
      tempNode-&gt;childA = tempNode-&gt;childC = tempNode-&gt;childG = tempNode-&gt;childT = NULL;
      if (tempNode == NULL)
      {
          cout &lt;&lt; "The tree is empty" &lt;&lt; endl;
      }
      else
      {
          while (tempNode != NULL)
          {
              if (parent-&gt;childA == NULL &amp;&amp; letter =='a')
              {
                  parent-&gt;childA = tempNode;
  
              }
              else if (parent-&gt;childC == NULL &amp;&amp; letter == 'c')
              {
                  parent-&gt;childC = tempNode;
  
              }
              else if (parent-&gt;childG == NULL &amp;&amp; letter == 'g')
              {
                  parent-&gt;childG = tempNode;
  
              }
              else if (parent-&gt;childT == NULL &amp;&amp; letter == 't')
              {
                  parent-&gt;childT = tempNode;
  
              }
              else
              {
                  cout&lt;&lt;"no"&lt;&lt;endl; //for testing//
                  return false;
                  break;
  
              }
          }
      }
  
      return true;    
  
  }
  </code></pre>
  
  <p>Like I stated before, this is a project. I'm not here looking for an easy way out. I just want learn how to fix my code.</p>