Member Avatar for Lioshenka

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);
}

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?

Member Avatar for Lioshenka

[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!

Member Avatar for Lioshenka

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.

commented: Thanks, great helper))) +3

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) ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.