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.

Recommended Answers

All 8 Replies

>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.

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

>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.

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

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

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

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

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

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.