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

Recommended Answers

All 5 Replies

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.

If you don't know, how can we help you? We didn't write the program, you did.

//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.

All of the above. If you are really getting different output, you have some major problems in your code. Narrow down what the problem is and we can help you fix it.

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?

I'd use a linked list. Or, if your file is not going to be very large, just an array of your class/structure to hold each record in memory.

what other modules can be added this program in order to implement OOPS concepts?

TD :: TD(){
	setcolor(15);
	line (320-150,320-13,320+150,320-13);
	line (320-150,320+12,320+150,320+12);
	line (320-150,320-13,320-150,320+12);
	line (320+150,320+12,320+150,320-13);
	int s=30,w;

	gotoxy(20,23);
	cout<<"LOADING . . .";
	for (int x1=171,x2=171,y1=308,y2=331,y=1,S=0;x1<470;x1++,x2++,y++,S++){
		setcolor(3);
		line (x1,y1,x2,y2);
		w=(x1-169)/3;
		for (int i=34; i<=78; i++){
			gotoxy(i,23) ;
			cout <<" " ;
		}
		gotoxy(34,23); cout<<w<<"%";
		s=10;
		delay(s);
	}

wat are the header files which i should i include for the functions setcolor, gotoxy, delay.
also how can i do the basic graphics like rectangles and mouse using gcc compiler. this compiler says bgi programming is not supported. what else can i use to program graphics for my command line application.

guys kindly let me know how to do simple graphics like rectangle and circle similar to BGI programming using the GCC compiler.

Well that would depend entirely on your OS.

gcc in itself has no idea about graphics of any sort, it's all down to one or more libraries which interface to your specific OS.

A simple example might be to use ncurses, which is a nice simple console based API for generating user interfaces.

As for your 'OO' question,

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;

There is no point to redeclaring all the members of rec inside bintree. For example

typedef struct rec{
	char FName[10], LName[10], address[50];
	unsigned int TNum;
}REC;
typedef struct node{
	struct node* left,* right;
	REC rec;
}bintree;

For one thing, it means you can then assign all the members of rec in one simple assignment rather than one at a time.

2. Replace all those char arrays with std::string - this is C++.

> REC r;
> bt trec;
There is no point in maintaining r as a member of each tree. It should be a local variable in the function which reads the file.

http://en.wikipedia.org/wiki/Model-view-controller
Think about separating your data from the presentation of that data.
Adding all that screen handling to your TD class is just going to make the whole thing messy.

commented: good.. thanks buddy +1
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.