# Towers of Hanoi is making my head explode and also overflowing badly

• 09-28-2012
rwagstaff
Towers of Hanoi is making my head explode and also overflowing badly
I am so confused. Input # of disks, start peg and end peg. My program is failing miserably yet it is pretty much the exact code that supposedly works. The function keeps subtracting one until it goes haywire. Please see the program and output below. Any help would be appreciated. Thank you in advance.
Code:

```#include <stdio.h> #include <stdlib.h> // Function Prototypes void SolveTower(unsigned d, unsigned from, unsigned to, unsigned aux); // Main Program int main() {     unsigned disks, pegstart, pegend;     printf("How many disks do you want to move?");     scanf_s("%u", &disks);     printf("    From which peg? [1 - 3] --> ");     scanf_s("%u", &pegstart);     printf("      To which peg? [1 - 3] --> ");     scanf_s("%u", &pegend);     printf("\n======= The Tower of Hanoi Solution ========\n");     SolveTower(disks, pegstart, pegend, 6 - pegstart - pegend);     printf("\n============================================\n"); } // Function Definitions void SolveTower(unsigned d, unsigned from, unsigned to, unsigned aux) {     if (d == 1)         printf("\nMove a disk from peg %u to peg %u.", from, to);     else         printf("\nDisks=%u; From=%u; To=%u; Aux=%u\n", d, from, to, aux); system("pause");  // Debugging code only         SolveTower(d - 1, from, aux, to);         printf("\n(ELSE MOVE) Move a disk from peg %u to peg %u.", from, to);         SolveTower(d - 1, aux, to, from); }```
Output:
How many disks do you want to move?4
From which peg? [1 - 3] --> 1
To which peg? [1 - 3] --> 3

======= The Tower of Hanoi Solution ========

Disks=4; From=1; To=3; Aux=2
Press any key to continue . . .

Disks=3; From=1; To=2; Aux=3
Press any key to continue . . .

Disks=2; From=1; To=3; Aux=2
Press any key to continue . . .

Move a disk from peg 1 to peg 2.Press any key to continue . . .

Disks=0; From=1; To=3; Aux=2
Press any key to continue . . .

Disks=4294967295; From=1; To=2; Aux=3
Press any key to continue . . .

Disks=4294967294; From=1; To=3; Aux=2
Press any key to continue . . .

• 09-28-2012
You've got a LOT of nerve asking for help on this!

You know the legend behind the game:

When the monks who labor night and day, moving the disks across the 3 diamond pegs, finally complete moving all the disks.

The universe, and everyone in it, in the twinkling of an eye, will return, to it's unrealized state.

You know this IS 2012!

You know the world will end in 2012, according to the legends!

Gah!!!

Prove to me you're not working to help those darn monks of Hanoi!

(Otherwise, I may be forced to help and also show my most colorful console game.)
• 09-28-2012
rwagstaff
Hurry I need to finish this so I can release it virally on December 21, 2012. All will be well with the universe I promise. =)
• 09-28-2012
rwagstaff
I'm embarrassed. I didn't have brackets around my else statement so it was blowing up. All is well in the universe again. =)
• 09-28-2012
anduril462
Glad you got it sorted out. You should look into using an editor that has an "auto-indent" feature. If you had a decent one (and there are many free, decent editors out there), it would very likely have made the indentation reflect the flow of the program, i.e. the two recursive calls to SolveTower, and the printf between them, line up with the if and else lines, so you could see that something was wrong.

EDIT: oh, and don't be embarrassed. Everybody has made that mistake a number of times.