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 ;

}``````
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by spoonlicker

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.

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.