Address Book

This is graphic C program that can be used to store personal information of people such as name, telephone, mobile, fax, city and country.

//Address Book Program by
//Manoj M
//manoj.exotic@gmail.com


#include <fstream.h>
#include <graphics.h>
#include <conio.h>
#include <mouseptr.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <ctype.h>
#include "final\head.cpp"

fstream adb;
int button, x, y;

struct CURSOR
{
 int x1;
 int x2;
 int y;
};

struct node
{
 char ch;
 node *next;
}*first = NULL, *temp;

void refreshbox()
{
 hidemouseptr();
 setfillstyle(1, 15);
 bar(10, 25, getmaxx(), 459);
 showmouseptr();
}

class menubutton
{
 int x1, y1, x2, y2;
 char caption[25];

 public:
 int flag;

 menubutton(int a1, int b1, int a2, int b2, char cap[25])
 {
  x1 = a1;  x2 = a2;
  y1 = b1;  y2 = b2;
  strcpy(caption, cap);
  flag=0;
 }

 void draw()
 {
  hidemouseptr();
  setfillstyle(1, 15);
  bar(x1, y1, x2, y2);
  setcolor(0);
  rectangle(x1, y1, x2, y2);
  setcolor(1);
  outtextxy(x1+10, y1+8, caption);
  showmouseptr();
 }

 void onover()
 {
  hidemouseptr();
  setfillstyle(1, 1);
  bar(x1+2, y1+2, x2-2, y2-2);
  setcolor(14);
  outtextxy(x1+10, y1+8, caption);
  showmouseptr();
 }

 int check()
 {
  getmousepos(&button, &x, &y);
  int val;
  val = x>x1 && x<x2 && y>y1 && y<y2;
  return val;
 }
};

void drawinterface()
{
 setfillstyle(1, 15);
 bar(0, 0, getmaxx(), getmaxx());
 setfillstyle(1, 1);
 bar(0, 460, getmaxx(), getmaxy());
 setfillstyle(1, 2);
 bar(0, 460, 60, getmaxy());
 setcolor(0);
 outtextxy(8, 466, "Start");
 setcolor(15);
 outtextxy(10, 467, "Start");
}

void initialize()
{
 int gd=DETECT, gm;
 initgraph(&gd, &gm, "");
 initmouse();
}

void draw3dbox(int x1, int y1, int x2, int y2)
{
 hidemouseptr();
 setcolor(7);
 line(x1, y1, x2, y1);
 line(x1, y1, x1, y2);
 setcolor(0);
 line(x1, y2, x2, y2);
 line(x1, y2+1, x2, y2+1);
 line(x2, y1, x2, y2);
 line(x2+1, y1, x2+1, y2);
 showmouseptr();
}

void displaychar(int x, int y, char ch)
{
 char str[2];
 str[0]=ch;
 str[1]=0;
 outtextxy(x, y, str);
}

