VB icon

Address book

Email
Submitted on: 1/1/2015 3:47:00 PM
By: Mokarrabin A Rahman (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 2682
 
     Simple address book Class in C++, has methods for adding new record, sorting, deleting and storing data in a file. Has it own menu aswell.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Address book
// Description:Simple address book Class in C++, has methods for adding new record, sorting, deleting and storing data in a file. Has it own menu aswell.
// By: Mokarrabin A Rahman (from psc cd)
//**************************************

//**********************************************
//	The address book
//	mokarrabin@mail.com
//	http://www.geocities.com/mokarrabin
//**********************************************
# define maxAdd100
# include<iostream.h>
# include<conio.h>
# include <fstream.h>
# include <string.h>
# include <dos.h>
class adbk{
 struct data{
char name[20];
char address[30];
char tel[12];
char fax[12];
char email [26];
 }info[maxAdd],temp;
 int count,n,PTR;char ch;
public:
//Constructor that reads all the previous data stored in File.
adbk(){
	 clrscr();cout<<"Reading data..";delay(1000);
	 count=0;n=0;
	//read all the data from HDD.
	ifstream in("adbk.dat");
	while(in.read((char *)&temp,sizeof(temp))!=0){
	 strcpy(info[count].name,temp.name);strcpy(info[count].address,temp.address);
	 strcpy(info[count].tel,temp.tel);strcpy(info[count].fax,temp.fax);
	 strcpy(info[count].email,temp.email);
	 if(temp.name[0]!=' ')count++;
	}
	in.close();cout<<"done.\n\n";delay(100);
};
//Destructor that truncates the older data file and writes the new data
~adbk(){		cout<<"Writing data to file..";
	//Write all data to HDD.
	ofstream out("adbk.dat",ios::trunc|ios::binary);
	for(n=0;n<count;n++){
	 out.write((char *)&info[n],sizeof(info[n]));
	}
	out.close();cout<<"Finished\n\n";delay(1000);
};
//Method to get new data from the user & keep it in memory.
getNew(){
	cout<<"Name:";cin>>temp.name;cout<<endl;
	cout<<"Address:";cin>>temp.address;cout<<endl;
	cout<<"Tel:";cin>>temp.tel;cout<<endl;
	cout<<"Fax:";cin>>temp.fax;cout<<endl;
	cout<<"Email:";cin>>temp.email;cout<<endl;
	 strcpy(info[count].name,temp.name);strcpy(info[count].address,temp.address);
	 strcpy(info[count].tel,temp.tel);strcpy(info[count].fax,temp.fax);
	 strcpy(info[count].email,temp.email);
	 count++;
};
//Method to show & edit all the data from the memory
// this function returns an integer value that is used as command in the
// main boy of the program.
int showNedit(){ n=0;
	//Show the records
	while(ch!=27){					clrscr();
	 cout<<"<ESC>Escape.\n<D>Delete Record.\n<U arrow>Up <D arrow>Down\n";
	 cout<<"<A>Add New Record\n<S>Sort Database\n";
	 cout<<"\nRecord Num:"<<n+1<<"\n\n";
	 cout<<info[n].name<<endl<<info[n].address<<endl<<info[n].tel<<endl;
	 cout<<info[n].fax<<endl<<info[n].email<<endl;
	 ch=getch();clrscr();//if(ch==0)ch=getch();
	 if (ch==72 && n>0)n--;
	 if (ch==80 && n<count-1)n++;
	 if (ch=='d'||ch=='D'){//delete a record.
	 temp=info[n];
	 info[n]=info[count-1];
	 count--; return 1;
	 }
	 if(ch=='a'||ch=='A')return 2;
	 if(ch=='s'||ch=='S')return 1;
	}
	return 0;//Return the total num of records.
};//end of retreive
//Method to sort the records list in memory.
sort(){
//Bubble Sort from 'DATA STRUCURES' by SEYMOUR & LIPSCHUTZ.
	for(n=1;n<count-1;n++){
	 PTR=1;
	 while(PTR<=count-n){
	 if(strcmp(info[PTR-1].name,info[PTR].name)>0){
	temp=info[PTR-1];
	info[PTR-1]=info[PTR];
	info[PTR]=temp;}
	 PTR++;
	 }
	}
//------
}
}ob;
main() {
int command=205;
while(command!=0){
 command= ob.showNedit();//returns a value.
 if(command==1)ob.sort();
 if(command==2){
	ob.getNew();
	ob.sort();
 }
}
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.