Home > C++ > C++ Project on Telephone Directory Management System

C++ Project on Telephone Directory Management System

Download Project
Share Facebook Twitter Orkut SMS

Telephone Billing System

/*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
*     TELEPHONE DIRECTORY MANAGEMENT SYSTEM     *
*                                               *
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include<fstream .h>
#include<conio .h>
#include<stdio .h>
#include<string .h>
#include<iomanip .h>
#include<dos .h>
 
struct node
{
unsigned long recno;
long int pin;
char name[20];
node *link;
};
node *first,*ptr1,*ptr2,*ptr,*temp;
 
class person
{
private:unsigned long record_id;
char person_name[20];
char res_address[30];
long int res_pincode;
long int res_phone_no1;
char occu_pation[10];
char company_name[20];
char off_address[20];
long int off_pincode;
long int off_phone_no1;
public:
person()
  {
    record_id=0;
    res_pincode=off_pincode=0;
    res_phone_no1=0;
    off_phone_no1=0;
  }
 
unsigned long get_rec_no();
void get_data();
void show_data();
void add_object();
void make_index();
void sort_name();
void sort_recno();
void sort_pin();
void show_object();
void sort_print();
void del_object();
void modify_object();
void search_object();
void sort_object();
void reports();
}pers;
unsigned long person::get_rec_no()
{
  int found=0;
  unsigned long rec_no,temp_recno;
  struct node1
  {
    unsigned long recno;
    node1 *link;
    };
 node1 *start,*ptr,*ptr1,*ptr2;
 fstream infile;
 infile.open("phoneno.dat",ios::in);
 infile.seekg(0,ios::end);
 int n=infile.tellg();
 infile.close();
 if(n==0)
      rec_no=1;
 else
 {
  infile.open("phoneno.dat",ios::in);
  start=ptr=new node1;
  infile.seekg(0,ios::beg);
  infile.read((char*)&pers,sizeof(pers));
  while(!infile.eof())
  {
   ptr->recno=record_id;
   ptr->link=new node1;
   ptr=ptr->link;
   infile.read((char*)&pers,sizeof(pers));
   }
   ptr->link=NULL;
   ptr1=start;
   while(ptr1->link!=NULL)
   {
    ptr2=ptr1->link;
    while(ptr2!=NULL)
    {
    if(ptr2->recno<ptr1 ->recno)
    {
      temp_recno=ptr2->recno;
 
      ptr2->recno=ptr1->recno;
      ptr1->recno=temp_recno;
      }
   ptr2=ptr2->link;
   }
   ptr2=ptr1->link;
   ptr1=ptr2;
   }
   ptr1=start;
   while(ptr1!=NULL && found!=1)
   {
    ptr2=ptr1;
    ptr1=ptr1->link;
    if((ptr2->recno)+1!=ptr1->recno)
    {
     rec_no=(ptr2->recno)+1;
     found=1;
     }
    }
    if(found!=1)rec_no=(ptr2->recno)+1;
    ptr=start;
    while(start!=NULL)
    {
     start=start->link;
     delete ptr;
     }
    }
    return rec_no;
    }
  void person::get_data()
  {
    clrscr();
    record_id=pers.get_rec_no();
    gotoxy(5,7);
    cout< <"ENTER THE NAME--------------->";
    gets(person_name);
    gotoxy(5,8);
    cout< <"RESIDENTIAL ADDRESS------------------>";
    gets(res_address);
    gotoxy(5,9);
    cout< <"ENTER RESIDENTIAL PINCODE------------>";
    cin>>res_pincode;
    gotoxy(5,10);
    cout< <"ENTER RESIDENTIAL PHONE NO----------->";
    cin>>res_phone_no1;
    gotoxy(5,11);
    cout< <"ENTER THE OCCUPATION---------------->";
    gets(occu_pation);
    gotoxy(5,12);
    cout< <"ENTER COMPANY'S NAME--------------->";
    gets(company_name);
    gotoxy(5,13);
    cout< <"ENTER COMPANY'S ADDRESS------------>";
    gets(off_address);
    gotoxy(5,14);
    cout< <"ENTER OFFICE PINCODE-------------->";
    cin>>off_pincode;
    gotoxy(5,15);
    cout< <"ENTER OFFICE PHONE NO-------------->";
    cin>>off_phone_no1;
    gotoxy(5,16);
    clrscr();
    }
  void person::show_data()
   {
     clrscr();
     gotoxy(5,2);
     cout< <"RECORD NUMBER----------------------->"< <record_id;
     gotoxy(5,3);
     cout<<"NAME-------------------------------->"< <person_name;
     gotoxy(5,4);
     cout<<"RESIDENTIAL ADDERSS------------->"< <res_address;
     gotoxy(5,5);
     cout<<"RESIDENTIAL PINCODE---------->"< <res_pincode;
     gotoxy(5,6);
     cout<<"RESIDENTIAL PHONE NO--------->"< <res_phone_no1;
     gotoxy(5,7);
     cout<<"OCCUPATION----------------->"< <occu_pation;
     gotoxy(5,8);
     cout<<"COMPANY's NAME-------------->"< <company_name;
     gotoxy(5,9);
     cout<<"COMPANY's ADDRESS--------->"< <off_address;
     gotoxy(5,10);
     cout<<"OFFICE PINCODE----------->"< <off_pincode;
     gotoxy(5,11);
     cout<<"OFFICE PHONE NO---------->"< <off_phone_no1;
     gotoxy(10,20);
     cout<<"PRESS ANY KEY TO CONTINUE";
     getch();
    }
 
    //This function creates a master file .
  void person::add_object()
   {
    fstream file;
    char choice='y';
    while(choice=='y')
    {
     file.open("phoneno.dat",ios::app);
     get_data();
     file.write((char*)&pers,sizeof(pers));
     file.flush();
     file.close();
     clrscr();
     gotoxy(10,11);
     cout<<"ANY MORE RECORDS TO BE READ<<Y/N>>-------------->";
     cin>>choice;
     }
    }
   void person::del_object()
   {
    fstream infile,outfile;
    unsigned long rec_no;
    clrscr();
    cout< <"ENTER THE RECORD ID.NO.TO BE DELETED--------->";
    cin>>rec_no;
    infile.open("phoneno.dat",ios::in);
    infile.seekg(0);
    outfile.open("tempno.dat",ios::app);
    infile.read((char*)&pers,sizeof(pers));
    while(!infile.eof())
    {
     if(pers.record_id!=rec_no)
     outfile.write((char*)&pers,sizeof(pers));
     outfile.flush();
     infile.read((char*)&pers,sizeof(pers));
     }
   infile.close();
   outfile.close();
   remove("phoneno.dat");
   rename("tempno.dat","phoneno.dat");
   }
   void person::search_object()
    {
     fstream infile;
     int search_choice;
     unsigned long rec_no;
     long int phno;
     char name[20];
     do
     {
     clrscr();
     int counter=0;
     gotoxy(22,7);
     cout< <"    SEARCH MENU    ";
     gotoxy(22,9);
     cout<<"------------------";
     gotoxy(22,11);
     cout<<"RECORD ID NO.           ..1";
     gotoxy(22,12);
     cout<<"NAME                    ..2";
     gotoxy(22,13);
     cout<<"PHONE NO.               ..3";
     gotoxy(22,14);
     cout<<"EXIT SEARCH MENU         ..4";
     gotoxy(1,20);
     cout<<"ENTER THE FIELD CHOICE ACCORDING TO WHICH A RECORD";
     cout<<"IS TO BE SEARCHED--------->";
     cin>>search_choice;
     switch(search_choice)
     {
     case 1:clrscr();
	    cout< <"\n ENTER THE RECORD ID.NO. TO BE SEARCHED----->";
	    cin>>rec_no;
	    infile.open("phoneno.dat",ios::in);
	    infile.seekg(0,ios::beg);
	    infile.read((char*)&pers,sizeof(pers));
	    while(!infile.eof())
	    {
	     if(pers.record_id==rec_no)
	     {
	      counter++;
	      pers.show_data();
	      }
	   infile.read((char*)&pers,sizeof(pers));
	  }
	infile.close();
	gotoxy(20,24);
	cout< <"RECORDS FOUND="<<counter;
	getch();
	break;
    case 2:clrscr();
	  cout<<"\n ENTER THE NAME TO BE SEARCHED---------->";
	  gets(name);
	  infile.open("phoneno.dat",ios::in);
	  infile.seekg(0,ios::beg);
	  infile.read((char*)&pers,sizeof(pers));
	  while(!infile.eof())
	  {
	   if(strcmpi(pers.person_name,name)==0)
	   {
	    counter++;
	    pers.show_data();
	   }
	   infile.read((char*)&pers,sizeof(pers));
	  }
	infile.close();
	gotoxy(20,24);
	cout< <"RECORDS FOUND="<<counter;
	getch();
	break;
   case 3:clrscr();
	cout<<"\nENTER THE PHONE NO.TO BE SEARCHED-------->";
	cin>>phno;
	infile.open("phoneno.dat",ios::in);
	infile.seekg(0,ios::beg);
	infile.read((char*)&pers,sizeof(pers));
	while(!infile.eof())
	{
	 if(pers.res_phone_no1==phno||pers.off_phone_no1==phno)
	 {
	  counter++;
	  pers.show_data();
	 }
	 infile.read((char*)&pers,sizeof(pers));
	}
	infile.close();
	gotoxy(20,24);
	cout< <"RECORDS FOUND="<<counter;
	getch();
	break;
   case 4:clrscr();
	gotoxy(22,15);
	cout<<"YOU HAVE ENDED THE SEARCH SESSION";
	gotoxy(27,18);
	cout<<"THANK YOU!";
	delay(700);
	break;
	}
       }
       while(search_choice!=4);
       }
  void person::modify_object()
   {
    fstream file;
    unsigned long code;
    int modify_choice;
    do
    {
    clrscr();
    gotoxy(22,7);
    cout<<"     MODIFY MENU     ";
    gotoxy(22,8);
    cout<<"---------------";
    gotoxy(22,10);
    cout<<"RESIDENTIAL INFORMATON  ..1";
    gotoxy(22,11);
    cout<<"OFFICIAL INFORMATION    ..2";
    gotoxy(22,12);
    cout<<"EXIT                    ..3";
    gotoxy(22,13);
    cout<<"SELECT THE INFORMATION TO BE CHANGED : ";
    gotoxy(22,14);
    cout<<"ENTER YOUR CHOICE----->";
    cin>>modify_choice;
    if(modify_choice!=3)
    {
     clrscr();
     gotoxy(10,15);
     cout< <"ENTER THE RECORD NO. OF THE PERSON---->";
     cin>>code;
     file.open("phoneno.dat",ios::in|ios::out);
     file.seekg(0,ios::beg);
     file.read((char*)&pers,sizeof(pers));
     int n=file.tellg();
     while(!file.eof())
     {
      if(pers.record_id==code)
      {
      switch(modify_choice)
      {
      case 1:clrscr();
	  cout< <"***** The Details of "<<pers.person_name;
	  cout<<"\n\nThe old Address is :-"<<pers.res_address;
	  cout<<"\nENTER NEW RESIDENTIAL ADDRESS-------->";
	  gets(pers.res_address);
	  cout< <"The old Pin code is :-"<<pers.res_pincode;
	  cout<<"\nENTER NEW PINCODE------------>";
	  cin>>pers.res_pincode;
	  cout< <"The old Phone No.is :-"<<pers.res_phone_no1;
	  cout<<"\nENTER NEW RESIDENTIAL PHONE NO-------->";
	  cin>>pers.res_phone_no1;
	  file.seekg(n-sizeof(pers));
	  file.write((char*)&pers,sizeof(pers));
	  file.flush();
	  break;
      case 2:clrscr();
	  cout< <"\nENTER THE OCCUPATION----------->";
	  gets(pers.occu_pation);
	  cout< <"\nENTER NEW COMPANY'S NAME------->";
	  gets(pers.company_name);
	  cout< <"\nENTER NEW OFFICE ADDRESS-------->";
	  gets(pers.off_address);
	  cout< <"\nENTER NEW OFFICE PIN CODE------>";
	  cin>>pers.off_pincode;
	  cout< <"\nENTER NEW OFFICE PHONE NO------->";
	  cin>>pers.off_phone_no1;
	  file.seekg(n-sizeof(pers));
	  file.write((char*)&pers,sizeof(pers));
	  file.flush();
	  break;
	    }
	   }
	   file.read((char*)&pers,sizeof(pers));
	   n=file.tellg();
	   }
	   file.close();
	   }
	  }
    while(modify_choice!=3);
    clrscr();
    gotoxy(22,10);
    cout< <"YOU ENDED MODIFY INFORTION SESSION";
    gotoxy(30,13);
    cout<<"THANK YOU";
    delay(700);
    }
  void ex_change()
  {
  temp=new node;
  temp->link=NULL;
  temp->recno=ptr2->recno;
  temp->pin=ptr2->pin;
  strcpy(temp->name,ptr2->name);
  ptr2->recno=ptr1->recno;
  ptr2->pin=ptr1->pin;
  strcpy(ptr2->name,ptr1->name);
  ptr1->recno=temp->recno;
  ptr1->pin=temp->pin;
  strcpy(ptr1->name,temp->name);
  delete temp;
  }
  void person::make_index()
  {
  fstream infile;
  first=new node;
  ptr=first;
  infile.open("phoneno.dat",ios::in);
  infile.seekg(0,ios::beg);
  infile.read((char*)&pers,sizeof(pers));
  while(!infile.eof())
  {
   ptr->recno=record_id;
   ptr->pin=res_pincode;
   strcpy(ptr->name,pers.person_name);
   ptr->link=new node;
   ptr=ptr->link;
   infile.read((char*)&pers,sizeof(pers));
  }
  ptr->link=NULL;
  infile.close();
  }
  void person::sort_name()
  {
   pers.make_index();
   ptr1=first;
   while(ptr1->link!=NULL)
   {
    ptr2=ptr1->link;
    while(ptr2->link!=NULL)
    {
     if(strcmpi(ptr2->name,ptr1->name)&lt;0)
     ex_change();
     ptr2=ptr2->link;
    }
    ptr2=ptr1->link;
    ptr1=ptr2;
    }
   }
   void person::sort_recno()
   {
    pers.make_index();
    ptr1=first;
    while(ptr1->link!=NULL)
    {
     ptr2=ptr->link;
     while(ptr2!=NULL)
     {
       if(ptr2->recno</ptr1><ptr1 ->recno)
       ex_change();
       ptr2=ptr2->link;
      }
      ptr2=ptr1->link;
      ptr1=ptr2;
      }
   }
   void person::sort_pin()
   {
    pers.make_index();
    ptr1=first;
    while(ptr1->link!=NULL)
    {
     ptr2=ptr1->link;
     while(ptr2!=NULL)
     {
      if(ptr2->pin</ptr1><ptr1 ->pin)
      ex_change();
      ptr2=ptr2->link;
     }
     ptr2=ptr1->link;
     ptr1=ptr2;
    }
   }
   void person::sort_print()
   {
   fstream infile;
   ptr=first;
   while(ptr!=NULL)
   {
    infile.open("phoneno.dat",ios::in);
    infile.seekg(0,ios::beg);
    infile.read((char*)&pers,sizeof(pers));
    while(!infile.eof())
    {
     if(ptr->recno==pers.record_id)
      {
      pers.show_data();
      infile.seekg(0,ios::end);
      }
    infile.read((char*)&pers,sizeof(pers));
    }
    infile.close();
    ptr=ptr->link;
    }
   }
   void del_index()
   {
   while(first!=NULL)
   {
    ptr=first;
    first=first->link;
    delete ptr;
    }
   }
   void person::sort_object()
   {
    int sort_choice;
    do
    {
    clrscr();
    gotoxy(22,7);
    cout< <"    SORT MENU    ";
    gotoxy(22,8);
    cout<<"-------------------";
    gotoxy(22,10);
    cout<<"SORTED RECORDS NOS.   ..1";
    gotoxy(22,11);
    cout<<"SORTED NAMES          ..2";
    gotoxy(22,12);
    cout<<"SORTED PINCODES NOS.  ..3";
    gotoxy(22,13);
    cout<<"EXIT SORT MENU        ..4";
    gotoxy(22,20);
    cout<<"ENTER YOUR CHOICE NO.----->";
    cin>>sort_choice;
    switch(sort_choice)
    {
    case 1:clrscr();
	  pers.sort_recno();
	  pers.sort_print();
	  del_index();
	  delay(200);
	  break;
    case 2:clrscr();
	  pers.sort_name();
	  pers.sort_print();
	  del_index();
	  delay(200);
	  break;
    case 3:clrscr();
	  pers.sort_pin();
	  pers.sort_print();
	  del_index();
	  delay(200);
	  break;
    case 4:clrscr();
	  gotoxy(22,10);
	  cout< <"YOU ENDED THE SORTED SESSION";
	  gotoxy(27,13);
	  cout<<"THANK YOU!";
	  delay(700);
	  break;
     }
     }
     while(sort_choice!=4);
     }
   void person::reports()
   {
   fstream infile;
   int report_choice;
   do
   {
    clrscr();
    gotoxy(22,7);
    cout<<"   REPORT MENU    ";
    gotoxy(22,8);
    cout<<"------------------";
    gotoxy(5,10);
    cout<<"SORTED LIST OF NAMES WITH RESIDENCE AND OFFICE PHONE NOS. ..1";
    gotoxy(5,11);
    cout<<"LIST OF NAMES WITH THIER OFFICE DETAILS.   ..2";
    gotoxy(5,12);
    cout<<"LIST OF NAMES WITH THEIR RECORD NOS.AND RESIDENTIAL ADDRESS ..3";
    gotoxy(5,13);
    cout<<"EXIT REPORT SESSION     ..4";
    gotoxy(5,14);
    cout<<"ENTER YOUR CHOICE NO.------>";
    cin>>report_choice;
    switch(report_choice)
	{
	case 1:clrscr();
	       {
               cout< <"\n NAME    RESIDENCE PHONE NO.   OFFICE PHONE NO.";
               cout<<"\n-----------------------------------------------";
	       pers.sort_name();
	       ptr=first;
	       while(ptr!=NULL)
			{
			infile.open("phoneno.dat",ios::in);
			infile.seekg(0,ios::beg);
			infile.read((char*)&pers,sizeof(pers));
			while(!infile.eof())
			    {
			    if(ptr->recno==pers.record_id)
			      {
			      cout< <"\n";
			      cout<<setiosflags(ios::left)
<<setw(28)<<pers.person_name<<setw(30)<<pers.res_phone_no1<<setw(30)        
			      <<pers.off_phone_no1;
			      infile.seekg(0,ios::end);
			      }
			    infile.read((char*)&pers,sizeof(pers));
			    }
			infile.close();
			ptr=ptr->link;
			}
	       del_index();
	       gotoxy(20,20);
	       cout< <"PRESS ANY KEY TO CONTINUE";
	       getch();
	       break;
	       }
       case 2:clrscr();
       infile.open("phoneno.dat",ios::in);
       infile.seekg(0,ios::beg);
       infile.read((char*)&pers,sizeof(pers));
       while(!infile.eof())
       {
	gotoxy(10,5);
	cout<<"NAME------------->"< <pers.person_name;
	gotoxy(10,6);
	cout<<"COMPNAY'S NAME--->"< <pers.company_name;
	gotoxy(10,7);
	cout<<"COMPANY'S ADDESS-->"< <pers.off_address;
	gotoxy(10,8);
	cout<<"OFFICE PHONE NO------>"< <pers.off_phone_no1;
	gotoxy(10,10);
	cout<<"PRESS ANY KEY TO CONTINUE";
	getch();
	infile.read((char*)&pers,sizeof(pers));
	clrscr();
	}
	infile.close();
	break;
    case 3:clrscr();
cout<<"\nREC NO.      NAME            RES.ADDRESS        RES PHONE NO.";
cout<<"\n-------------------------------------------------------------";
    infile.open("phoneno.dat",ios::in);
    infile.seekg(0,ios::beg);
    infile.read((char*)&pers,sizeof(pers));
    while(!infile.eof())
    {
     cout<<"\n";
     cout<<setiosflags(ios::left)
	 <<setw(15)<<pers.record_id
	 <<setw(15)<<pers.person_name<<
	 setw(18)<<pers.res_address<<
	 setw(35)<<res_phone_no1;
	cout<<setw(55)<<" ";
	 infile.read((char*)&pers,sizeof(pers));
	 }
	 infile.close();
	 gotoxy(20,40);
	 cout<<"\nPRESS ANY KEY TO CONTINUE";
	 getch();
	 break;
	 case 4:clrscr();
	 gotoxy(22,10);
	 cout<<"YOU HAVE ENDED REPORT SESSION";
	 gotoxy(27,30);
	 cout<<"THANK YOU";
	 delay(700);
	 break;
	 }
	}
	while(report_choice!=4);
       }
	void main()
	{
	 int main_choice;
	 do
	 {
	 clrscr();
	 gotoxy(22,7);
	 cout<<"    MAIN MENU       ";
	 gotoxy(25,11);
	 cout<<"  ADD A NEW RECORD   ..1";
	 gotoxy(25,12);
	 cout<<"  DELETE A RECORD    ..2";
	 gotoxy(25,13);
	 cout<<"  MODIFY A RECORD    ..3";
	 gotoxy(25,14);
	 cout<<"  SEARCH A RECORD    ..4";
	 gotoxy(25,15);
	 cout<<"  SORTED INFORMATION  ..5";
	 gotoxy(25,16);
	 cout<<"  REOPORTS           ..6";
	 gotoxy(25,17);
	 cout<<"  EXIT              ..7";
	 gotoxy(25,20);
	 cout<<"ENTER YOUR CHOICE NO.--------->";
	 cin>>main_choice;
 
	 switch(main_choice)
	 {
	  case 1:pers.add_object();
		break;
	  case 2:pers.del_object();
		break;
	  case 3:pers.modify_object();
		break;
	  case 4:pers.search_object();
		break;
	  case 5:pers.sort_object();
	       break;
	  case 6:pers.reports();
 
		break;
	  case 7:clrscr();
	       gotoxy(22,10);
	  cout< <"YOU HAVE ENDED THE SESSION";
	  gotoxy(27,13);
	  cout<<"THANK YOU";
	  delay(1000);
	  break;
	  }
	 }
	 while(main_choice!=7);
	 }
  1. No comments yet.
  1. No trackbacks yet.