R
rrice899
Hi,
I want to look at how recursion works. So I modified another recursion file with some code to print a dash ('-') starting from an empty char pointer "*s". Then concatenating the dash to 's'. But I can't get it to print or even run. There is one error:
line 11 error: expected ')' before string constant.
This is my first post and I'm just learning c(teaching myself). I apologize for any transgressions. I appreciate any comments. Below is the program:
#include <stdio.h>
#include <string.h>
void towers(int, char, char, char);
void towers(int n, char frompeg, char topeg, char auxpeg){
int indent = 10;
int i;
char *str = "";
{
for(i = 0; i < indent; i++)
{
char *strcat(str, "-");
}
if(n > 0){
towers(n-1, frompeg, auxpeg, topeg);
printf("\nMove disk %d from peg %c to peg %c\n", n, frompeg, topeg);
indent = indent +1;
printf("%c\n", *str);
towers(n-1, auxpeg, topeg, frompeg);
indent = indent + 1;
}
}
}
int main(){
int n = 2;
printf("The tower of Hanoi involves the moves: \n\n");
towers(n, 'A', 'C', 'B');
return 0;
}
I want to look at how recursion works. So I modified another recursion file with some code to print a dash ('-') starting from an empty char pointer "*s". Then concatenating the dash to 's'. But I can't get it to print or even run. There is one error:
line 11 error: expected ')' before string constant.
This is my first post and I'm just learning c(teaching myself). I apologize for any transgressions. I appreciate any comments. Below is the program:
#include <stdio.h>
#include <string.h>
void towers(int, char, char, char);
void towers(int n, char frompeg, char topeg, char auxpeg){
int indent = 10;
int i;
char *str = "";
{
for(i = 0; i < indent; i++)
{
char *strcat(str, "-");
}
if(n > 0){
towers(n-1, frompeg, auxpeg, topeg);
printf("\nMove disk %d from peg %c to peg %c\n", n, frompeg, topeg);
indent = indent +1;
printf("%c\n", *str);
towers(n-1, auxpeg, topeg, frompeg);
indent = indent + 1;
}
}
}
int main(){
int n = 2;
printf("The tower of Hanoi involves the moves: \n\n");
towers(n, 'A', 'C', 'B');
return 0;
}