GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2005, 07:38
Tommmmm Tommmmm is offline
New Member
 
Join Date: Apr 2005
Posts: 5
Tommmmm is on a distinguished road

Help needed reading a file into a BST


I am creating a program to sort fishing entries, entered from a text file, into a binary search tree. As you can see from the code im having some problems!!

Im new to this and any help would be greatly appreciated.
Thanks

HEADER FILE----------------------
CPP / C++ / C Code:
/* Field definitions for competitor structure*/
typedef struct competitors
{
    char name[30];
    char address[100];
    char phone[20];

    int number;

    int river_ounces;
    int sea_ounces;
    int fly_ounces;

    struct competitors *left;
    struct competitors *right;

}data;

MAIN PROGRAM-------------------------------

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include "fishing.h"

/*  This is a fisihing competition program designed
 *  @authour Tom Elliott
 *  @version 05/03/05
 *  This code is based on the example solution to assignment 2 written by Dave Price
 */



/* The main function executes the program */
main()
{
	int status = getInput();
};

/* The getInput function enables the program to ask the user for input
 *
 */

int getInput()
{
   char file_name[80]; /* No file name will be longer than 79 characters*/
   char temp[80];
   char *eof = "a"; /* will indicate the end of the file */


   FILE *nfile;

    printf("CI3370 Assignment 3\n\n ");
    printf("Please enter the name of the file you wish to open:");
    scanf("%s", &file_name);
    /* fopen takes two string arguments. The first is the name of the file the
     * second is the access character which in this case shows that the file is
     * being opened to be read
     */
    nfile = fopen(file_name, "r");


};

/*create the binary tree and add data */
    while (eof != NULL)
    {

        struct competitors *new_competitors;
        new_competitors = (struct competitors *) malloc (sizeof(struct competitors)); //NOTE TO SELF, THIS COULD CAUSE PROBLEMS !!!//
        /*set the left and right nodes to null*/
        new_competitors-> right = NULL;
        new_competitors-> left  = NULL;

        int stones, pounds, ounces;

        /*save the competitors name*/
        eof = fgets(temp, 80, nfile);

        if(eof == NULL)
	{
            /*IF here at end of file*/
            break;
        };

	strncpy(new_competitors-> name , temp, 30);

        /*save the competitors address*/
        fgets(temp, 80, nfile);
        strncpy(new_competitors-> address , temp, 100);

        /*save the competitors phone number*/
        fgets(temp, 80, nfile);
        strncpy(new_competitors-> phone_num , temp, 20);;

        /*save the competitors number*/
        new_competitors-> number = number;
        mumber++;

        /*save the competitors weights for the competition*/
        fgets(temp, 80, nfile);
        sscanf(temp, "%d %d %d", &stones, &pounds, &ounces);
        new_competitors-> riv_ounces = ounces(stones, pounds, ounces);

        fgets(temp, 80, nfile);
        sscanf(temp, "%d %d %d", &stones, &pounds, &ounces);
        new_competitors-> sea_weight = ounces(stones, pounds, ounces);

        fgets(temp, 80, nfile);
        sscanf(temp, "%d %d %d", &stones, &pounds, &ounces);
        new_competitors-> fly_weight = ounces(stones, pounds, ounces);


        insert(new_competitors);

};



/*used in the first run of entering the structures into the tree
  if there is no root, root is set to to_insert otherwise the function
  insert_node is called passing root in as next_node*/
void insert(struct competitors *insert)
{

    if(root == NULL)
    {
        root = insert;

    };
    else if (root != NULL)
    {

        insert(root, insert);
    };
};


/* allows for a node to be inserted into the tree*/
int insert(struct fisher *next, struct fisher *insert);
{

    int compare;
    compare = compareTo(node, insert);
    /*printf("test_in_func_insert_node\n");*/

    /* the node is already in there*/
    if(compare == 0)
    {
        printf("error added twice");
        return 0;
    };

    /* traversing the left sub tree*/
    if(compare == -1)
    {

        if((node-> left) != NULL)
	{
            insert(node -> left_, insert);
        };
        /*If the left sub tree is empty*/
        if((node-> left) == NULL)
	{
            node-> left = insert;
            return 1;
        };
    };

    /*traversing the right subtree*/
    if(compare == 1)
    {



        if((node-> right) != NULL)
	{

            insert(node-> right, insert);
        }

        /*IF the right sub tree is empty*/
        if(node-> right == NULL)
	{

            node-> right = insert;
            return 1;
        };
    };
};
Last edited by LuciWiz : 12-Apr-2005 at 07:41. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 12-Apr-2005, 08:05
Tommmmm Tommmmm is offline
New Member
 
Join Date: Apr 2005
Posts: 5
Tommmmm is on a distinguished road
Thanks for the help on the tags.
  #3  
Old 12-Apr-2005, 08:25
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Tommmmm
Thanks for the help on the tags.

Could you please tell us what error did you get, if any? Or what went wrong and why do you think that happened? It would make it easier on us to provide you with faster and better help.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 12-Apr-2005, 08:28
Tommmmm Tommmmm is offline
New Member
 
Join Date: Apr 2005
Posts: 5
Tommmmm is on a distinguished road
Ive got a long long list of errors im trying to work through!

\im getting rid of a few of them slowly. An annoying one is:

passing of arg 3 of 'fgets' from incompatible pointer type.

Got that one on a number of lines when creating the binary tree.
  #5  
Old 12-Apr-2005, 08:47
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Tommmmm
Ive got a long long list of errors im trying to work through!

\im getting rid of a few of them slowly. An annoying one is:

passing of arg 3 of 'fgets' from incompatible pointer type.

Got that one on a number of lines when creating the binary tree.

No problem, we'll take them one by one.
You have a while statement outside of any function. That is illegal:

CPP / C++ / C Code:
/*create the binary tree and add data */
while (eof != NULL)

Also, what's up with all those semicolons after closing braces? There's no need for them. I suppose you got an error about needing a semicolon before while, but that was because it was outside of any function.

You have a function implemented like this:

CPP / C++ / C Code:
void insert(struct competitors *insert)
{

	if(root == NULL)
	{
		root = insert;

	}
	else if (root != NULL)
	{

		insert(root, insert);
	}
}

But what is root here? It's not defined in the current scope (or any, for that matter). Please try to solve these problems, and then we'll get to any others you may have.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.