-
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
}
}
-
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
-
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.
-
> 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++;
-
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.