void entertext(int x1, int y1, int x2, int y2, node *string)
{
 int flag=1;
 char ch;
 setcolor(0);
 rectangle(x1, y1, x2, y2);

 int tx, ty;
 int tm, cm;
 tx = x1+5;
 ty = y1+10;

 CURSOR cursor;
 cursor.x1 = x1+5;
 cursor.x2 = x1+10;
 cursor.y  = y1+18;

 setcolor(0);
 line(cursor.x1, cursor.y, cursor.x2, cursor.y);
 do
 {
  setcolor(0);
  rectangle(x1, y1, x2, y2);
  ch = getch();

//***************************************************************************
  if (ch>=33 && ch<=126 && !(ty>=y2-10))
  {
   if ( !(ty>=y2-20 && tx>=x2-25) )
   {
    node *newlink = new node;
    newlink = NULL;
    newlink->ch = ch;
    newlink->next=NULL;
    if (first==NULL)
    {
     first=newlink;
    }
    else
    {
     temp = first;
     while (temp->next!=NULL)
 temp=temp->next;
     temp->next=newlink;
    }
    setcolor(15);
    line(cursor.x1, cursor.y, cursor.x2, cursor.y);
    setcolor(0);
    displaychar(tx, ty, ch);
    tx+=8;
    tm = tx;
    cursor.x1+=8;
    cursor.x2+=8;
    cm = cursor.x1;
    if (tx>=x2-15)
    {
 tm=tx;
 tx=x1+5;
 ty+=8;
 cm = cursor.x1;
 cursor.x1 = x1+5;
 cursor.x2 = x1+10;
 cursor.y += 8 ;
    }
    line(cursor.x1, cursor.y, cursor.x2, cursor.y);
   }
  }
//***************************************************************************

  if (ch==8)
  {
   node *temp = first;
   node *ptr = temp;
   while (temp->next!=NULL)
   {
 ptr=temp;
 temp=temp->next;
 ch = temp->ch;
   }
   if (ptr==temp)
 first=NULL;
   setcolor(15);
   line(cursor.x1, cursor.y, cursor.x2, cursor.y);
   ptr->next=NULL;
   displaychar(tx, ty, temp->ch);
   tx-=8;
   cursor.x1-=8;
   cursor.x2-=8;
   displaychar(tx, ty, temp->ch);
   if (tx<x1+5)
   {
 tx=tm-8;
 ty-=8;
 cursor.x1-=8;
 cursor.x2-=8;
 flag=0;
   }
   if (flag==0)
   {
    cursor.x1 = cm-8;
    cursor.x2 = cm;
    cursor.y -= 8;
    flag=1;
   }
   if (ty<y1+10)
   {
    tx = x1+5;
    ty = y1+10;
    cursor.x1 = x1+5;
    cursor.x2 = x1+10;
    cursor.y  = y1+18;
   }
   displaychar(tx, ty, temp->ch);
   setcolor(0);
   line(cursor.x1, cursor.y, cursor.x2, cursor.y);
  }
//***************************************************************************

  if (ch==13)
  {
   setcolor(15);
   line(cursor.x1, cursor.y, cursor.x2, cursor.y);
   tx = x1+5;
   ty+=8;
   cursor.x1 = x1+5;
   cursor.x2 = x1+10;
   cursor.y += 8;
   setcolor(0);
   line(cursor.x1, cursor.y, cursor.x2, cursor.y);
   node *newlink = new node;
   newlink = NULL;
   newlink->ch = 32;
   newlink->next=NULL;
   if (first==NULL)
   {
    first=newlink;
   }
   else
   {
    temp = first;
    while (temp->next!=NULL)
 temp=temp->next;
    temp->next=newlink;
   }
  }

 }while (ch!=27);

 string=first;
 first = string;
}

