Hiya,

I've got a lickle program here which reads data about people into structures and then saves their surnames in a Binary Search Tree.

There is more stuff to go to that program, that is why I need structures, but at the moment I can not understand why it doesn't read ALL the surnames from file into BST. It seems that all 10 structures (I have 10 people in the input file) are created, but only 3 surnames are saved into the tree and I can't understand why. i've got a basic tree output function which is run when you press 2 - the program compiles OK so you can test it if you want.

The members.data.txt file needs to be renamed to members.data

Thanks!

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <stdlib.h>

typedef struct{
            char dummy[256];
            char surname[225];
            char name[256];
            int ID;
            char house[256];
            char street[256];
            char town[256];
            char county[256];
            char area[256];
            char country[256];
            char postcode[256];
            int phone;
            char email[256];
            char boattype[256];
            char boatname[256];
            }user;
            
FILE *file;
int i=0;
char number_of_people[256];
int nop;
char yetdummy[256]; 
char yetdummy2[256]; 
char g[80];


            
struct node
{
char data[15];
struct node *left,*right;
};

void insert(struct node *r,struct node *p)
{
if((r->right==NULL)&&(strcmp(p->data,r->data)>0))
r->right=p;
else if((r->right!=NULL)&&(strcmp(p->data,r->data)>0))
insert(r->right,p);
if((r->left==NULL)&&(strcmp(p->data,r->data)< 0))
r->left=p;
else if((r->left!=NULL)&&(strcmp(p->data,r->data)< 0))
insert(r->left,p);
}

void tree(struct node *r,int c)
{
int top,flag;
struct node *w,*stack[20];
if(r!=NULL)
{ 

if(c!=4)
{
if(c == 1)
printf(" %s ",r->data);
tree(r->left,c);
if(c == 3)
printf(" %s ",r->data);
}
}
}

