hi :)
I have a pro. here>>I have to write a prog. to get info. from a file and then to take these info. and to put them in a tree >>
so what happened here ,,that the compiler reads the info. correctly from the file and take them ..but when it reaches to the func.[add_book(...)] in the func.[load_file]it didn't make a new node !!
it only prints (empty)
plz help me :(
thanks ^_^
NOTE:Actually I wrote the prog in 2 seperate classes :P
#pragma once
#include<iostream>
#include<string>
using namespace std ;
class node
{
protected:
int no_of_books;
int id;
char title[30];
char author[30];
char publisher[30];
int status;
node*left;
node*right;
public:
node(void);
~node(void);
node(int);
node(int,char[],char[],char[],int);
friend class library;
};
#include "node.h"
#include<string>
#include<iostream>
using namespace std ;
node::node(void)
{
id=0;
title[30];
author[30];
publisher[30];
status=0;
no_of_books=0;
left=NULL;
right=NULL;
}
node::~node(void)
{
}
node::node(int Id)
{
id=Id;
title[30]=0;
author[30]=0;
publisher[30]=0;
status=0;
no_of_books=0;
left=NULL;
right=NULL;
}
node::node(int Id,char t[30],char a[30],char p[30],int s)
{
id=Id;
memcpy(title,t,30);
memcpy(author,a,30);
memcpy(publisher,p,30);
status=s;
no_of_books=0;
left=NULL;
right=NULL;
}
#pragma once
#include<iostream>
#include<string>
using namespace std ;
class library
{
public:
node*add_book(node*,int,char[],char[],char[],int);
bool search_id(node*,int);
void search_title(node*,char[]);
bool search_author(node*,char[]);
void borrow_book(node*,int);
void return_book(node*,int);
void load_file();
void put_to_new_file_();
void print_menu();
void print_n(node*);
bool check_AVL(node*,int*);
library(void);
~library();
node obj2;
node*root;
};
#include "node.h"
#include "library.h"
#include<string>
#include<fstream>
#include<iostream>
using namespace std ;
library::library(void)
{
root=NULL;
}
library::~library(void)
{
cout<<"the tree is destroyted \n";
}
void library::load_file()
{
FILE*fp;
char name[20];
cout<<"plz enter the name of the file U want to OPEN \n";
cin>>name;
fp=fopen(name,"a+");
if((fp=fopen(name,"a+"))==NULL)
{
cout<<"FILE NOT FOUND\n";
}
else
{
fscanf(fp,"%d",&(obj2.no_of_books));
for(int i=0;i<obj2.no_of_books;i++)
{
fscanf(fp,"%d",&(obj2.id));
fscanf(fp,"%s",&(obj2.title));
fscanf(fp,"%s",&(obj2.author));
fscanf(fp,"%s",&(obj2.publisher));
fscanf(fp,"%d",&(obj2.status));
add_book(root,obj2.id,obj2.title,obj2.author,obj2.author,obj2.status);
}
}
fclose(fp);
}
node* library::add_book (node*root,int d,char t[30],char a[30],char p[30],int s)
{
node*n=new node(d,t,a,p,s);
if(root== NULL){
cout<<"empty"<<endl;
root=n;
return root;
}
else
{
if(root->id >= n->id){
if(root->left==NULL)
{
root->left=n;
}
else
{
root->left=add_book(root->left,d,t,a,p,s);
}
return root;
}
else
{
if(root->right==NULL)
{
root->right=n;
}
else
{
root->right=add_book(root->right,d,t,a,p,s);
}
obj2.no_of_books+=1;
return root;
}
}
}