Thread: Help required

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    Help required

    Hi guys totaly new to this so please be gentle.
    I cant get the following code to work.....i know its prob sonething stupid...any ideas would be appreciated



    Code:
    #include<stdio.h>
    
     struct custdata
    
     {
    
       char cname[20];
    
       char custemail[30];
    
       int numorders;
    
    
       
    
     };
    
     
    
     struct node 
    
     {
    
           
    
        struct node *left;
    
        struct node *right;
    
        struct custdata *cdata; //declares a pointer to the custdata struct
    
      };
    
     
    
     
    
     struct node * init(struct node *);
    
     struct custdata * initcustdata(struct custdata *);
    
     int menu(int);
    
     struct custdata * getdetails(struct custdata *);
    
     struct node * add(struct node *,struct custdata *);
    
     struct node * display(struct node *);
    
     int flag = 0;
    
    
    
     int main() 
    
     {
    
    
      int choice;
    
      struct node * tree;
    
      tree = init (tree);
    
      struct custdata *cdata;
    
      cdata = initcustdata (cdata);
    
    
     
    
         do
    
         {
    
             choice = menu(choice);
    
             switch(choice)
    
             {
    
              case 1 :
    
                cdata = getdetails(cdata);
    
                tree = add(tree,cdata); 
    
                break;
    
              case 2 : 
    
                display(tree);
    
                break;
    
              case 3 :
    
              printf("EXITING ! \n");
    
              break;
    
             }
    
         }while(choice != 3);
    
    
    
     }
    
     
    
     struct node * init (struct node *tree)
    
     {
    
      tree = NULL;
    
      return tree;
    
     }
    
     
    
     struct custdata * initcustdata (struct custdata *cdata)
    
     {
    
      cdata = NULL;
    
      return cdata;
    
     }
    
     
    
     int menu (int choice)
    
     {
    
      printf("PRESS 1 to ADD to BINARY TREE\n");
    
      printf("PRESS 2 to DISPLAY sorted BININARY TREE\n");
    
      printf("PRESS 3 to EXIT the BINARY TREE\n");
    
      scanf("%d",&choice);
    
      return choice;
    
     }
    
     
    
     struct custdata * getdetails(struct custdata *cdata)
    
     {
    
      cdata = new custdata; //allocates memory for cdata
    
      printf("Enter Customer Name to add to record \n");
    
      scanf("%s",cdata->cname);
    
      printf("Enter Customer Email to add to record \n");
    
      scanf("%s",cdata->custemail);
    
      printf("Enter quantity required to add to record \n");
    
      scanf("%d",&cdata->numorders);
    
      return cdata;
    
    }
    
     
    
     struct node * add (struct node *tree,struct custdata *cdata) 
    
     {
    
         if(tree == NULL) //No data 
    
         {
    
              tree = new node; //create new node
    
              tree->left = NULL;
    
              tree->right= NULL;
    
              tree->cdata = cdata;//assign struct customer pointer variable the cdata value which contains the entered user customer data
    
              return tree;
    
         }
    
        
    
         if(strcmp(cdata->cname,tree->cdata->cname)<0) //tests to see if entered details name is  < tree->cdata->name alphabetically
    
         {
    
          tree->left= add(tree->left,cdata);
    
          return tree;
    
         }
    
            if(strcmp(cdata->cname,tree->cdata->cname)>0) //tests to see if entered details name is  >tree->cdata->name alphabetically
    
               {
    
                tree->right = add(tree->right,cdata);
    
                return tree;
    
               }
    
               
    
                if(strcmp(cdata->cname,tree->cdata->cname)==0) //tests to see if entered details name matches tree->cdata->name alphabetically
    
                {
    
                 printf("Name matches incrementing orders field \n");
    
                 tree->cdata->numorders = tree->cdata->numorders++;
    
                 return tree;
    
     
    
               }
    
     
    
     }
    
     
    
     struct node * display (struct node *tree) 
    
     {
    
    
    
     if(tree!=NULL) 
    
     {
    
      display(tree->left); //recurses and passes in left subtree which is all the smallest data alphabetically first
    
      printf("Customer Name is %s \n",tree->cdata->cname);
    
      printf("Customer Email is %s \n",tree->cdata->custemail);
    
      printf("Number of Orders is %d \n",tree->cdata->numorders);
    
      display(tree->right); //recurses and passes in left subtree which is all the largest data alphabetically from smallest right side data to largest right side data      
    
     }
    
    
     }

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    The errors that im getting are:

    binversion4.c: In function `getdetails':
    binversion4.c:83: error: `new' undeclared (first use in this function)
    binversion4.c:83: error: (Each undeclared identifier is reported only once
    binversion4.c:83: error: for each function it appears in.)
    binversion4.c:83: error: parse error before "custdata"
    binversion4.c: In function `add':
    binversion4.c:97: error: `new' undeclared (first use in this function)
    binversion4.c:97: error: parse error before "node"

    ive worked out that its "new" thats causing the problem and that malloc should be there but am at a loss on how to implement it

    thanks in advance
    Last edited by ItsMeHere; 06-01-2006 at 05:48 AM.

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Well, I honestly can't be arsed checking all that, but I assume your compiler doesn't seem to recognise the 'new' operator: binversion4.c:83: error: `new' undeclared (first use in this function)

    I'd suggest replacing it with malloc to see if it gets rid of that error.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > cdata = new custdata;
    new is C++, this is C
    You need
    #include <stdlib.h>

    and
    cdata = malloc ( sizeof *cdata );


    > tree->cdata->numorders = tree->cdata->numorders++;
    Prefix and postfix operators modify the value directly, so there's no need to assign (it is in fact wrong to try)
    Just do
    tree->cdata->numorders++;
    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.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    Thanks Salem...your info was spot on...got another slight problem with it but i will try and sort it before i post again..lol.
    Once again thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 04:47 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM