C++ project on Address Book Using classes and File Handling -Output

C++ project on address book using classes and file handling is one of the simplest C++/ C project to demonstrate the working of a computer Project. This project was actually demonstrated in Class room – How to create a computer Project using all the acquired knowledge.

This project uses a simple class Telephone and one data file ‘telephone.dat‘ . Here is the sample code of this project. We missed some functions from this project as the same should be prepared by the student in order to fully understand the working of this project.

for better understanding on File handling in C++, You can have our notes on file handling and assignments and some high level file handling programs in C++.

/*	Project on Telephone directory 
	made by		: xyz     Roll No: 
				   &
				  abcd	  Roll No :
*/
#include
#include
#include
#include
using namespace std;
void line(char ch='-', int n=80)
{
	for(int i=0;i<n;i++)
		cout<<ch;
	cout<<endl;
}

class Telephone
{
	private:
			int recno;
			char name[60];
			char fname[60];
			char add[100];
			char phone[15];
			char email[80];
	public:
			// input data from keyboard
			void input()
			{
				cout<<"\n Enter Name :";
				fflush(stdin);
				cin.getline(name,60);
				cout<<"\n Enter Father Name :";
				fflush(stdin);
				cin.getline(fname,60);
				cout<<"\n Enter Address :";
				fflush(stdin);
				cin.getline(add,100);
				cout<<"\n Enter Phone No :";
				fflush(stdin);
				cin.getline(phone,15);
				cout<<"\n Enter Email Id :";
				fflush(stdin);
				cin.getline(email,80);
			}
			int count_record(){
				int rec=0;
				ifstream fin;
				fin.open("telephone.dat",ios::binary);
				while(fin.read((char*)this,sizeof(Telephone)))
						rec++;
				fin.close();
			 	return rec;
			}
			// menus
			void main_menu();
			void search_menu();
			void report_menu();
			
			void add_new_record();
			
			void delete_record(){
			}
			void modify_record(){
			}
			// search options
			void search_name();
			void search_fname(){
			}
			void search_address(){
			}
			void search_phone(){
			}
			void search_email(){
			}
			// report options
			void report_total();
			void report_name_phone(){
			}
			void report_name_email(){
			}
			void report_name_phone_email(){
			}
	};


void Telephone:: search_name(){
	char tname[30];
	int found =0;
	ifstream fin;
	fin.open("telephone.dat",ios::binary);
	system("cls");
	fflush(stdin);
	cout<<"\n Enter Name  :";
	cin.getline(tname,30);
	fin.read((char*)this,sizeof(Telephone));
	while(!fin.eof())
	{
		if(strcmpi(name,tname)==0)
			{
				found =1;
				cout<<"\n Name  :"<<name;
				cout<<"\n Fname :"<<fname;
				cout<<"\n Address :"<<add;
				cout<<"\n Phone :"<<phone;
				cout<<"\n Email :"<<email;
			}
		fin.read((char*)this,sizeof(Telephone));
	}
	fin.close();
	if(found==0)
		cout<<"\n Record not found.....";
	return;	
}

// member function to show main menu

void Telephone::main_menu(){
	int choice;
	do{
		system("cls");
		cout<<"\n\t\t\t  MAIN MENU";
		cout<<"\n\n\t\t1.	Add New Record";
		cout<<"\n\n\t\t2.	Delete Record";
		cout<<"\n\n\t\t3.	Modify Record";
		cout<<"\n\n\t\t4.	Search Menu";
		cout<<"\n\n\t\t5.	Report Menu";
		cout<<"\n\n\t\t6.	Exit";
		cout<<"\n\t\t\t\t Enter Your choice(1..6) :"; cin>>choice;
		switch(choice)
		{
			case 1:
					add_new_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\n Wrong Choice...Try again";
		}
		
	}while(choice!=6);
	return;
}

void Telephone::search_menu(){
	int choice;
	do{
		system("cls");
		cout<<"\n\t\t\t  SEARCHN MENU";
		cout<<"\n\n\t\t1.	Name";
		cout<<"\n\n\t\t2.	Father Name";
		cout<<"\n\n\t\t3.	Address";
		cout<<"\n\n\t\t4.	Phone No";
		cout<<"\n\n\t\t5.	Email";
		cout<<"\n\n\t\t6.	Exit";
		cout<<"\n\t\t\t\t Enter Your choice(1..6) :"; cin>>choice;
		switch(choice)
		{
			case 1:
					search_name();
					getch();
					break;
			case 2:
					search_fname();
					break;
			case 3:
					search_address();
					break;
			case 4:
					search_phone();
					break;
			case 5:
					search_email();
					break;
			case 6:
					break;
			default:
					cout<<"\n\n Wrong Choice...Try again";
		}
		
	}while(choice!=6);
	return;
}

void Telephone::report_menu(){
	int choice;
	do{
		system("cls");
		cout<<"\n\t\t\t  REPORT  MENU";
		cout<<"\n\n\t\t1.	Total Record";
		cout<<"\n\n\t\t2.	Name - Phone";
		cout<<"\n\n\t\t3.	Name- email";
		cout<<"\n\n\t\t4.	Name- Email-Phone";
		cout<<"\n\n\t\t5.	Exit";
		cout<<"\n\t\t\t\t Enter Your choice(1..5) :"; cin>>choice;
		switch(choice)
		{
			case 1:
					report_total();
					break;
			case 2:
					report_name_phone();
					break;
			case 3:
					report_name_email();
					break;
			case 4:
					report_name_phone_email();
					break;
			case 5:
					break;
			default:
					cout<<"\n\n Wrong Choice...Try again";
		}
		
	}while(choice!=5);
	return;
}

// member function to add a new record

void  Telephone::add_new_record()
 {
 	ofstream fout;
 	fout.open("telephone.dat",ios::binary|ios::app);
 	input();
 	fout.write((char *)this, sizeof(Telephone));
 	fout.close();
 }

// function to display total records( report
void Telephone::report_total(){
	ifstream fin;
	int no=0;
	int row=0;
	int page=1;
	int total_pages;
	int rec=0;
	
	rec = count_record();  // function call to find out total record
	
	total_pages = rec/5;
	if(rec%5!=0)
		total_pages++;
	
	
	getch();
	
	fin.open("telephone.dat",ios::binary);
	system("cls");
	cout<<"\n\t\t\t\t\t Telephone Diary";
	cout<<"\n\t\t\t\t\t	 Report\t\t\t\t Page :"<<page<<"of"<<total_pages<<"\n";
	line();
	cout<<"S.No \t Name \t\tAddress \t\tPhone \t\t Email\n"; line(); fin.read((char*)this,sizeof(Telephone)); while(!fin.eof()) { no++; row++; if(row>5)
		  {
		  	page++;
		  	row =0;
		  	cout<<"\n Press any key to continue....";
		  	getch();
		  	system("cls");
			cout<<"\n\t\t\t\t\t Telephone Diary";
			cout<<"\n\t\t\t\t\t	 Report\t\t\t\t Page :"<<page<<"of"<<total_pages<<"\n";
			line();
			cout<<"S.No	\t	Name	\t\tAddress	\t\tPhone \t\t Email\n";
			line();
		  }
	  	cout<<"\n"<<no<<"\t\t"<<name<<"\t\t"<<fname<<"\t\t"<<add;
	  	cout<<"\t"<<phone<<"\t"<<email<<"\n";
	   	fin.read((char*)this,sizeof(Telephone));
	  }
	fin.close();
	getch();
	return ;
}



int main()
{
	Telephone T;
	T.main_menu();
	return 0;
}

The output of this project is as follows

C++ project -output

%d bloggers like this: