title: Decoding Morse Code with strtok in binary tree - garbage output
body: <p>I was able to encode English to Morse Code but I am having trouble doing the reverse operation. Here's what I have so far:</p>
  
  <p>In the main():</p>
  
  <pre><code>cout &lt;&lt; "Enter your Morse code, separated by /, ended by *: ";
  cin.getline(morseCode, 100, '*');
  char* token = strtok(morseCode, "/");
  while(token != NULL)
  {
      cout &lt;&lt; endl &lt;&lt; "Decoding: " &lt;&lt; token &lt;&lt; endl;
      string newCode = token;
      t.Decode(newCode);
      token = strtok(NULL, "/");
  }
  </code></pre>
  
  <p>Decode function:</p>
  
  <pre><code>void Decode(string x)
  {
      Node* r = SearchAndReturnString(root, x);
      if(r-&gt;code == x) cout &lt;&lt; r-&gt;letter;
      else cout &lt;&lt; r-&gt;code &lt;&lt; " with x being " &lt;&lt; x &lt;&lt; endl; cout &lt;&lt; "Error.";
  }
  </code></pre>
  
  <p>My output is a bunch of random garbage data then the program crashes. I know it has something to do with the <code>SearchAndReturnString</code> function but I can't figure out what else to do with it.</p>
  
  <hr>
  
  <h2>Edit:</h2>
  
  <p>Node structure:</p>
  
  <pre><code>struct Node
  {
      string letter;
      string code;
      Node *left;
      Node *right;
  };
  </code></pre>
  
  <p>SearchAndReturn function:</p>
  
  <pre><code>Node* SearchAndReturnString(Node *r, string x)
  {
      if(r != NULL)
      {
          if(r-&gt;code == x) {cout &lt;&lt; r-&gt;code &lt;&lt; " matches " &lt;&lt; x &lt;&lt; endl; return r;}
          else if(r-&gt;code &gt; x) {SearchAndReturnString(r-&gt;left, x);}
          else {SearchAndReturnString(r-&gt;right, x);}
      }
      else return NULL;
  }
  </code></pre>
  
  <p>Here's the entire code as requested: </p>
  
  <p>Header: <a href="http://pastebin.com/QyaakvMK" rel="nofollow">http://pastebin.com/QyaakvMK</a></p>
  
  <p>Main: <a href="http://pastebin.com/NcseqrbX" rel="nofollow">http://pastebin.com/NcseqrbX</a></p>