int main()
{ //open main

int choice,c,i,flag;
char temp='N',temp1[15];
struct node *s,*root,*r,*q;
root = NULL;
do
{ //open do
system("cls");
printf("\n 1. Read the data file into the program");
//printf("\n 2. Delete ");
printf("\n 2. Search ");
//printf("\n 4. Display");
printf("\n 3. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&choice);

switch(choice)
{ //open switch
case 1:printf("***** Reading data... ***** ");
do
{file = fopen("members.data","r");
 if(file==NULL) {
    printf("Error: can't open file.\n");
    return 1;
  } 
  else {
    printf("File opened successfully.\n");
         }
 fscanf(file,"%s \n", number_of_people);
 //printf("%s \n", number_of_people);
 nop = atoi(number_of_people);
 //printf("%d\n", nop);
 user usera[nop]; 
 
  for (i=0; i<nop; i++){
system("cls");
  printf("Reading a structure...\n");
      fgets(usera[i].dummy, 256, file);        
      fgets(usera[i].surname, 256, file); 
      fgets(usera[i].name, 256, file);  
      fgets(yetdummy, 256, file);  
      usera[i].ID = atoi(yetdummy);      
      fgets(usera[i].house, 256, file);  
      fgets(usera[i].street, 256, file);  
      fgets(usera[i].town, 256, file); 
      fgets(usera[i].county, 256, file);  
      fgets(usera[i].area, 256, file);  
      fgets(usera[i].country, 256, file);  
      fgets(usera[i].postcode, 256, file);  
      fgets(yetdummy2, 256, file); 
      usera[i].phone = atoi(yetdummy2);
      fgets(usera[i].email, 256, file);  
      fgets(usera[i].boattype, 256, file);  
      fgets(usera[i].boatname, 256, file);        
     
      printf("Read the user with surname  %s\n", usera[i].surname);      
      printf("His ID is %d\n", usera[i].ID); 
      printf("Finished reading, now adding to tree\n");      
      s=malloc(sizeof(struct node));
      s->left=NULL;
      s->right=NULL;
      strcpy(s->data,usera[i].surname);
      if(root==NULL)
      root=s;
      else
      insert(root,s);
      printf("User inserted"); 
      sleep(600);
}
fclose(file);
}

while(temp=='y');
break;

case 2:/*printf("****** Search Operation *******\n");
do
{
printf("\n Enter Name To Be Searched: ");
scanf("%s",temp1);
i=0;
s=root;
while(s!=NULL&&i==0)
{
if(strcmp(s->data,temp1)< 0)
s=s->right;
if(strcmp(s->data,temp1)>0)
s=s->left;
if(strcmp(s->data,temp1)==0)
i=1;
}
if(i==0)
printf("\nElement Not Found\n");
else
printf("\nElement Found\n");
printf("\nEnter More Elements[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
*/
do
{

c=1;
if(root==NULL)
printf("Tree Not Started Yet");
else
tree(root,c);
printf("\n Press Any Key To Continue......");
getch();
break;
}
while(c!=5);
}
}
while(choice!=3);
}
Attachments
10
#
Morris
Huw Rhys Morgan
1015
Dun Sailin
3, High Road
Abernowhere
Ceredigion
Wales
u.k.
ZZ49 1RU
000111456765123
h.w.m.morris@noclub.clubs.uk
Flyer
The Blue One
#
Price
David
235
Southdown
11, My Street,
Abersomewhere
Ceredigion
Wales
u.k.
ZZ49 1TH
000456129453445
d.price@noclub.clubs.uk
Invader
Goldcrest
#
Long
Fred
2
Still Sailin
24, East Street
LlanDafydd
Powys
Wales
u.k.
ZP47 1QT
000458910937834
f.long@long.people.uk
AberSloop
Jaqueta
#
Neal
Mark Junior Simon
56
Wish-i-were-sailin
The Farm,
Ystumnowhere
Plymlumon
Chile
South America
SA1 5CH
000996123456812
m.j.s.neal@yacht.club.ch
MetalBox
Kit of Parts
#
Zakaria
Phillip Peter
23
Cottage Farm
5, Upper South Road,
Handsworth
Birmingham,
West Midlands
U.K.
B19 99RU
000121888899992
zak@inland.clubs.uk
Mirror
Uppa Nova
#
Hardy
Nigel
456
Vicarage House
Main Street
Northern Village
Powys
Wales
UK
PW56 67ZZ
000675129765394
n.hardy@seaside.yachts.cy
PlasticBath
It'l nvr float
#
Piper
Peter
87
Poetry Cottage
Yellow Brick Street
Townsville
Never County
Dream State
USA
ZIP47 9N34
001345867194385
p.piper@bigboats.clubs.us
Apple
Master Sloop
#
Snooke
Neal
359
New House
Main Street
Llan Eastern
Ceredigion
Wales
U.K.
CR47 2HU
000286209434958
NealSnooke@yacht.clubs.uk
TwinHull
Keep m Together
#
Ratcliffe
Mark
657
The Old Chemical Works
Hillside
Aberystwyth
Ceredigion
Wales
U.K.
CR40 3BY
000374967284957
twolunches@yacht.clubs.uk
SlickBoat
Only One Year
#
Shipman
Richard
857
Old House
Side Street
Llan Eastern
Ceredigion
Wales
U.K.
CR47 2HM
000286209434924
R.Shipman@yacht.clubs.uk
SlickBoat
Only a Starter
#

These lines should give you a compiler error:

nop = atoi(number_of_people);
user usera[nop];

C does not allow an array to be declared with an (at compile time) unknown amount of elements. If you want to use dynamic arrays, you should have a look at malloc() and free() What compiler are you using?

[s]Hmm, it is weird, I just realised that... I am using Dev-C++

Anyway, I tried to hardcode number of people (10) as the array size; it still only returns 3 names :/[/s]

Never mind!

Another thing I wanted to ask - is it possible to change my code so that it stores pointers (to structures) in BST and will the search work for pointers?

Another thing I wanted to ask - is it possible to change my code so that it stores pointers (to structures) in BST and will the search work for pointers?

Sure why not? You can 'store' just about everything in a search-tree.
I also recommend that you read this article. It's a great tut on BST.

Comments
Thanks, great helper)))

These lines should give you a compiler error:

nop = atoi(number_of_people);
user usera[nop];

C does not allow an array to be declared with an (at compile time) unknown amount of elements. If you want to use dynamic arrays, you should have a look at malloc() and free() What compiler are you using?

Previously I've been able to compile such programs, I guess this has to do with VLAs (=Variable Length Arrays) ?

This article has been dead for over six months. Start a new discussion instead.