void enterlinetext(int x1, int y1, int x2, char *string, int opt=0)
{
 /*
 OPTION   USE
 0   Files
 1   Name
 2   Numbers
 3   Email
 4   Address
 */ char *perm = NULL;
 char ch, sub[2];
 int y2 = y1+25;
 int tx, ty;
 tx = x1+5;
 ty = y1+8;
 perm = string;
 sub[1] = 0;
 CURSOR cursor;
 cursor.x1 = x1+5;
 cursor.x2 = x1+10;
 cursor.y  = y2-8;
 setcolor(0);
 rectangle( x1, y1, x2, y2);
 line( cursor.x1, cursor.y, cursor.x2, cursor.y);
 *string =0;
 do
 {
  ch = getch();
  sub[0] = ch;
  if ( ( isalpha(ch) || isdigit(ch) || ch =='.' || ch =='_' || ch =='\\'
       || ch ==':' ) && cursor.x2<x2-10 && opt==0)
  {
   *string++ = ch;
   *string = 0;
   setcolor(0);
   outtextxy(tx, ty, perm);
   setcolor(15);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
   cursor.x1+=8;
   cursor.x2+=8;
   setcolor(0);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
  }
  if ( (isalpha(ch) || ch =='.' || ch ==' ') && cursor.x2<x2-10 && opt==1)
  {
   *string++ = ch;
   *string = 0;
   setcolor(0);
   outtextxy(tx, ty, perm);
   setcolor(15);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
   cursor.x1+=8;
   cursor.x2+=8;
   setcolor(0);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
  }
  if ( isdigit(ch) && cursor.x2<x2-10 && opt==2)
  {
   *string++ = ch;
   *string = 0;
   setcolor(0);
   outtextxy(tx, ty, perm);
   setcolor(15);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
   cursor.x1+=8;
   cursor.x2+=8;
   setcolor(0);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
  }
  if ( (isalpha(ch) || isdigit(ch) || ch =='.' || ch =='_' || ch=='@')
 && cursor.x2<x2-10 && opt==3)
  {
   *string++ = ch;
   *string = 0;
   setcolor(0);
   outtextxy(tx, ty, perm);
   setcolor(15);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
   cursor.x1+=8;
   cursor.x2+=8;
   setcolor(0);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
  }
  if ( ( isalpha(ch) || isdigit(ch) || ch =='.' || ch =='-' || ch ==','
       || ch ==':' ) && cursor.x2<x2-10 && opt==4)
  {
   *string++ = ch;
   *string = 0;
   setcolor(0);
   outtextxy(tx, ty, perm);
   setcolor(15);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
   cursor.x1+=8;
   cursor.x2+=8;
   setcolor(0);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
  }
  else if ( ch == 8 && *perm!=0 )
  {
   setcolor(15);
   outtextxy(tx, ty, perm);
   string--;
   *string = 0;
   setcolor(0);
   outtextxy(tx, ty, perm);
   setcolor(15);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
   cursor.x1-=8;
   cursor.x2-=8;
   setcolor(0);
   line( cursor.x1, cursor.y, cursor.x2, cursor.y);
  }
 }while (ch!=27 && ch!=13);
}

class
{
 char name[50];
 char nickname[50];
 char job[50];
 char email[50];
 char telephone[20];
 char mobile[20];
 char fax[20];
 char address[100];
 char city[50];
 char zip[20];
 char country[50];

 public:
 friend void adbkaddmenu();
 friend void adbkremmenu();
 friend void adbkviewmenu();

 void reset()
 {
  strcpy(name, "");
  strcpy(nickname, "");
  strcpy(job, "");
  strcpy(email, "");
  strcpy(telephone, "");
  strcpy(mobile, "");
  strcpy(fax, "");
  strcpy(address, "");
  strcpy(city, "");
  strcpy(zip, "");
  strcpy(country, "");
 }
}info;

void adbkaddmenu()
{
 refreshbox();
 hidemouseptr();
 setcolor(0);
 outtextxy(45, 50,  "     Enter Name: ");
 outtextxy(45, 80,  " Enter Nickname: ");
 outtextxy(45, 110, "     Profession: ");
 outtextxy(45, 140, "      E-mail ID: ");
 outtextxy(45, 170, "  Telephone no.: ");
 outtextxy(45, 200, "     Mobile no.: ");
 outtextxy(45, 230, "            Fax: ");
 outtextxy(45, 260, "        Address: ");
 outtextxy(45, 290, "           City: ");
 outtextxy(45, 320, "       Zip Code: ");
 outtextxy(45, 350, "        Country: ");
 rectangle(200, 40, 400, 65);
 rectangle(200, 70, 400, 95);
 rectangle(200, 100, 400, 125);
 rectangle(200, 130, 500, 155);
 rectangle(200, 160, 400, 185);
 rectangle(200, 190, 400, 215);
 rectangle(200, 220, 400, 245);
 rectangle(200, 250, 500, 275);
 rectangle(200, 280, 400, 305);
 rectangle(200, 310, 400, 335);
 rectangle(200, 340, 400, 365);
 enterlinetext(200, 40, 400, info.name, 1);
 enterlinetext(200, 70, 400, info.nickname, 1);
 enterlinetext(200, 100, 400, info.job, 1);
 enterlinetext(200, 130, 500, info.email, 3);
 enterlinetext(200, 160, 400, info.telephone, 2);
 enterlinetext(200, 190, 400, info.mobile, 2);
 enterlinetext(200, 220, 400, info.fax, 2);
 enterlinetext(200, 250, 500, info.address, 4);
 enterlinetext(200, 280, 400, info.city, 1);
 enterlinetext(200, 310, 400, info.zip, 2);
 enterlinetext(200, 340, 400, info.country, 1);
 adb.open("Address.dat", ios::app);
 adb.write((char*)&info, sizeof(info));
 adb.close();
 setfillstyle(1, 4);
 bar(5, 440, getmaxx()-5, 455);
 setcolor(14);
 outtextxy(20, 445, "Information successfully Added !");
 getch();
 showmouseptr();
}

