im building a telephone directory. and the code is given below.
i am using file system to store the record which i get from the user. and then i should be able to do searching, deleting, sorting and other default telephone dir functions. so far i have been able to get the input from the user and store it in a file, retrive the records from the file into a binary search tree and display the contents of the binary search tree.
there are some bugs in this program. bugs i guess are in the building the tree and displaying the tree. i would also like to display the menu after i call the inorder function which i tried but in vain.
//edit: i fixed how to display menu after the inorder function. :)
//the bug is.. wen i try to display from the tree, some records are displayed twice.
i used diffrent compilers (g++, bcc32, visual c++, turbo c++) and i got different outputs.
can anyone suggest a ansi standard compiler.
i am not sure if my code is an optimized one. using a binary tree a good idea? or sud i try to do direct searching and other functions from the file itself?
#include<iostream>
#include<fstream>
using namespace std;
typedef struct rec{
char FName[10], LName[10], address[50];
unsigned int TNum;
}REC;
typedef struct node{
struct node* left,* right;
char fname[10] ,lname[10];
unsigned int tnum;
char add[50];
}bintree;
typedef bintree* bt;
class TD{
REC r;
bt trec;
void wrec();
void disprec();
public:
TD();
void menu();
bt insert(bt, char*, char*, unsigned int, char*);
bt makenode(char*, char*, unsigned int, char*);
void inorder(bt);
~TD();
};
TD::TD(){
trec=NULL;
}
TD::~TD(){}
void TD :: menu(){
char i;
cout<<"Enter choice: ";
cin>>i;
switch(i){
case '1':
wrec();
break;
case '2':
disprec();
break;
case '3':{
inorder(trec);
menu();
break;
}
case '0':
exit(0);
}
}
void TD :: wrec(){
cout<<endl<<"FName: "; cin>>r.FName;
cout<<endl<<"LName: "; cin>>r.LName;
cout<<endl<<"TNumber: "; cin>>r.TNum;
cout<<endl<<"Address: ";fflush(stdin); gets(r.address);
ofstream o("td", ios::app);
o<<r.FName<<" "<<r.LName<<" "<<r.TNum<<" "<<r.address<<endl;
o.close();
menu();
}
void TD :: disprec(){
ifstream i("td");
while(!i.eof()){
i>>r.FName>>r.LName>>r.TNum; i.getline(r.address, 50);
if(i.eof()){i.close();menu();}
cout<<r.FName<<" "<<r.LName<<"\t"<<r.TNum<<"\t"<<r.address<<endl;
trec=insert(trec, r.FName, r.LName, r.TNum, r.address);
}
}
bt TD :: makenode(char* fname, char* lname, unsigned int tnum, char* add){
bt l;
l= new bintree[sizeof(bintree)];
strcpy(l->fname, fname);
strcpy(l->lname, lname);
l->tnum=tnum;
strcpy(l->add, add);
l->left=NULL;
l->right=NULL;
return l;
}
bt TD :: insert(bt t, char* fname, char* lname, unsigned int tnum, char* add){
if(!t)
return makenode(fname, lname, tnum, add);
if(strcmp(t->fname, fname)>0)
t->left=insert(t->left, fname, lname, tnum, add);
else
t->right=insert(t->right, fname, lname, tnum, add);
return t;
}
void TD :: inorder(bt t){
if(t){
inorder(t->left);
cout<<t->fname;
cout<<" "<<t->lname;
cout<<"\t"<<t->tnum;
cout<<"\t"<<t->add;
cout<<endl;
inorder(t->right);
}
}
int main(){
TD t;
t.menu();
return 0;
}