Output character as a node

I want it to output
Hi Hello I'm Good
Hi Hello lel
Hi How are you

But it outputs
72 72 73
72 72 108
72 72

The tree:
_________________Hi
___________Hello__________How are you
______I'm good____lel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include<mmsystem.h>
#include<thread>
#include<stdlib.h>
using namespace std;

struct node
{
	char data[1000];
	struct node* left;
	struct node* right;
};

void printArray(char ints[], int len)
{
	int i;
	for (i = 0; i<len; i++) {
		printf("%d ", ints[i]);
	}
	printf("\n");
}

void printPathsRecur(struct node* node, char path[], int pathLen)
{
	if (node == NULL) return;

	strcpy_s(&path[pathLen], 1000, node->data);
	pathLen++;

	if (node->left == NULL && node->right == NULL)
	{
		printArray(path, pathLen);
	}
	else
	{
		printPathsRecur(node->left, path, pathLen);
		printPathsRecur(node->right, path, pathLen);
	}
}

void printPaths(struct node* node)
{
	char path[1000];
	printPathsRecur(node, path, 0);
}

struct node* newNode(char *data)
{
	struct node* node = (struct node*)malloc(sizeof(struct node));
	strcpy_s(node->data, data);
	node->left = NULL;
	node->right = NULL;
	return node;
}

int main()
{
struct node *root = newNode("Hi");
	root->left = newNode("Hello");
	root->right = newNode("How are you");
	root->left->left = newNode("I'm good");
	root->left->right = newNode("lel");
	printPaths(root);
	system("pause>0");
	return 0;
}
My C is terrible, but I see this on line 17:
printf("%d ", ints[i]);

You clearly don't want to be printing out a number (%d) :)
I removed the %d part and now it outputs blank. I think my problem is the logic itself. There's something wrong in my program. haha
Changed part
1
2
3
4
5
6
7
8
void printArray(char ints[], int len)
{
	int i;
	for (i = 0; i<len; i++) {
		cout << " " << ints[i];
	}
	cout << "\n";
}
I reran the program and it now outputs
H H I
H H l
H H

weird.

It outputs the first letter of a node not the actual string itself.
whack in a few break points and examine your strings as they are going through the program. Or put in some debug print statements, whichever one your prefer.

edit:

wait a minute....
 
printPathsRecur(node, path, 0);


Do you ever change your pathlength to anything other than zero?
Last edited on
pathlength is incremented at printpathsrecur();
Idk how to fix it. how can i debug it?
but surely that means you're only printing out 1 char, then 2 char etc each time you call printArray()?

how can i debug it?

like I said: either step through it with a debugger or sprinkle some printf or cout statements in your code to display various values of stuff as the code runs.
I added %s still not working
1
2
3
4
5
6
7
8
void printArray(char ints[], int len)
{
	int i;
	for (i = 0; i<len; i++) {
		printf("%s ", ints[i]);
	}
	cout << "\n";
}

and idk how to debug. I'm still beginner. Sorry haha
I fixed the break problems
1
2
3
4
5
void printPaths(struct node* node)
{
	char path[9000];//changed this to 9000 instead of 1000
	printPathsRecur(node, path, 0);
}


now my problem is the string itself
The increase from 1000 to 9000 doesn't look necessary.

Fixing your original C code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
//#include<mmsystem.h> WHY SYSTEM HEADERS?
//#include<thread>
#include<stdlib.h>
#include<string.h>

#define DEBUG_INFO

// replacement for system("pause>0");
void waitForUser(){
    printf("Press ENTER to continue...\n");
    while((getchar() != '\n') && !feof(stdin))
    ; /* Empty loop */
}

struct node
{
    char data[1000];
    struct node* left;
    struct node* right;
};

void printArray(char ints[], int len)
{
    int i;
    for (i = 0; i<len; i++) {
        //printf("%d ", ints[i]);
        printf("%c", ints[i]);
    }
#ifdef DEBUG_INFO
    // for debugging, o/p length, too
    printf(" (%d)");
#endif
    printf("\n");
}

void printPathsRecur(struct node* node, char path[], int pathLen)
{
    if (node == NULL)
        return;

    //strcpy_s(&path[pathLen], 1000, node->data);
    strncpy(&path[pathLen], node->data, 1000 - pathLen); // added - pathLen
    //pathLen++;
    pathLen+=strlen(node->data);
    // add space
    strncpy(&path[pathLen], " ", 1000 - pathLen); // added - pathLen
    pathLen++;
 
    if (node->left == NULL && node->right == NULL)
    {
        printArray(path, pathLen);
    }
    else
    {
        printPathsRecur(node->left, path, pathLen);
        printPathsRecur(node->right, path, pathLen);
    }
}

void printPaths(struct node* node)
{
    char path[1000];
    printPathsRecur(node, path, 0);
}

struct node* newNode(char *data)
{
    struct node* node = (struct node*)malloc(sizeof(struct node));
    //strcpy_s(node->data, data);
    strncpy(node->data, data, 1000);
    node->left = NULL;
    node->right = NULL;
    return node;
}

int main()
{
    struct node *root = newNode("Hi");
    root->left = newNode("Hello");
    root->right = newNode("How are you");
    root->left->left = newNode("I'm good");
    root->left->right = newNode("lel");
    printPaths(root);
    // SHOULD REALLY FREE ALL MEMORY YOU ALLOCATED BEFORE EXITING!
    //system("pause>0");
    waitForUser();
    return 0;
}


Hi Hello I'm good  (18)
Hi Hello lel  (13)
Hi How are you  (15)


I tweaked the code so it shows how much of the buffer gets used. Rather less than 9000 characters!

And while it now displays what you wanted, the code could be simplified somewhat (e.g. printArray.)

Andy
Last edited on
Thank you for the code! I really appreciate it!
Topic archived. No new replies allowed.