Ok heres my problem i was told to make a simple tower of hanoi project (standard for C++ courses) heres what i have it works pretty well the only problem i have is that i need to number the discs from smallest to largest and tell which is moving. i only have from which tower you move the disc but my teacher wants to know which disc is moving to which tower. can anyone help with this it would be greatly appreciated.

#include<iostream>

#include<cstdlib>

#include<windows.h>



 using namespace std;





 void T( int, char, char, char);



 int main()

 {

     int n;
     


     const char A = 'A', B = 'B', C = 'C';





cout<<"\n\n\n\n The TOWER OF HANOI" << endl << endl;

     cout<<"Enter the Number of Disc: " << endl;

     cin >> n;

     cout << "The Solution to the Problem is: " << endl;

     T(n, A, B, C);

     return 0;

 }



 void T(int n, char A, char B, char C)

 {

     if(n == 1)

     {

        cout<<"\nMove Disc from tower "<< A << " to tower "<< C << endl;



    }

     else

     {

         T(n-1, A, C, B);

         cout<<"\nMove Disc from tower "<< A << " to tower "<< B<< endl;

         T(n-1, B, A, C);

     }



     return ;

 }

Recommended Answers

All 2 Replies

Lots of ways to do it. I'd say it boils down to keeping track of what disks are on what pegs. Since this involves three STACKs of disks, I suggest using three stacks to keep track of things. When you make a move, you "pop" off one stack and "push" onto another. Use the STL stack. It's tailor made for this.

http://www.cplusplus.com/reference/stl/stack/

Could you please explain what you are trying to do better?

I don't understand what it is you're trying to do. Be more specific and I'll try to help as best I can, but I didn't get the example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.