Telephone Directory - Source Code

Posted on Sunday, October 15, 2006 by Arun Basil Lal

This is a program written by myself for the school papers. Its a telephone Directory and personal Address Book. You can store details, search for them, modify them and delete them. It also has a help associated with it. Coded in Turbo C++ it need to be compiled. It uses graphics.h and need some add-onn files to run as a stand alone program. But you can run it within C++ without much problem. The required files are : BOLD.CHR, EGAVGA.BGI, GOTH.CHR, LITT.CHR, SANS.CHR, SIMP.CHR, TRIP.CHR. These files can be found in the C++ installed directory.
Have fun using this code. Please send me your comments and suggestions to arunbasillal@gmail.com or post as comments on this site.

[ begin of code ]

---------------------------------------------------------------------------

/************************************************************/
/* -=[ Magnum's ]=- */
/* Telephone Directory And Address Book */
/* Coding : Arun Basil Lal */
/* Produced By : Magnum Software Solutions */
/* (C) 2005 . All Rights Reserved */
/************************************************************/

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<process.h>
#include<dos.h>
#include<graphics.h>
#include<stdio.h>
#include< string.h>

struct record
{
int slno;
char title[10],name[100];
char hname[100],city[100],pin[10],state[100],country[100];
char phone[100],mobile[100];
char email[100];
char notes[100];
}rec;

void splash();
void loading();
void format();
void input();
void modify();
void search();
int searchname(char []);
void display();
int del();
void help();
void exit();
void credits();

void main()
{
char ch;
int gdriver = EGA, gmode = EGAHI;
initgraph(&gdriver, &gmode, ""); //Initialise graphic devices
splash();
loading();

label:
cleardevice(); // Clears The Screen
setbkcolor(1); // Sets back colour
gotoxy(30,8);
cout<<"1. Input Record \n";
cout<<"\t\t\t 2. Modify Record \n";
cout<<"\t\t\t 3. Search \n\t\t\t 4. Delete Record \n";
cout<<"\n\t\t\t 5. Help Me ! \n\n\t\t\t 6. Credits \n";
cout<<"\t\t\t 7. Exit";
gotoxy(1,21);
cout<<"\t\t\t Enter Choice : ";
format();
rectangle(90,70,getmaxx()-100,getmaxy()-100);
rectangle(90,getmaxy()-80,getmaxx()-100,getmaxy()-50);
ch=getch();
switch(ch)
{
case '1':
input();
break;
case '2':
modify();
break;
case '3':
search();
break;
case '4':
del();
break;
case '5':
help();
break;
case '6':
credits();
break;
case '7':
exit();
break;
default:
cout<<"Invalid Entry\a";
getch();
}
goto label;
}

void format()
{
rectangle(1,1,getmaxx(),getmaxy()); // Draws Border
rectangle(2,2,getmaxx()-1,getmaxy()-1); // Double Walled Border
int style, midx;
midx = getmaxx() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
style=TRIPLEX_FONT;
int size = 3;
settextstyle(style,HORIZ_DIR, size);
outtextxy(midx, 10,"--=[ Magnum's ]=-- ");
outtextxy(midx,35," Telephone Directory & Address Book " );
size = 5,style=SMALL_FONT;
settextstyle(style, HORIZ_DIR, size);
outtextxy(midx+130, 330," (c) 2006 (r) Magnum Software Solutions ");
}

void splash()
{
setbkcolor(4); // Sets back colour
cleardevice(); // Clears The Screen
rectangle(1,1,getmaxx(),getmaxy()); // Draws Border
rectangle(2,2,getmaxx()-1,getmaxy()-1); // Double Walled Border
int style, midx, midy,size;
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
style=TRIPLEX_FONT, size = 4;
settextstyle(style,HORIZ_DIR, size);
outtextxy(midx, 120,"--=[ Magnum's ]=-- ");
outtextxy(midx,midy," Telephone Directory & Address Book " );
size = 5,style=SMALL_FONT;
settextstyle(style, HORIZ_DIR, size);
outtextxy(midx+130, 330," (c) 2006 (r) Magnum Software Solutions ");
delay(5000); // Sets Delay Time
}

