Thread: Wha'ts wrong

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    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);
    	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2006
    Location
    Silale, Lithuania
    Posts
    3
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void print_tree(struct tree *root, int 1);
    This 'l' (ell) looks rather like a 1 (one)
    Numbers cannot be identifiers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Yep, that was it.
    Thank you all for the help, it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM