include<iostream>
class Hanoi {
private:
int n;// no. of disks
public:
Hanoi(int);
solve(char, char, char int);
};
void Hanoi :: Hanoi(int a) {
cout << "Enter number of disks : " << endl;
cin >> a;
n = a;
}
void Hanoi :: tower(char from, char use, char to) {
if (n > 0) {
tower(n-1, from, use, to);
cout << "Move disk " << n << " from " << from << " to " << to);
tower(n-1, use, to, from);
}
}
int main(void)
{ Hanoi h = new Hanoi();
h.tower('A','B','C');
}
views:
107answers:
4you need
using namespace std;
... and to read again the C++ specifications .... and you may get something like this:
#include<iostream>
using namespace std;
class Hanoi {
public:
Hanoi();
void solve(int, char, char, char);
};
Hanoi :: Hanoi() {
}
void Hanoi :: solve(int n, char from, char use, char to) {
if (n > 0) {
solve(n-1, from, use, to);
cout << "Move disk " << n << " from " << from << " to " << to << endl;
solve(n-1, use, to, from);
}
}
int main(void) {
Hanoi h;
int N;
cout << "Enter number of disks : " << endl;
cin >> N;
h.solve(N,'A','B','C');
}
Constructors dont have a return type. Hanoi
is your class and the following a constructor it need not have a void return type
void Hanoi :: Hanoi(int a)
should be
Hanoi :: Hanoi(int a)
No guys it's not that. I'm not good at programming. I had a bad background with it. I am getting tutored on C++ from scratch. The homework s due tomorrow at 8:30 in the morning. It's just a little late and I needed this help. I tried it on my own and ended up with 6 errors.
Why is everyone coming down on me like that? I needed help. I did not know this would be such a big idea. It is not like I can't go back and understand where I want wrong when my professor goes over it in class tomorrow. Everyone relax. I apologize for asking a question on this website, or shall I say requesting an answer. I thought this is what the website was for. Someone referred me to this website.