void loading()
{
cleardevice();
setbkcolor(8);
for(int i=100;i<=getmaxx()-60;i=i+40)
{
format();
settextstyle(GOTHIC_FONT,HORIZ_DIR, 4);
outtextxy(200, 120," Loading ... ");
rectangle(90,getmaxy()-80,i,getmaxy()-50);
setfillstyle(SOLID_FILL,4);
floodfill(91,getmaxy()-79,getmaxcolor());
delay(500);
if(i<=getmaxx()-100)
{
cleardevice();
}
}
delay(100);
}

void input()
{
char ch;
int count=0;
cleardevice();
setbkcolor(0);
format();
ifstream fin("teledat.dat",ios::binary);
if(!fin)
{
count=0;
}
else
{
fin.seekg (0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
count++;
}
}
fin.close();
rec.slno=count;
ofstream fout("teledat.dat",ios::binaryios::app);
if(!fout)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be opened ";
cout<<"\n\t\t\t File Corrupted :: Program Failure ";
cout<<"\n\t\t\t Error Code : 80 ";
cout<<"\n\t\t\t Aborting...";
format();
getch();
exit(0);
}
label:
gotoxy(20,5);
cout<<" Enter Details For Contact No : "<<rec.slno+1;
rec.slno++;
cout<<"\n\t\t ( Press \' Enter \' for blank fields )";
format();
cout<<"\n\n>\t\t\t Enter Title ( Mr/Mrs ) : ";
gets(rec.title);
cout<<">\t\t\t Enter Name : ";
gets(rec.name);
cout<<"\n>\t\t\t Enter House Name : ";
gets(rec.hname);
cout<<">\t\t\t Enter city : ";
gets(rec.city);
cout<<">\t\t\t Enter Pin / Zip Code : ";
gets(rec.pin);
cout<<">\t\t\t Enter State : ";
gets(rec.state);
cout<<">\t\t\t Enter Country : ";
gets(rec.country);
cout<<"\n>\t\t\t Enter Phone No. : ";
gets(rec.phone);
cout<<">\t\t\t Enter Mobile No. : ";
gets(rec.mobile);
cout<<"\n>\t\t\t Enter Email id('s) \n>\t\t\t(separate id's by ,) : ";
gets(rec.email);
cout<<">\t\t\t Enter Notes : ";
gets(rec.notes);
fout.write((char*)&rec,sizeof(rec));
getch();
cleardevice();
format();
gotoxy(20,6);
cout<<"Do you wish to continue ( y / n ) : ";
ch=getch();
if(ch=='y'ch=='Y')
goto label;
fout.close();
}

void modify()
{
label:
int count=0,keysl;
char key[100],ch;
cleardevice();
setbkcolor(0);
format();
gotoxy(20,8);
cout<<"\n>\t\t Enter the Name of the Contact : ";
cin>>key;
keysl=searchname(key);
if(keysl==0)
{
cout<<"\n>\t\t The contact was not found ";
}
else
{
ifstream fin("teledat.dat",ios::appios::binary);
if(!fin)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be opened ";
cout<<"\n\t\t\t File Corrupted :: Program Failure ";
cout<<"\n\t\t\t Error Code : 87 ";
cout<<"\n\t\t\t Aborting Modify... ";
format();
getch();
goto end;
}
fin.seekg(0);
fin.read((char*)&rec,sizeof(rec));
fin.seekg(0);
while(rec.slno!=keysl)
{
count++;
fin.read((char*)&rec,sizeof(rec));
}
cleardevice();
setbkcolor(0);
format();
gotoxy(20,5);
cout<<" Enter Details For Contact No : "<<rec.slno;
cout<<"\n\t\t ( Press \' Enter \' for blank fields )";
format();
cout<<"\n\n>\t\t\t Enter Title ( Mr/Mrs ) : ";
gets(rec.title);
cout<<">\t\t\t Enter Name : ";
gets(rec.name);
cout<<"\n>\t\t\t Enter House Name : ";
gets(rec.hname);
cout<<">\t\t\t Enter city : ";
gets(rec.city);
cout<<">\t\t\t Enter Pin / Zip Code : ";
gets(rec.pin);
cout<<">\t\t\t Enter State : ";
gets(rec.state);
cout<<">\t\t\t Enter Country : ";
gets(rec.country);
cout<<"\n>\t\t\t Enter Phone No. : ";
gets(rec.phone);
cout<<">\t\t\t Enter Mobile No. : ";
gets(rec.mobile);
cout<<"\n>\t\t\t Enter Email id('s) \n>\t\t\t(separate id's by ,) : ";
gets(rec.email);
cout<<">\t\t\t Enter Notes : ";
gets(rec.notes);
display();
fin.close();
ofstream fout("teledat.dat",ios::appios::binary);
if(!fout)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be opened ";
cout<<"\n\t\t\t File Corrupted :: Program Failure ";
cout<<"\n\t\t\t Error Code : 80 ";
cout<<"\n\t\t\t Aborting...";
format();
getch();
goto end;
}
fout.seekp((count)*sizeof(rec));
fout.write((char*)&rec,sizeof(rec));
fout.close();
}
cout<<"\n>\t\t Do You Wish to continue ? ( y / n ) : ";
ch=getch();
if(ch=='y'ch=='Y')
{
goto label;
}
end:
}