void adbkremmenu()
{
 char *name=NULL;
 int flag=0;
 refreshbox();
 hidemouseptr();
 setcolor(0);
 outtextxy(100, 110, "Enter Name: ");
 enterlinetext(200, 100, 400, name, 1);
 fstream temp_file;
 adb.open("Address.dat", ios::in);
 temp_file.open("Temp.dat", ios::out);
 adb.read((char*)&info, sizeof(info));
 while(!adb.eof())
 {
  if (strcmpi(name, info.name)!=0)
 temp_file.write((char*)&info, sizeof(info));
  else
 flag=1;
  adb.read((char*)&info, sizeof(info));
 }
 if (strcmpi(name, info.name)!=0 && flag==0)
 temp_file.write((char*)&info, sizeof(info));
 else
 flag=1;
 temp_file.close();
 adb.close();
 if (flag==1)
 {
  setfillstyle(1, 4);
  bar(5, 440, getmaxx()-5, 455);
  setcolor(14);
  outtextxy(20, 445, "Information successfully Deleted !");
  getch();
 }
 else if (flag==0)
 {
  setfillstyle(1, 4);
  bar(5, 440, getmaxx()-5, 455);
  setcolor(14);
  outtextxy(20, 445, "Information Not Found !");
  getch();
 }
 remove("Address.dat");
 rename("Temp.dat", "Address.dat");
 info.reset();
 showmouseptr();
}

void adbkviewmenu()
{
 char *name=NULL;
 char *str=NULL;
 int flag=0;
 refreshbox();
 hidemouseptr();
 setcolor(0);
 outtextxy(100, 40, "Enter Name: ");
 enterlinetext(200, 40, 400, name, 1);
 adb.open("Address.dat", ios::in|ios::app);
 int abc=0;
 if(!adb)
    outtextxy(10, 10, "problem");
 adb.read((char*)&info, sizeof(info));
 adb.read((char*)&info, sizeof(info));
 abc=adb.tellg();
 while (!adb.eof())
 {
  if (strcmpi(name, info.name)==0)
   {
    strcpy(str, "Name: ");
    strcat(str, info.name);
    outtextxy(80, 70, str);

    strcpy(str, "Nickname: ");
    strcat(str, info.nickname);
    outtextxy(80, 100, str);

    strcpy(str, "Profession: ");
    strcat(str, info.job);
    outtextxy(80, 130, str);

    strcpy(str, "Email ID: ");
    strcat(str, info.email);
    outtextxy(80, 160, str);

    strcpy(str, "Telephone: ");
    strcat(str, info.telephone);
    outtextxy(80, 190, str);

    strcpy(str, "Mobile: ");
    strcat(str, info.mobile);
    outtextxy(80, 220, str);

    strcpy(str, "Fax: ");
    strcat(str, info.fax);
    outtextxy(80, 250, str);

    strcpy(str, "Address: ");
    strcat(str, info.address);
    outtextxy(80, 280, str);

    strcpy(str, "City: ");
    strcat(str, info.city);
    outtextxy(80, 310, str);

    strcpy(str, "Zip Code: ");
    strcat(str, info.zip);
    outtextxy(80, 340, str);

    strcpy(str, "Country: ");
    strcat(str, info.country);
    outtextxy(80, 370, str);
    flag=1;
    break;
   }
  adb.read((char*)&info, sizeof(info));
  if (adb.eof())
 break;
 }
  adb.close();
  if (flag==1)
  {
   setfillstyle(1, 4);
   bar(5, 440, getmaxx()-5, 455);
   setcolor(14);
   outtextxy(20, 445, "Hit any key to continue...");
  }
  else if (flag==0)
  {
   setfillstyle(1, 4);
   bar(5, 440, getmaxx()-5, 455);
   setcolor(14);
   outtextxy(20, 445, "Information not found !");
  }
  getch();
 showmouseptr();
}

