I am having problems with my code. I think it is the print function, but I'm not sure. Everything compiles correctly. Just no output. Any help is appreciated. Thanks
Here is what I have:

#include <iostream>

using namespace std;

struct node* tree = NULL;

struct node
{
    int data;
    struct node* left;
    struct node* right;
};

struct node* newNode(int data)
{
    struct node* tree = new(struct node);
    tree->data = data;
    tree->left = NULL;
    tree->right = NULL;

    return(tree);
}

struct node* insert(struct node* tree, int data)
{
    if (tree == NULL)
    {
        return(newNode(data));
    } 
    else
    {  
        if (data <= tree->data)
        {
            tree->left = insert(tree->left, data);
        }
        else
        {
            tree->right = insert(tree->right, data);
        }
        return(tree);
    }
}

void printTree(struct node* tree)
{
    if (tree == NULL)
    {
        return;
    }
    printTree(tree->left);
    cout << tree->data;
    printTree(tree->right);
}

int main()
{
    int i;
    do
    {
        cout << "Please enter a number into the tree: ";
        cin >> i;
        tree = insert(tree, i);
    }
    while( i != -0);
    {
        printTree(tree);
    }
    return 0;
}

Odd, I was able to get it to run correctly without any changes (under MinGW GCC 4.4.1). Which compiler are you using?

BTW, the use of the negative operator on zero is has no effect; zero is neither positive nor negative. HTH.

Maybe your system is broken, and doesn't flush the output buffer (or displays it incorrectly) when the program exits. Try adding a cout << flush or perhaps a cout << endl before exiting main.

I tried cout << flush; and cout << endl; with no luck. I also changed the "-0" to "0" with no luck either. Not sure what the issue is... I'll just have to talk to my professor next class. Thanks for the help... I appreciate it

On what platform are you programming? Try changing it to another, and use that. Try to run the .exe from the console and see what happens...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.