int del()
{
int keysl;
cleardevice();
setbkcolor(0);
format();
char key[100],ch;
outtextxy(getmaxx()/2,((getmaxy()/2)-100),"* Delete Contact *");
gotoxy(20,8);
cout<<"Do you Really wish to delete ? ( y / n ) : ";
ch=getch();
if(ch=='n'ch=='N')
{
return(0);
}
label:
cleardevice();
format();
gotoxy(20,8);
ifstream fin("teledat.dat",ios::binary);
if(!fin)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be opened ";
cout<<"\n\t\t\t File Corrupted :: Program Failure ";
cout<<"\n\t\t\t Error Code : 90 ";
cout<<"\n\t\t\t Aborting Delete... ";
format();
getch();
return(0);
}
ofstream fout("temptel.dat",ios::binary);
if(!fout)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File temptel.dat cannot be created ";
cout<<"\n\t\t\t File Corrupted :: Program Failure ";
cout<<"\n\t\t\t Error Code : 95 ";
cout<<"\n\t\t\t Aborting...";
format();
getch();
exit(0);
}
fin.seekg(0);
cout<<"\n\n>\t\t\tEnter the name of the contact : ";
cin>>key;
keysl=searchname(key);
if(keysl==0)
{
cout<<"\n>\t\t Do you wish to continue ? ( y/ n ) : ";
ch=getch();
if(ch=='y'ch=='Y')
{
goto label;
}
else
return(0);
}
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if(rec.slno!=keysl)
{
fout.write((char*)&rec,sizeof(rec));
}
}
fout.close();
fin.close();
ofstream fout1("teledat.dat",ios::binary);
if(!fout1)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be created ";
cout<<"\n\t\t\t File Corrupted :: Program Failure ";
cout<<"\n\t\t\t Error Code : 95 ";
cout<<"\n\t\t\t Aborting...";
format();
getch();
exit(0);
}
ifstream fin1("temptel.dat",ios::binary);
if(!fin1)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be opened ";
cout<<"\n\t\t\t File Corrupted :: Program Failure ";
cout<<"\n\t\t\t Error Code : 90 ";
cout<<"\n\t\t\t Aborting...";
format();
getch();
exit(0);
}
fin1.seekg(0);
while(fin1)
{
fin1.read((char*)&rec,sizeof(rec));
fout1.write((char*)&rec,sizeof(rec));
}
fin1.close();
fout1.close();
return(0);
}

