title: Tower of Hanoi C++(using recursion)
body: <p>I wrote the following code as a practice exercise.<br>
  I'm getting incorrect output when I print the destination stack.<br>
  Can anyone please point out where I'm going wrong ?</p>
  
  <pre><code>//Tower of Hanoi using Stacks!
  #include&lt;iostream.h&gt;
  #include&lt;conio.h&gt;
  #include&lt;stdlib.h&gt;
  
  class Stack
  {
  private:
      int *t;
      int length, top;
  
  public:
      Stack(int len)
      {
          length=len;
          t= new int[len];
          top=-1;
      }
  
      ~Stack()
      {
          delete []t;
      }
  
      void push(int d)
      {
          top++;
          t[top]=d;
      }
  
      int pop()
      {
          top--;
          return t[top+1];
      }
  
      void printstack()
      {
          int cur=top;
          while(cur&gt;-1)
          {
              cout&lt;&lt;t[cur]&lt;&lt;endl;
              cur--;
          }
      }
  };
  
  void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
  {
      if (disk==0)
      {
          destination-&gt;push(source-&gt;pop());
      }
      else
      {
          MoveTowerofHanoi(disk-1,source,temp,destination);
          destination-&gt;push(source-&gt;pop());
          MoveTowerofHanoi(disk-1,temp,destination,source);
      }
  }
  
  void main()
  {
      clrscr();
      int disks;
      cout&lt;&lt;"Enter the number of disks!"&lt;&lt;endl;
      cin&gt;&gt;disks;
      Stack* source=new Stack(disks);
      for(int i=0; i&lt;disks; i++) {
          source-&gt;push(disks-i);
      }
      cout&lt;&lt;"Printing Source!"&lt;&lt;endl;
      source-&gt;printstack();
      Stack* temp=new Stack(disks);
      Stack* destination=new Stack(disks);
      MoveTowerofHanoi(disks,source,temp,destination);
      cout&lt;&lt;"Printing Destination!"&lt;&lt;endl;
      destination-&gt;printstack();
      getch();
  }
  </code></pre>
  
  <p>Here's the output I'm getting:  </p>
  
  <pre><code>Enter the no. of disks!  
  3  
  Printing Source!  
  1  
  2  
  3  
  Printing Destination!  
  -4
  </code></pre>
  
  <hr>
  
  <p>After editing, the code looks like this:  </p>
  
  <pre><code>    void MoveTowerofHanoi(int disk, Stack *source, Stack *destination, Stack *temp)
  {
      if (disk==1)
      {
          destination-&gt;push(source-&gt;pop());
      }
      else
      {
          MoveTowerofHanoi(disk-1,source,temp,destination);
          destination-&gt;push(source-&gt;pop());
          MoveTowerofHanoi(disk-1,temp,destination,source);
      }
  }
  </code></pre>
  
  <p>the first mistake was:   </p>
  
  <pre><code>void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
  </code></pre>
  
  <p>the second was:</p>
  
  <pre><code>if (disk==0)
  </code></pre>
  
  <p>Many thanks to all for helping!</p>
  
  <hr>
  
  <p>Changes made to stack class:</p>
  
  <pre><code>void push(int d)
  {
       if(top&lt;length-1)
      {
      top++;
      t[top]=d;
      }
  
  }
  
  int pop()
  {
      if(top&gt;-1)
      {
      top--;
      return t[top+1];
      }
  }
  </code></pre>