954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

hanoi puzzle help

hey i was wondering if someone could help me with this problem. It deals with the hanoi puzzle and i did the code but the output isnt correct and ii cant figure out why can someone help?
heres my code

#include<iostream> 
using namespace std; 

void towers_of_hanoi(int height, int from, int to); 

int main() 
{ 
  int num_of_pegs; 
  int peg1,peg2; 

  cout<<"How many pegs do you want to move? : "; 
  cin>> num_of_pegs; 

  cout<<" From which peg? :" <<endl; 
  cin>>peg1; 
  cout<<" To which peg? : "<<endl; 
  cin>>peg2; 

  towers_of_hanoi(num_of_pegs, peg1,peg2); 

  return 0; 
} 
void towers_of_hanoi(int height, int from, int to) 
{ 
  if(height==1) 
    cout<<"Move a disk from peg "<< from<< " to peg "<< to<<endl; 

  else 
  { 
    towers_of_hanoi(height - 1,from, to); 
    cout<<"Move a disk from peg "<< from<< " to peg "<< to<<endl; 
    towers_of_hanoi(height - 1,to,from); 
  } 
}

and the output is supposed to look like this
SAMPLE OUTPUT
Output is supposed to look like this but it doesnt. What am i doing wrong/???

how many disks do you want to move? _3__
from which peg? __3_
to which peg? __2_


move a disk from peg 3 to peg 2.
move a disk from peg 3 to peg 1.
move a disk from peg 2 to peg 1.
move a disk from peg 3 to peg 2.
move a disk from peg 1 to peg 3.
move a disk from peg 1 to peg 1.
move a disk from peg 3 to peg 2.

mina1984
Newbie Poster
16 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

>What am i doing wrong/???
You're asking a debugging question without having tried to debug. Do you think we can look at any piece of code and know what's wrong? No, we have to debug. That's about 90% of what programming is. You run the program. You run the program with print statements tossed in so that you know where you are at any given point. You run the program and print the contents of your variables at every step. You run the program with a debugger. You pray. You scream. You bash your head against a wall. But you never ever EVER post just the code and an "it's not working" message on a message board full of real programmers.

We put the same effort into helping you as you put into helping yourself. If you don't try, neither do we.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Ok, im sorry.
the problem is the output does not match the sample output the numbers are off.

this is my output
How many pegs do you want to move? _3_
from which peg? 3
to which peg? 2

move a disk from peg 3 to peg 2
move a disk from peg 3 to peg 2
move a disk from peg 2 to peg 3
move a disk from peg 3 to peg 2
move a disk from peg 2 to peg 3
move a disk from peg 3 to peg 2

And ask you can see its not right.
i hope this simplifies it alittle better. thank you

mina1984
Newbie Poster
16 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

>i hope this simplifies it alittle better.
You don't need to simplify. I know what you're problem is and I know what's causing it. However, you've done nothing to show that you've made any attempt to figure it out on your own. Therefore, it's only fair that I do nothing to show you what's wrong.

As I said, debugging is 90% of programming, and if I just give you the answer without making you go out and learn how to debug, I'm doing you a great disservice.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

In regard to your original code: Could you please explain why it is supposed to work correctly?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

For a little more help on the situation, given I only looked at your code for a few seconds, i saw one call of your function, with neither Main() or the function containig any kind of loop coding, i.e. - while(), for(;;)
...at least a step in the right direction

UrClothesRBlue
Newbie Poster
2 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Something else you didn't do for a few seconds was look at the post dates.
Last post (before you) was 16th Feb 2006

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

and...what is your point? Honestly, the one person who created this thread likely gets less help than the myriads of people who search the web for C++ help and examples. It doesn't really help those people leaving them 'high and dry' when they are looking for a solution

UrClothesRBlue
Newbie Poster
2 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Hey UrClothesRBlue I think mina1984 has left us and gone to Mars so why bother any further...

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You