void search()
{
char ch,key[100];
int opt;
label:
int fl=0;
ifstream fin("teledat.dat",ios::binary);
if(!fin)
{
cleardevice();
setbkcolor(0);
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be opened ";
cout<<"\n\t\t\t File Corrupted or No Data To Read ";
cout<<"\n\t\t\t Program Failure";
cout<<"\n\t\t\t Error Code : 81 ";
cout<<"\n\t\t\t Aborting Search...";
format();
getch();
goto end;
}
opt=1;
cleardevice();
setbkcolor(0);
format();
outtextxy(getmaxx()/2,(getmaxy()/2)-100,"* Search Options *");
outtextxy(getmaxx()/2,86,"[ Keywords Are Case Sensitive ]");
gotoxy(20,8);
cout<<"Search With : \n>\t\t\t 1. Title \n>\t\t\t 2. Name ";
cout<<"\n>\t\t\t 3. House Name \n>\t\t\t 4. City";
cout<<"\n>\t\t\t 5. State \n>\t\t\t 6. Country";
cout<<"\n>\t\t\t 7. Phone No. \n>\t\t\t 8. Mobile No.";
cout<<"\n\n>\t\t\t 9. Exit Search ";
cout<<"\n\n>\t\t Enter Your Choice : ";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\n>\t\t\t Enter Title : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.title))==0)
{
display();
opt=0;
}
}
fin.close();
break;
case '2':
cout<<"\n\n>\t\t\t Enter Name : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.name))==0)
{
display();
opt=0;
}
}
fin.close ();
break;
case '3':
cout<<"\n\n>\t\t\t Enter House Name : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.hname ))==0)
{
display();
opt=0;
}
}
fin.close();
break;
case '4':
cout<<"\n\n>\t\t\t Enter City : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read ((char*)&rec,sizeof(rec));
if((strcmp(key,rec.city))==0)
{
display();
opt=0;
}
}
fin.close();
break;
case '5':
cout<<"\n\n>\t\t\t Enter State : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.state))==0)
{
display();
opt=0;
}
}
fin.close();
break;
case '6':
cout<<"\n\n>\t\t\t Enter Country : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.country))==0)
{
display();
opt=0;
}
}
fin.close();
break;
case '7':
cout<<"\n\n>\t\t\t Phone No. : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.phone))==0)
{
display();
opt=0;
}
}
fin.close();
break;
case '8':
cout<<"\n\n>\t\t\t Enter Mobile No : ";
cin>>key;
fin.seekg(0);
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.mobile))==0)
{
display();
opt=0;
}
}
fin.close();
break;
case '9':
{
fin.close();
goto end;
}
default:
{
cout<<" Invalid Entry \a";
fl=2;
fin.close();
}
}
if(opt!=0 && fl!=2)
cout<<">\t\t Search Complete :: No Record Found ";
if(opt==0 && fl!=2)
cout<<"\n>\t\t\t Search Complete ";
getch();
goto label;
end:
}

int searchname(char key[100])
{
char ch;
int sl=-1;
ifstream fin("teledat.dat",ios::binary);
if(!fin)
{
gotoxy(20,5);
cout<<"\n\n\t\t\t File teledat.dat cannot be opened ";
cout<<"\n\t\t\t File Corrupted or No Data To Read ";
cout<<"\n\t\t\t Program Failure";
cout<<"\n\t\t\t Error Code : 81 ";
cout<<"\n\t\t\t Aborting...";
format();
getch();
exit(0);
}
while(fin)
{
fin.read((char*)&rec,sizeof(rec));
if((strcmp(key,rec.name))==0)
{
display();
cout<<"\n\n>\t\t Is this the required record ? ( y / n ) : ";
ch=getch();
if(ch=='y'ch=='Y')
{
sl=rec.slno;
fin.close();
return(sl);
}
}
}
if(sl==-1)
{
cout<<"\n\n>\t\t Search Complete :: No Match Found";
}
return(0);
}

void display()
{
cleardevice();
format();
gotoxy(20,6);
cout<<"Details of Entry No : "<<rec.slno;
cout<<"\n\n>\t\t\t Name : "<<rec.title<<" "<< rec.name;
cout<<"\n>\t\t\t House Name : "<<rec.hname;
cout<<"\n>\t\t\t City : "<<rec.city<<"\n>\t\t\t Pin : "<< rec.pin;
cout<<"\n>\t\t\t State : "<<rec.state;
cout<<"\n>\t\t\t Country : "<<rec.country;
cout<<"\n>\t\t\t Phone No. : "<<rec.phone;
cout<<"\n>\t\t\t Mobile No. : "<<rec.mobile;
cout<<"\n>\t\t\t Email id('s) : "<<rec.email;
cout<<"\n>\t\t\t Notes : "<<rec.notes<<endl;
getch();
}

