samohtvii

I don't really understand why people think I answered my own question. An answer I am looking for is something along the lines of

C++

void readBinaryTree(BinaryTree* &p, ifstream *fin)

C

void readBinaryTree(BinaryTree* *p, FILE* fin)
prototype:
void readBinaryTree(BinaryTree**, FILE*)

and then used in the program

int main()
{
    //Open filestream and create a bianry tree
    readBinaryTree(&myTree, fin); 
}

That's my guess at how you would do it in C but can someone clarify this. I don't really think I need to post the rest of the program.

Thanks

samohtvii

Can someone tell me how to write

void readBinaryTree(BinaryTree *&p, ifstream &fin) {

in C
...

    `void readBinaryTree(BinaryTree **p, ifstream *fin) {`
    `readBinaryTree(&p, fin)`  

???

Thanks

samohtvii

I am looking to create a binary tree and then print it out. I am getting some strange errors I cant fix.

#include <stdio.h>
#include <string.h>

struct animalTree
{
    char data[100];
    struct animalTree *left;
    struct animalTree *right;
};

typedef struct animalTree aTree;

void fillTree(FILE*, struct animalTree*);
int readNext(char*, FILE*, int*);
void printTree(struct animalTree*);
struct aTree *makeNode(char*);

aTree* root;

int main()
{
    FILE *fp;
    fp=fopen("animals", "r");
    char stuff[40];
    if(fp != NULL)
    {
        fillTree(fp, root);
        printTree(root);
    }
    else
    {
        root = (aTree*)makeNode("Horse");
    }   
    fclose(fp);
    return 0;
}

void fillTree(FILE* fp, struct animalTree *node)
{
        char data[100] = {""};
    int isNULL = 1;
    if(readNext(data, fp, &isNULL))
        return;
    if(isNULL == 1)
    {
        node = (aTree*)makeNode(data);
        fillTree(fp, node->left);
        fillTree(fp, node->right);
    }
}

void printTree(struct animalTree *root)
{
    if(root == NULL) 
    {
        printf("%s", "here");
        return;
    }
    printf("%s", root->data);
    printTree(root->left);
    printTree(root->right);
}

int readNext(char* data, FILE* fp, int* isNULL)
{
    fgets(data, 100, fp);
    if(feof(fp))
        return 1;
    else
    {
        if(strcmp(data, "NULL"))
        {
            isNULL = 1;
        }
        else
        {
            isNULL = 0;
        }
        return 0;
    }
}

struct aTree* makeNode(char* data)
{
    aTree *node;
    node = (aTree*)malloc(sizeof(aTree));
    if (node)
    {
        strcpy(node->data, data);
        node->left = NULL;
        node->right = NULL;
    }
    return node;
}

ERRORS:

user@ubuntu:~/Desktop/Assignment2/C$ gcc program.c 
program.c: In function ‘readNext’:
program.c:74:11: warning: assignment makes pointer from integer without a cast [enabled by default]
program.c: In function ‘makeNode’:
program.c:87:17: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]    
program.c:94:2: warning: return from incompatible pointer type [enabled by default]

I am aware these are warnings and that the program ...

samohtvii

Ok thanks for the help. I didn't nother posting code or errors because it wouldn't really help much.

samohtvii

Thanks for the advice.
I am currently going through changing what I can. I just have some questions about some errors I am getting.

Can C pass by reference?
Is there something about structs C doesn't like because everywhere my sruct is used it gives an error.
Does C have 'for' loops?

and lastly

I created a new node:
node = new animalTree(data);

with a constructor that initializes data and the left right nodes.

how do I do that in C

node = (animalTree(data)*) malloc(sizeof(animalTree)); ???'

THanks for the help

samohtvii

I was just wondering if anyone can give me some info on converting C++ to C. I don't really care about finding a program to do it for me, I would just like to know what is different between the two. My program is 'fairly' simple. uses file IO, prints, reads and all the other usual stuff.
Does anyone know a decent website that looks at these differences and hows what the ifferences are?

What I have worked out.
No headers except stdio.h
cout = printf
fstream = FILE
new = malloc

Other changes?

Thanks

samohtvii

Hi all,

Say i have 2 nodes of a tree.

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

NODE1
data = 3;
left = NULL;
right = NULL

and

NODE2
data = 6;
left = NULL;
right = NULL

How do i say Node 2 if the left link of node 1 so it creates the start of a tree.

So it looks like

    NODE1
    /   \
 NODE2  NULL

I thought I could say node1->left = &node2; // left = the address of node 2.

Is that right?

Thanks

samohtvii

Looks like i'm all goos at this stage. Managed to work it all. Thanks for all the help.
That link you gave was a godsend. Exactly what i needed.

Thanks

