You are on page 1of 12

/* Program to make address book

developed by : ------------
name : Kavita Bisht
Class : XII - A :
Sesssion : 2015-16
roll No : 15
-------------
*/

// HEADER FILES

# include<iostream>
# include<conio.h>
#include<string.h>
#include<fstream>
# include<stdio.h>
using namespace std;

// CLASS DECLARATION
void line()
{
int i;
for(i=0;i<80;i++)
cout<<"-";
}
class addressbook
{
private :
int sno;
char name[20];
char address[80];
char state[20];
char phoneno[30];
char city[20];
char rno[10];
char rn[20];

public:
char choice[20];
void main_menu(); // function to display main menu
void add_record(); // function to add record in a data file
void delete_record(); // function to delete record from data file
void modify_record(); // function to modify a record in a data file
void search_menu() ; // function to display search menu
void search_sno();
void search_name();
void search_address();
void search_phoneno();
void search_city();
void search_state();

void report_menu() ; // function to display report menu


void report_complete();
void report_name();
void report_address();
void input_data(); // function to read all data members from keyboard
void display(); // function to display information
int record_count();

} b;
// function to display main menu of record----------

void addressbook:: main_menu()


{
int choice;
do
{
system("cls");
cout<<" \n\n\t\t\t Main menu";
cout<<"\n\n\t\t\t 1. Add Record ";
cout<<"\n\n\t\t\t 2. Delete Record ";
cout<<"\n\n\t\t\t 3. Modify Record";
cout<<"\n\n\t\t\t 4. Search Record";
cout<<"\n\n\t\t\t 5. Report";
cout<<"\n\n\t\t\t 6. Exit ";
cout<<"\n\n\n\t\t\t Enter Your Choice (1...6) :";
cin>>choice;
switch(choice)
{
case 1:
add_record();
break;

case 2:
delete_record();
break;

case 3:
modify_record();
break;

case 4:
search_menu();
break;

case 5:
report_menu();
break;

case 6: break;

default :
cout<<"\n Wrong Choice...... Try Again ";

}while(choice!=6);

return;
}

// member function to receive data from keyboard

void addressbook:: input_data()


{
system("cls");
cout<<"\n Enter s.no. : ";
cin>>sno;
cout<<"\n Enter Name : ";
cin>>name;
cout<<"\n Enter address : ";
cin>>address;
fflush(stdin);
cout<<"\n Enter phone no. : ";
cin>>phoneno;
cout<<"\n Enter city : ";
cin>>city;
cout<<"\n Enter state : ";
cin>>state;

}
void addressbook::display()
{

cout<<"\n Sno : "<<sno;


cout<<"\n Name : "<<name;
cout<<"\n address : "<<address;
cout<<"\n phone no. : "<<phoneno;
cout<<"\n state : "<<state;
cout<<"\n city :"<<city;

return;

}
// Function to add new record

void addressbook::add_record()
{
ofstream fout;

fout.open("address.book",ios::app|ios::binary);

input_data();
fout.write((char *)this, sizeof(addressbook));

fout.close();
return;

}
void addressbook::delete_record()
{
ifstream fin;
ofstream fout;
int tsno;
int flag =0;
system("cls");
cout<<"\n Enter serial no to delete :";
cin>>tsno;

fin.open("address.book",ios::binary);
fout.open("temp.dat");

while(fin.read((char*)this, sizeof(addressbook)))
{
if(tsno!=sno)
fout.write((char*)this,sizeof(addressbook));
else
flag=1;

}
fin.close();
fout.close();

remove("address.book"); // function from stdio.h header file


rename("temp.dat","address.book"); // function from stdio.h header file

if(flag==1)
cout<<"\n\n Record Sucessfully removed ";
else
cout<<"\n Serial no. :"<<tsno <<" does not exist...Try again";
getch();
return;
}

void addressbook::modify_record()
{
ifstream fin;
ofstream fout;
int tsno;
int front =0;
system("cls");
cout<<"\n Enter sno. to Modify :";
cin>>tsno;

fin.open("address.book",ios::binary);
fout.open("temp.dat");

while(fin.read((char*)this, sizeof(addressbook)))
{
if(tsno == sno)
{ input_data(); front=1; }

fout.write((char*)this,sizeof(addressbook));

}
fin.close();
fout.close();

remove("address.book"); // function from stdio.h header file


rename("temp.dat","address.book"); // function from stdio.h header file

if(front==1)
cout<<"\n\n Record Sucessfully modified ";
else
cout<<"\n Serial no. :"<<tsno <<" does not exist...Try again";
getch();
return;

// Function name : search menu


// Objective : produce search menu for easy navigation

void addressbook::search_menu()
{
int search;
do{

system("cls");
cout<<"\n\n\t\t\t search menu ";
cout<<"\n \n\t\t\t 1.s.no.";
cout<<"\n \n\t\t\t 2.Name";
cout<<"\n\n\t\t\t 3.Phone no.";
cout<<"\n\n\t\t\t 4. Address";
cout<<"\n\n\t\t\t 5. City";
cout<<"\n\n\t\t\t 6. State";
cout<<"\n\n\t\t\t 7. Exit";
cout<<"\n\n\t\t\t enter your choice (1...7):";
cin>>search;
switch(search)
{
case 1:
search_sno();
getch();
break;

case 2:
search_name();
getch();
break;

case 3:
search_address();
getch();
break;

case 4:
search_phoneno();
getch();
break;

case 5:
search_state();
break;

case 6:
search_city();
getch();
break;
case 7:
break;

default :
cout<<"\n wrong search..... try again!!";

}while(search!=7);
return;

}
/*--------------function to search record serial no. wise ------*/

void addressbook::search_sno()
{
ifstream fin;
fin.open("address.book");

int tsno;
int found=0;

system("cls");
cout<<"\n Enter serial number to search : ";
cin>>tsno;

while(fin.read((char*)this,sizeof(addressbook)))
{
if(tsno == sno)
{
system("cls");
cout<<"\n Information of a person ";
cout<<"\n--------------------------------------------------------"<<endl;
display();
found = 1;
}

}
fin.close();
if(found==0)
cout<<"\n\n Serial no. : "<<tsno <<" does not exist in our address book.... Try again";

return;

}
/*--------------function to search record accoring to name ------*/

void addressbook::search_name()
{
ifstream fin;
fin.open("address.book");
char tname[20];
int fit=0;
system("cls");
cout<<"\n Enter Name to search :";
cin>>tname;
while(fin.read((char*)this,sizeof(addressbook)))
{
if(strcmpi(tname,name)==0)
{
system("cls");
cout<<"\n Information of person ";
cout<<"\n--------------------------------------------------------"<<endl;
display();
fit=1;
}
}
fin.close();
if(fit==0)
cout<<"\n No such name : "<<strupr(tname)<<" exist in our address book... Try again";

return;
}
/*--------------function to search record acording to address ------*/

void addressbook::search_address()
{
ifstream fin;
fin.open("address.book");
char tadd[80];
int flag=0;
system("cls");
cout<<"\n Enter Address to search :";
cin>>tadd;
while(fin.read((char*)this,sizeof(addressbook)))
{
if(strcmpi(tadd,address)==0)
{
system("cls");
cout<<"\n Information of a person ";
cout<<"\n--------------------------------------------------------"<<endl;
display();
flag=1;
}
}
fin.close();
if(flag==0)
cout<<"\n No such address : "<<strupr(tadd)<<" exist in our address book... Try again";

return;
}
/*---function to search student record according to phone no.-----*/

void addressbook::search_phoneno()
{
ifstream fin;
fin.open("address.book");
char ph[20];
int flag=0;
system("cls");
cout<<"\n Enter Phone no. to search :";
cin>>ph;
while(fin.read((char*)this,sizeof(addressbook)))
{
if(strcmpi(ph,phoneno)==0)
{
system("cls");
cout<<"\n Information of a person ";
cout<<"\n--------------------------------------------------------"<<endl;
display();
flag=1;
}
}
fin.close();
if(flag==0)
cout<<"\n No such phone no. : "<<strupr(ph)<<" exist in our address book... Try again";

return;
}
/*--------function to search record according to state ------*/

void addressbook::search_state ()
{
ifstream fin;
fin.open("address.book");
char st[30];
int flag=0;
system("cls");
cout<<"\n Enter name of state to search :";
cin>>st;
while(fin.read((char*)this,sizeof(addressbook)))
{
if(strcmpi(st,state)==0)
{
system("cls");
cout<<"\n Information of a person";
cout<<"\n--------------------------------------------------------"<<endl;
display();
flag=1;
}
}
fin.close();
if(flag==0)
cout<<"\n No such name of state : "<<strupr(st)<<" exist in our address book... Try again";

return;
}
/*---------function to search record according city ------*/

void addressbook::search_city()
{
ifstream fin;
fin.open("address.book");
char ct[20];
int flag=0;
system("cls");
cout<<"\n Enter Phone no. to search :";
cin>>ct;
while(fin.read((char*)this,sizeof(addressbook)))
{
if(strcmpi(ct,city)==0)
{
system("cls");
cout<<"\n Information of a person";
cout<<"\n--------------------------------------------------------"<<endl;
display();
flag=1;
}
}
fin.close();
if(flag==0)
cout<<"\n No such name of city : "<<strupr(ct)<<" exist in our address book... Try again";

return;
}

// function to print all the record from the table


void addressbook::report_menu()
{
int choice;
do
{
system("cls");
cout<<"\n\n\t\t\t REPORT MENU ";
cout<<"\n\n\t\t\t 1. Complete Report ";
cout<<"\n\n\t\t\t 2. Modify record ";
cout<<"\n\n\t\t\t 3. Search Record";
cout<<"\n\n\t\t\t 4. Report _menu ";
cout<<"\n \n\t\t\t 5. report_name";
cout<<"\n\n\t\t\t 6. Exit";
cout<<"\n\n\n\t\t Enter your choice (1..6) :";
cin>>choice;
switch(choice)
{
case 1:
report_complete();
break;
case 2:
modify_record();
break;

case 3:
search_menu();
break;
case 4:
report_menu();
break;

case 5:
report_name();
break;

case 6: break;
default :
cout<<"\n Wrong choice ... Try again";
}
} while(choice!=5);
return ;
}
// function to read data from text file and display them on the screen
void addressbook::report_complete()
{
int record = record_count(); // function call to find out total available record
int pages;
if(record<=20)
pages = 1;
else
pages = record/20 + 1 ;
int row= 6;
int cur_page=1;
ifstream fin ;
fin.open("address.book",ios::binary);
system("cls");
cout<<"\n\t\t\t\t\t COMPLETE REPORT \n";
line();
cout<<"\n sno. \t\t\tName \t\t\taddress \t\t\t phoneno. \t\t\t state \t\t\t city \t\t\t ";
line();
cout<<"\n";
while(fin.read((char *)this, sizeof(addressbook)))
{
cout<<sno<<"\t\t"<<name<<"\t\t"<<phoneno<<"\t\t"<< state<<"\t\t"<<city<<"\n";
row++;
if( row>=20)
{
cur_page++;
cout<<"\n\n Press any key to continue.....";
getch();
row =6;
line();
cout<<"\n Page "<< cur_page << " of "<< pages;
line();
}
}

getch();
fin.close();

return ;
}
void addressbook::report_name()
{
char tname[20];

int row =6;


int no =0;
ifstream fin;
fin.open("address.book");
system("cls");
cout<<"\n Enter name :";
cin>>tname;

system("cls");

cout<<"\n\t\t ADDRESS BOOK ";


cout<<"\n\t\t name "<<tname<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<"sno\tName\taddress\t phone no. \t state\t city\n";
cout<<"-------------------------------------------------------------------------------"<<endl;

while(fin.read((char*)this, sizeof(addressbook)))
{
if(tname==name )
{
row= row+1;
no++;
if(row>=20)
{
cout<<"\n\n Press any key to continue ................";
getch();
cout<<"\n\t\t ADDRESS BOOK";
cout<<"\n\t\t name :"<<tname<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<"sno.\tName\taddress\tphone no. \t state\t city\n";
cout<<"-------------------------------------------------------------------------------"<<endl;
row =6;
}
cout<<sno<<"\t"<<name<<"\t"<<address<<"\t"<<phoneno<<"\t"<<state<<"\t"<<city<<endl;

}
}
fin.close();
cout<<"-------------------------------------------------------------------------------"<<endl;
getch();
return ;

void addressbook::report_address()
{
char tadd[80];

int row =6;


int no =0;
ifstream fin;
fin.open("address.book");

system("cls");
cout<<"\n Enter address :";
cin>>tadd;

system("cls");
cout<<"\n\t\t ADDRESS BOOK ";
cout<<"\n\t\t address "<<tadd<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<"sno\tName\taddress\t phone no. \t state\t city\n";
cout<<"-------------------------------------------------------------------------------"<<endl;

while(fin.read((char*)this, sizeof(addressbook)))
{
if(tadd==address )
{
row= row+1;
no++;
if(row>=20)
{
cout<<"\n\n Press any key to continue ................";
getch();
cout<<"\n\t\t ADDRESS BOOK";
cout<<"\n\t\t address :"<<tadd<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<"sno.\tName\taddress\tphone no. \t state\t city\n";
cout<<"-------------------------------------------------------------------------------"<<endl;
row =6;
}
cout<<sno<<"\t"<<name<<"\t"<<address<<"\t"<<phoneno<<"\t"<<state<<"\t"<<city<<endl;

}
}
fin.close();
cout<<"-------------------------------------------------------------------------------"<<endl;
getch();
return ;

// function to count total no of records

int addressbook::record_count()
{
int n=0;
ifstream fin;
fin.open("address.book",ios::binary);

while(fin.read((char *)this, sizeof(addressbook)))


{
n++;
}
return n;
}
int main()
{
addressbook b;
b.main_menu();

return 0;
}

You might also like