tower of hanoi

i am having aproblem in my tower of hanoi game using stack please help me to remove the error
heres the code

this is my header file

<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef STACK_H
#define STACK_H


class stack
{

    private:
    int MAX, TOP;
    public:
    int *A;
    public:
    stack();
    void init();
    int isempty();
    int isfull();
    void push(int );
    int pop(int);
};

#endif // STACK_H 

>

this is my class file

<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stack.h"
#include <iostream>

using namespace std;

stack::stack()
{
    MAX=10;
    TOP =0;
    A =new int(MAX);
}
void stack::init()
{
    TOP = 0;
}
int stack::isempty()
{
    return (!TOP);
}
int stack::isfull()
{
    return (TOP==(MAX-1));
}
void stack::push(int x)
{
    A[TOP++] = x;
    cout<<x;
}
int stack::pop(int x)
{
    return A[--TOP]=x;
}

>


and this is my main

<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include "stack.h"
using namespace std;
//------------------------Globals--------------------------------------------------------
int count= 0; //Counter to display the number of moves.
//------------------------Functions------------------------------------------------------
void move( stack ,stack );
void Towers(int N, stack & s, stack & t, stack & a); //This is a recursive function.
void Display (int, stack & , stack & ,stack &);// function to display the stacks
//---------------------------------------------------------------------------------------
int main()
{


    stack source;// create 3 empty stacks
    stack target;
    stack aux;

    int N;// Number of discs.

    cout<<"\n\t **************   TOWERS OF HANOI   **************\n"<<endl;
    cout<<" WELCOME.....:" << endl;
    cout<<" Please Enter the number of discs ... you want to play with? "<<endl<<endl;

    cin>>N;//Input number of disks
    cout<<endl;
    while(N<0)//to make sure that the user didnot enter an invalid number
        {
            cout<<"The number you entered is invalid...Please reneter!"<<endl;
            cin>>N;
            cout<<endl;
        }

    for(int i=1; i<=N; i++)//Fill the source peg with the number of discs.
    {
        source.push(i);
    }


    Towers(N, source, target, aux); //to call the recursive function that will move the discs
//    Display (N, source, target,aux);


    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;

}
//-----------------------------------------------------------------------------------------------------------
void move( stack s,stack t) //This will move an element from first stack to the second stack
{
    int e; // element that will be removed from one stack and added to the other

    s.pop(e);//pop from source

    t.push(e);//and move to target

}
//----------------------------------------------------------------------------------------
void Display (int N, stack & source , stack & target ,stack & aux)
{
    stack S;// create temporarliy stack A
    stack T;// create temporarliystack B
    stack A;// create temporarliy stack C
    for(int i=1; i<=N; i++)
    {
        source.pop(i);//moves the disc from source
        S.push(i);//to the temporarily stack to dispaly it
        cout<<"Source Peg:"<<&S<<endl;

        target.pop(i);//moves the disc from source
        T.push(i);//to the temporarily stack to dispaly it
        cout<<"Target Peg:"<<&T<<endl;

        aux.pop(i);//moves the disc from source
        A.push(i);//to the temporarily stack to dispaly it
        cout<<"Auxillary Peg:"<<&A<<endl;
    }
}
//-------------------------------------------------------------------------------------------------------------
void Towers(int N, stack &s, stack &t, stack &a) //This is a recursive function.
{

    if (N>1) //a recurisve function in order to move the disks
    {
        Towers(N-1,s,a,t); //to move N-1 disks from the source to the auxillary with the help of the target peg
        move(s,t);//to move the last disk to the target peg directly
//        move(N-1,s,t,a);
        count++; //increment the number of steps before displaying
        cout<<count<<endl;
        cout<<"disk "<<N<<" is moved from "<<s<<" to "<<t<<endl;
        Towers(N-1,a,t,s); //to move N-1 disks from the auxillary to the target with the help of the source peg

//        Display ( N, source, target, aux);
    }
     else if(N==1) //if there is only one disk, move this disk from the source to the target directly
    {
        move(s,t); //moves the disk from the source to the target directly
        cout<<"You have entered one disc..."<<endl;
        cout<<"It moves direclty from the source to the target"<<endl;

//        count++; //increment the number of moves
//
//        cout<<count<<endl;
        Display (N, s, t,a);

    }

}
>


Last edited on
One thing I noticed

1
2
3
4
int stack::pop(int x)
{
    return A[--TOP]=x;
}


should look like
1
2
3
4
int stack::pop()
{
    return A[--TOP];
}


or

1
2
3
4
void stack::pop(int &x)
{
    x = A[--TOP];
}

Otherwise what is the specific problem that you are facing?
Topic archived. No new replies allowed.