void main()
{
 initialize();
 drawinterface();
 showmouseptr();
 menubutton adbkadd(200, 150, 370, 180, "Add Entry");
 menubutton adbkrem(200, 200, 370, 230, "Delete Entry");
 menubutton adbkview(200, 250, 370, 280, "View Entry");
 menubutton adbkqt(200, 300, 370, 330, "Back");
 hidemouseptr();
 restrictmouseptr(0, 0, getmaxx(), 459);
 setfillstyle(1, 1);
 bar(4, 4, getmaxx()-4, 24);
 setcolor(15);
 outtextxy(250, 10, "ADDRESS BOOK");
 adbkadd.draw();
 adbkrem.draw();
 adbkview.draw();
 adbkqt.draw();
 draw3dbox(2, 2, getmaxx()-2, 457);
 showmouseptr();
 do
 {
  getmousepos(&button, &x, &y);
  if (adbkadd.check() && adbkadd.flag==0)
  {
 adbkadd.onover();
 adbkadd.flag=1;
  }
  if (!adbkadd.check() && adbkadd.flag==1)
  {
 adbkadd.draw();
 adbkadd.flag=0;
  }
  if ((button&1)==1 && adbkadd.check())
  {
 adbkadd.flag=2;
 adbkrem.flag=2;
 adbkview.flag=2;
 adbkqt.flag=2;
 adbkaddmenu();
 refreshbox();
 adbkadd.draw();
 adbkrem.draw();
 adbkview.draw();
 adbkqt.draw();
 adbkadd.flag=0;
 adbkrem.flag=0;
 adbkview.flag=0;
 adbkqt.flag=0;
  }

  if (adbkrem.check() && adbkrem.flag==0)
  {
 adbkrem.onover();
 adbkrem.flag=1;
  }
  if (!adbkrem.check() && adbkrem.flag==1)
  {
 adbkrem.draw();
 adbkrem.flag=0;
  }
  if ( (button & 1)==1 && adbkrem.check() )
  {
 adbkadd.flag=2;
 adbkrem.flag=2;
 adbkview.flag=2;
 adbkqt.flag=2;
 adbkremmenu();
 refreshbox();
 adbkadd.draw();
 adbkrem.draw();
 adbkview.draw();
 adbkqt.draw();
 adbkadd.flag=0;
 adbkrem.flag=0;
 adbkview.flag=0;
 adbkqt.flag=0;
  }

  if (adbkview.check() && adbkview.flag==0)
  {
 adbkview.onover();
 adbkview.flag=1;
  }
  if (!adbkview.check() && adbkview.flag==1)
  {
 adbkview.draw();
 adbkview.flag=0;
  }
  if ( (button & 1)==1 && adbkview.check() )
  {
 adbkadd.flag=2;
 adbkrem.flag=2;
 adbkview.flag=2;
 adbkqt.flag=2;
 adbkviewmenu();
 refreshbox();
 adbkadd.draw();
 adbkrem.draw();
 adbkview.draw();
 adbkqt.draw();
 adbkadd.flag=0;
 adbkrem.flag=0;
 adbkview.flag=0;
 adbkqt.flag=0;
  }

  if (adbkqt.check() && adbkqt.flag==0)
  {
 adbkqt.onover();
 adbkqt.flag=1;
  }
  if (!adbkqt.check() && adbkqt.flag==1)
  {
 adbkqt.draw();
 adbkqt.flag=0;
  }
  if ((button&1)==1 && adbkqt.check())
 break;
 }while (1);
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post