-
Wha'ts wrong
I can't seem to find the problem with this program. I've been looking over it for a long time, and i just can't find the error. It comes up with around 8 errors, but i think only one is causing the rest or the errors.
Code:
#include <stdlib.h>
#include <stdio.h>
struct tree
{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root;
struct tree *stree(struct tree *root, struct tree *r, char info);
void print_tree(struct tree *root, int 1);
void main(void)
{
char s[80];
root = NULL;
do
{
printf("Enter a letter: ");
gets(s);
if(!root) root = stree(root, root, *s);
else stree(root, root, *s);
}while(*s);
print_tree(root, NULL);
}
struct tree *stree(struct tree *root, struct tree *r, char info)
{
if(!r)
{
r = (struct tree *) malloc(sizeof(struct tree));
if(!r)
{
printf("Out of Memory\n");
exit(0);
}
r->left = NULL;
r->right = NULL;
r->info = info;
if(!root) return r;
if(info<root->info) root->left = r;
else root->right = r;
return r;
}
if(info<r->info) stree(r, r->left, info);
else
stree(r, r->right, info);
}
void print_tree(struct tree *r, int 1)
{
int i;
if (!r)return;
print_tree(r->right, 1+1);
for(i=0; i<1; ++i) printf(" ");
printf("%c\n", r->info);
print_tree(r->left, 1+1);
}
-
1. What is the program supposed to do?
2. What are the errors that you get?
3. Change "void main()" to "int main()".
4. You most probably do not need a global variable, in this case the one named root.
-
Well, there are quite a few errors. For example, when you declare a function, you use a variable name which is one character long and begins with a number:
void print_tree(struct tree *root, int 1);
In the same above function, you don't need the "struct" keyword. "tree" already describes the type of root.
So these two mistakes repeat everywhere in the code.
-
To be fair, the second is not a mistake, as far as I know it is just a C style of doing things... though I believe C programmers tend to use typedef to eliminate the need for that anyway.
-
this program prints a binary tree depending on the characters you enter.
The errors have to do with a missing '{' , ')', or ';' .
The first error says: '{' : missing function header (old-style formal list?)
ps- this is the whole program, it can be copied and pasted into a compiler. I use Visual Studio 2005.
-
> void print_tree(struct tree *root, int 1);
This 'l' (ell) looks rather like a 1 (one)
Numbers cannot be identifiers.
-
Yep, that was it.
Thank you all for the help, it works now.