# Thread: The Hanoi Towers Recursive

1. ## The Hanoi Towers Recursive

Hey guys, ive just been doing this coding but im very lost at the debuggings, just can somone help me, tell me whats wrong and debug it. I tried the debugging for a bout an hour and as a newbie its hard to tell where my problem is.

It is suposed to be in recurssion and tel the use what steps to take to solve the hanoi problems.

Here is the Code

Code:
```#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
double Hanoi (int numdisks, int start, int goal, int temp);
main()
{
int num, steps, start, temp, goal, numdisks;
start = 1;
temp = 2;
goal = 3;

printf ("          Welcome T0 Kavi'S Towers Of Hanoi\n");

printf("\nEnter a number to calculate the steps for the Towers Of Hanoi:
");
scanf("%d",&num);

while (num < 1 || num > 15 )
{
printf ("disks Have to be above 0 and under 12 Re Enter
Value: ");
scanf("%d",&num);
}
steps =  pow(2,num)- 1 ;
num = numdisks
printf("\n Number of steps is: %i ",steps);
printf("\n Move Disk from Peg %i to Peg %i", Hanoi (numdisks, start,
goal, temp);

fflush(stdin);
getch();
return 0;
}
{
Hanoi (numrings,start,goal,temp)
if numrings = 1
printf ("move a disk from "start" to "goal")
else
Hanoi (numrings-1, start, temp, goal)
Hanoi (numrings 1, start, goal, temp)
Hanoi (numrings-1, temp, goal, start)
}```

2. Don't use fflush(stdin), see the FAQ for why.

Code:
```printf("\n Move Disk from Peg %i to Peg %i", Hanoi (numdisks, start,
goal, temp);```
You're missing a closing parenthesis ).

Code:
`if numrings = 1`
This isn't BASIC.
Code:
`if(numrings == 1)`
You're missing curly braces around the Hanoi function . . . or rather they're in the wrong place. (Might want to look at the function tutorial, near here.) You're also missing lots of semicolons.

3. Look up tutorials on if statements, I have never seen people put a space before the parameters of a function in C or C++. Put a return statement on the Hanoi function. Don't fflush(stdin).

Oh, and learn decent indenting.

4. does any one acutally have the recursive to the Hanoi Towers
so i just have like a basic thing to look at....please and thanks

5. > does any one acutally have the recursive to the Hanoi Towers
Probably, but that's not the point.

If you read some of the previous posts, and spent some of the last 18 hours or so just fixing the syntax errors in your code (or at least posting your progress so far), then perhaps we could help you to making YOUR answer work.

You're basically on the right track, there's just some detail for you to try and fix.

6. I continued working on it, still some flaws some errors bout the pow statement can somone jus clear up some of the mistakes

Code:
```#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
double Hanoi (int numdisks, int start, int goal, int temp);
main()
{
int num, steps, start, temp, goal, numdisks;
start = 1;
temp = 2;
goal = 3;

printf ("          Welcome T0 Kavi'S Towers Of Hanoi\n");

printf("\nEnter a number to calculate the steps for the Towers Of Hanoi: ");
scanf("%d",&num);

while (num < 1 || num > 15 )
{
printf ("Factorials Have to be above 0 and under 12 Re Enter Value: ");
scanf("%d",&num);
}
steps =  pow(2,num)- 1 ;
num = numdisks
printf("\n Number of steps is: %i ",steps);
printf("\n Move Disk from Peg %i to Peg %i", Hanoi (numdisks, start, goal, temp);

fflush(stdin);
getch();
return 0;
}
{
Hanoi (numrings,start,goal,temp);
if (numrings = 1);
printf ("move a disk from "start" to "goal");
else;
Hanoi (numrings-1, start, temp, goal);
Hanoi (numrings 1, start, goal, temp);
Hanoi (numrings-1, temp, goal, start);
}```

7. I'm wondering how you managed to get this far when the basic mechanics of writing C are beyond you.

Code:
```#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>

/*!! why exactly are you returning a double here? */
double Hanoi(int numdisks, int start, int goal, int temp);

/*!! always say main returns int */
int main()
{
int num, steps, start, temp, goal, numdisks;
start = 1;
temp = 2;
goal = 3;

printf("          Welcome T0 Kavi'S Towers Of Hanoi\n");

printf("\nEnter a number to calculate the steps for the Towers Of Hanoi: ");
scanf("%d", &num);

while (num < 1 || num > 15) {
/*!! your instructions and code do not agree */
printf("Factorials Have to be above 0 and under 12 Re Enter Value: ");
scanf("%d", &num);
}
steps = pow(2, num) - 1;
num = numdisks;/*!! missing ; */

printf("\n Number of steps is: %i ", steps);

/*!! 1. This printf() is missing a closing ) */
/*!! 2. The number of parameters does not match the number of conversions */
/*!! 3. The types are wrong - you said Hanoi() returns a double */
printf("\n Move Disk from Peg %i to Peg %i",
Hanoi(numdisks, start, goal, temp);

/*!! we already said this was bad, you still use it */
fflush(stdin);
getch();
return 0;
}

{ /*!! what is this brace doing here? */

/*!! why have to dropped back to implicitly declaring the return type? */
Hanoi(numrings, start, goal, temp);  /*!! where is the { ? */
if (numrings = 1);   /*!! use == for comparison, and drop the ; */
/*!! you already used printf successfully above, this isn't good at all */
printf("move a disk from " start " to " goal ");
else;/*!! more stray ; and no {} */
Hanoi (numrings-1, start, temp, goal);
Hanoi (numrings 1, start, goal, temp);   /*!! maybe -1 ? */
Hanoi (numrings-1, temp, goal, start);
}```