samohtvii

Ok I have it all set-up now works fine. I just do all the filling of the tree and then i want to later on in the program read the data but the i can't get the root back.
I say

animalTree* root;

like i had done before but it seems to return maybe the end of the tree because it outputs garbage. How can i get the root back? :S
Thanks

samohtvii

EDIT:

OK I have mine set-up similar to the example in that link.

struct animalTree {
    char data[100];
    animalTree *left;
    animalTree *right;
};

void fillTree(ifstream& ins, animalTree *&node) //Checks if file data says "NULL"
{
if(!readNext(data, ins)) return;

node = new animalTree(data);
fillTree(ins, node->left);
fillTree(ins, node->right);
}

So it doesn't like the node = new aniaml... line. Do i need a constructor that says like

animalTree(char* x) (data = x);

or something?
and do i ever need to make the struct like animalTree thisIsATree;
Thanks

samohtvii

Hi all,

I am having trouble getting a tree to and from a text file.

The text file will hold questions and animals. Each node should hold one question and one animal.
So if you can imagine:

      Does it have 4 legs?
       /               \
 Does it purr?    Does it swim?
   /       \        /       \
 Cat      Dog     fish      NULL

yes being left and no being right.

That is the tree that is created. How would that get read into a file. How can I say in the text file "Don't go down any further past 'does it purr', go back to the right node and put the new node there".

So the file will be saved and can be read back into another run of the program.
How can i reconstruct this tree without knowing what it looked like before.

I hope this makes sense. It's doing my head in trying to think about it.

Ok so what i was thinking was add the nodes in sequence. so the text file would be
Does it have 4 legs?
Does it purr?
Cat
Dog
Does it swim?
fish

Does this require recursive functions?
Now that i think about it that ^above way would work right?

maybe just one node with 'data' and if ends in a question mark its a question and has more branches.

Any thoughts? ideas?

Thanks

samohtvii

All been sorted out. Thanks for the help. The 6 was a typo.
As for the language I got kinda mixed up between what was used in what, it's been a while. It was suppose to be c++ so I've made the corrections.
Thanks

samohtvii

AHAHAHAHA. THanks so much mate. What a lame error. I would have never got that.

Thanks

samohtvii

Hi all,

I am trying to write a CWEB c++ program that just keepsgoing until the user types 'no'. Anyway i get some errors that I can't fix.

Here is the .w file (x's are comment blocks to save congestion)

@* Program Skeleton

@ x

@c
using namespace std;@;
@<Header files@>@;
@<The main program@>@;

@ x

@<Header files@>=
#include <iostream>
#include <fstream>

@* x

@<The main program@>=
int main()
{
   @<Variables of main@>@;
   do {
      @<Check play again@>@;
   } while(continue != "No");

   return 0;
}

@ x

@<Check play again@>=
cout << "Do you want to play again? ";
cin >> continue;

@ x

@<Variables of main@>=
char continue[3];

@ x

The errors i get are:

ctangle test.w
g++ test.c
./test.w: In function ‘int main()’:
./test.w:40:6: error: expected unqualified-id before ‘continue’
./test.w:34:6: error: expected primary-expression before ‘continue’
./test.w:34:6: error: expected ‘;’ before ‘continue’
./test.w:25:8: error: expected primary-expression before ‘continue’
./test.w:25:8: error: expected ‘)’ before ‘continue’
./test.w:25:8: error: expected ‘;’ before ‘continue’
./test.w:25:8: error: continue statement not within a loop
./test.w:25:16: error: expected ‘;’ before ‘!=’ token
./test.w:25:16: error: expected primary-expression before ‘!=’ token
./test.w:25:22: error: expected ‘;’ before ‘)’ token

No idea what they are asking me to do.

Here is the .c file

/*2:*/
#line 6 "./test.w"

using namespace std;
/*3:*/
#line 13 "./test.w"

#include <iostream> 
#include <fstream> 

/*:3*/
#line 8 "./test.w"

/*4:*/
#line 19 "./test.w"

int main()
{
/*6:*/
#line 39 "./test.w"

char continue[3];

/*:6*/
#line 22 "./test.w"

do{
/*5:*/
#line 32 "./test.w"

cout<<"Do you want to play ...

samohtvii

Hi all,

I have a simple program:

#include<iostream>
using namespace std;
6
int main()
{
    char x;
    do
    {
        scanf("%c", x);
    } while (x != 'y');
    return 0;
}

and for some reason when i gcc test.c i get
"test.c:1:20: fatal error: iostream: No such fiel or directory
compilation terminated."

I had a look around but couldn't find oput what the problem is.

This is the first time I have used gcc to compile since i installed so could the iostream fiel be missing :S?

Please help.
Thanks