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

This is a discussion on Towers of Hanoi is making my head explode and also overflowing badly within the C Programming forums, part of the General Programming Boards category; I am so confused. Input # of disks, start peg and end peg. My program is failing miserably yet it ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    15

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



  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.)
    Last edited by Adak; 09-28-2012 at 07:51 PM.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    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. =)

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    I'm embarrassed. I didn't have brackets around my else statement so it was blowing up. All is well in the universe again. =)

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Towers Of Hanoi
    By ineedmunchies in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2011, 04:42 PM
  2. Towers of Hanoi are ........ing me off
    By bananaHUNT in forum C++ Programming
    Replies: 11
    Last Post: 12-14-2009, 12:13 PM
  3. My head is going to explode.
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 11-16-2003, 01:41 AM
  4. The Towers of Hanoi
    By awkeller in forum C Programming
    Replies: 7
    Last Post: 01-07-2002, 06:07 AM
  5. Towers of Hanoi
    By janehung in forum C Programming
    Replies: 12
    Last Post: 01-07-2002, 05:40 AM

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