void help()
{
int f=0;
do
{
cleardevice();
format();
setbkcolor(8);
char ch;
outtextxy(getmaxx()/2,(getmaxy()/2)-100,"* Help Options *");
gotoxy(20,8);
cout<<"\n>\t\t\t 1. General Tips ";
cout<<"\n>\t\t\t 2. Menu Help \n>\t\t\t 3. Trouble Shoot ";
cout<<"\n\n>\t\t\t 4. Contact Magnum \n>\t\t\t 5. Exit Help ";
cout<<"\n\n>\t\t Enter Your Choice : ";
ch=getch();
settextstyle(SMALL_FONT, HORIZ_DIR, 5);
switch(ch)
{
case '1':
cleardevice();
setbkcolor(8);
outtextxy(100,100," General Tips : ");
gotoxy(2,10);
cout<<"\n\t 1. Follow a general trend while entering details. ";
cout<<"\n\t Note that all key words are case sensitive. ";
cout<<"\n\t eg: Always use small letters or capitals ";
cout<<"\n\n\t 2. When some keyword do not work, try it again";
cout<<"\n\t but after changing the case of the keyword";
cout<<"\n\n\t 3. Be logical in your operations. Do not try to ";
cout<<"\n\t delete some records when you have not entered any ";
format();
getch();
break;
case '2':
cleardevice();
setbkcolor(8);
outtextxy(100,100," Menu Help : ");
gotoxy(2,10);
cout<<"\n\t 1. The 'Input' option ( 1 ) lets you add a record to ";
cout<<"\n\t the databse. You can even add multiple records ";
cout<<"\n\t 2. 'Modify' option( 2 ) lets you modify an existing ";
cout<<"\n\t record. You will have to re-enter all the details";
cout<<"\n\t 3. The 'Search' option ( 3 ) helps you in locating a ";
cout<<"\n\t record. You can search with almost all fields ";
cout<<"\n\t 4. 'Delete' option ( 4 ) can be used to permanently ";
cout<<"\n\t remove records from the database ";
cout<<"\n\t 5. 'Help Me !' ( 5 ) provides necessary help ";
cout<<"\n\t 6. 'Credits' ( 6 ) displays the credits";
cout<<"\n\t 7. 'Exit' ( 7 ) can be used to exit from the program ";
format();
getch();
break;
case '3':
cleardevice();
setbkcolor(8);
outtextxy(100,100,"Trouble Shoot :");
gotoxy(2,10);
cout<<"\n\t 1. Selecting Modify or Search or Delete Displays Error :";
cout<<"\n\t Database Might be missing or no record to read. ";
cout<<"\n\t Input some record and then try ";
cout<<"\n\n\t 2. Cannot find a record : ";
cout<<"\n\t Keyword may not be matching with what you had entered";
cout<<"\n\t Change the case of the keyword and then try ";
format();
getch();
break;
case '4':
cleardevice();
setbkcolor(8);
outtextxy(100,100," Contact Info : ");
gotoxy(2,10);
cout<<"\n\t Send your Responses, Bugs,Suggestions and Queries To : ";
cout<<"\n\t\t --- arunbasillal@gmail.com ---";
format();
getch();
break;
case '5':
f=1;
break;
default:
cout<<"Invalid Entry \a ";
}
}while(f==0);
}

void exit()
{
char ch;
cleardevice();
format();
setbkcolor(9);
rectangle(60,110,getmaxx()-60,getmaxy()-190);
settextstyle(TRIPLEX_FONT,HORIZ_DIR, 2);
outtextxy((getmaxx()/2)-50, 130,"Do You Really Wish To Exit (y/n) : ");
ch=getch();
if(ch=='y'ch=='Y')
{
rectangle(60,180,getmaxx()-60,getmaxy()-60);
outtextxy((getmaxx()/2),200,"Thank You For Choosing ");
outtextxy((getmaxx()/2),230,"--=[ Magnum Software Solutions ]=--");
outtextxy((getmaxx()/2),260,"Press Any Key");
getch();
credits();
exit(0);
}
}

void credits()
{
cleardevice(); // Clears The Screen
setbkcolor(6); // Sets back colour
format();
int size = 6;
int style=SMALL_FONT;
int midx = getmaxx() / 2;
settextstyle(style, HORIZ_DIR, size);
outtextxy(midx, 100," Coding : Arun Basil Lal ");
outtextxy(midx, 120," Concept : Anoop Davis ");
outtextxy(midx, 140," Graphic Design : Arun Basil Lal ");
outtextxy(midx, 160," Technical Support : Reshmi Sasikumar ");
outtextxy(midx, 200," Program Version : 1.0 ");
outtextxy(midx, 240," --- Send Your Queries And Suggestions To ---");
outtextxy(midx, 260," arunbasillal@gmail.com ");
outtextxy(midx, 300," * Special Thanks To Reshmi Sasikumar *");
getch();
}

---------------------------------------------------------------------
[ end of code ]

Disclaimer : Use this program at your own risk. No liabilities arising out of use of this program rest on me. All that I have to do after this is to go back to work. But I always appreciate criticisms. Nothing more.

b

0 Responses to "Telephone Directory - Source Code":