C++ implementation of Phone Book Management System














































C++ implementation of Phone Book Management System



C++ implementation of Phone Book Management System
  • Phonebook is a simple mini project in C++ that can help you understand the basic concepts of functions and file handling. 
  • This application will teach you how to add, list, modify or edit, search and delete data to/from the file. 
  • Adding new records, listing them, modifying them and updating, search for contacts saved, and deleting the phonebook records are the basic functions which make up the main menu of this Phonebook application. 
  • We have used many functions in this mini project. 

These functions, under the class phonebook, perform the following operations.
  1. newrecord()- It allow user to enter new record. 
  2. display()- It displays all the entries . 
  3. display(char *nm)- It searches for added record by name. 
  4. searchpname()- It searches for added record by phone number. 
  5. updatetelno()- This function is used to modify added records. 
  6. deleterec()- It deletes record from file.


Principles of OOPS implemented in the project-
  1.  Classes and objects
  2.  Namespace
  3.  Header files 
  4. Exception handling 
  5. Function overloading(polymorphism) 
  6. File handling

newrecord


  • newrecord function takes the input from the user as A NAME and A CONTACT NUMBER.

    getdata function is called in newrecord .

  • In getdata function we check the validity of the number entered with the help of exception handling.
  • We will check the  uniqueness of  the contact entered . If the contact is already existing it will show the message.
  • After a successful addition of the Record another message will appear on the screen .



display

  • We have used function overloading to display records.
  • One function is designed to display all the records of the contact list.
  • Other function is designed to display a contact number with a given name.



searchpname


With a given telephone number it searches and displays the name of the person holding that phone number.



update

Update function has 2 options of updating %u2013 

  1. Updating name of the contact 
  2. Updating conact number.


delete

  • Delete function deletes the record by taking the name of the the person holding the contact number.
  • If two person have the same name then it will give the option of deleting the record by looking at their contact

    .
DISPLAY ALL EXISTING RECORDS


ADDING NEW RECORDS



UPDATING EXISTING RECORDS



DELETION OF RECORDS WITH SAME NAME




File named phonebook.h
#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip>
#include <stdio.h>
using namespace std;

namespace register1
{
class phoneBook
{
char name[20],phno[11];
public:
void getdata();
void showdata();
char *getname(){ return name; }
char *getphno(){ return phno; }
void update(char *nm,char *telno)
{
strcpy(name,nm);
strcpy(phno,telno);
}
void newrecord();
void display();
void display(char *nm);
void searchpname();
void updatetelno();
void deleterec();

};

void phoneBook :: getdata()
{
cout<<"\nEnter Name : ";
cin>>name;
repeat:
cout<<"Enter Phone No. : ";
cin>>phno; //CAN CREATE A FUNCTION THAT CHECKS WHETHER STRING IS DIGIT OR NOT

for(int i=0;i<strlen(phno);i++)
{
try
{
if(phno[i]<48 || phno[i]>57||strlen(phno)!=10)
throw phno[i];
}
catch(char a)
{
cout<<"Enter a valid number"<<endl;
cout<<"Try again"<<endl;
goto repeat;
}
}
}

void phoneBook :: showdata(){
cout<<"\n";
cout<<setw(20)<<name;
cout<<setw(15)<<phno;
}

void phoneBook :: newrecord()
{
int found=0;
char rrep[11];
char nmopi[20];
getdata();
strcpy(nmopi,name);
strcpy(rrep,phno);
fstream file;
file.open("phone.txt", ios::in | ios::app | ios::binary );
while(file.read((char *) this, sizeof(*this)))
{
if(strcmp(rrep,getphno())==0)
{
found=1;
break;
}
}
file.clear();
file.close();
if(found==1)
cout<<"\n\n---Contact already exist---\n";
else
{
file.open("phone.txt", ios::app | ios::binary );
update(nmopi,rrep);
file.write((char *) this, sizeof(*this));
cout<<"Record Added Succesfully\n";
file.close();
}


}
void phoneBook :: display()
{
//Display All Records
fstream file;
file.open("phone.txt", ios::ate | ios::in | ios::out | ios::binary );
file.seekg(0,ios::beg);
cout<<"\n\nRecords in Phone Book\n";
while(file)
{
file.read((char *) this, sizeof(*this));
if(!file.eof())
showdata();
}
file.clear();
file.close();
}

void phoneBook :: display(char *nm) //function overloading
{ //Search Tel. no. when person name is known.
char c,telno[11];
int cnt,found=0;

fstream file;
file.open("phone.txt", ios::ate | ios::in | ios::out | ios::binary );
file.seekg(0,ios::beg);
while(file.read((char *) this, sizeof(*this)))
{
if(strcmp(nm,getname())==0)
{
found=1;
showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
file.close();

}

void phoneBook :: searchpname()
{
//Search name on basis of tel. no
char c,ch,nm[20],telno[11];
int cnt,found=0;
cout<<"\n\nEnter Telephone No : ";
cin>>telno;
fstream file;
file.open("phone.txt", ios::ate | ios::in | ios::out | ios::binary );
file.seekg(0,ios::beg);
while(file.read((char *) this, sizeof(*this)))
{
if(strcmp(telno,getphno())==0)
{
found=1;
showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
file.close();

}

void phoneBook :: updatetelno()
{ //Update Telephone No.
int k;
fstream file;
file.open("phone.txt", ios::ate | ios::in | ios::out | ios::binary );
cout<<"Enter 1 to update name"<<endl;
cout<<"Enter 2 to update phone number"<<endl;
cin>>k;
if(k==1)
{ char c,ch,nm[20],newnm[15],teleno[15];
int cnt=0,found=0;
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
while(file.read((char *) this, sizeof(*this)))
{
cnt++;
if(strcmp(nm,getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{ strcpy(teleno,phno);
int location = (cnt-1) * sizeof(*this);
cin.get(ch);
if(file.eof())
file.clear();

cout<<"Enter New Name: ";
cin>>newnm;
file.seekp(location);
update(newnm,teleno);
file.write((char *) this, sizeof(*this));
file.flush();
}
}
if(k==2)
{
char c,ch,nm[20],telno[15],newteleno[15];
int cnt=0,found=0;
cout<<"\n\nEnter Phone number : ";
cin>>telno;
file.seekg(0,ios::beg);
while(file.read((char *) this, sizeof(*this)))
{
cnt++;
if(strcmp(telno,getphno())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{ strcpy(nm,name);
int location = (cnt-1) * sizeof(*this);
cin.get(ch);
if(file.eof())
file.clear();

cout<<"Enter New Phone number: ";
cin>>newteleno;
file.seekp(location);
update(nm,newteleno);
file.write((char *) this, sizeof(*this));
file.flush();
}
}

file.close();
}


void phoneBook :: deleterec()
{
char bb[20],teleno[11];
cout<<"Enter the name of the record which is to be deleted:";
cin>>bb;
fstream file;
file.open("phone.txt", ios::in | ios::binary );
fstream fout;
fout.open("tempfile.txt",ios::out | ios::binary);
int i=0,j=0,found=0;
file.read((char *) this, sizeof(*this));
while(!file.eof())
{
i++;
if(strcmp(bb,getname()))
{
fout.write((char*)this,sizeof(*this));
j++;
}
// found++
file.read((char*)this,sizeof(*this));

}
fout.close();
file.close();
if(i-j<=1)
{
file.close();
remove("phone.txt");
rename("tempfile.txt","phone.txt");
if(j<i)
cout<<"RECORD DELETED"<<endl;
if(j==i)
cout<<"RECORD NOT FOUND"<<endl;
}
else
{
cout<<"\n\nTWO SAME CONTACT NAMES EXIST\n"<<endl;
file.open("phone.txt", ios::ate | ios::in | ios::out | ios::binary );
file.seekg(0,ios::beg);
while(file.read((char *) this, sizeof(*this)))
{
if(strcmp(bb,getname())==0)
{
found=1;
showdata();
cout<<endl;
}
}
file.clear();
file.close();
cout<<"\n\n NOW ENTER THE CONTACT NUMBER YOU WANT TO DELETE : "<<endl;
cin>>teleno;
//fstream file;
file.open("phone.txt", ios::in | ios::binary );
// fstream fout;
fout.open("tempfileph.txt",ios::out | ios::binary);
// int i=0,j=0,found=0;
file.read((char *) this, sizeof(*this));
i=0;
j=0;
while(!file.eof())
{
i++;
if(strcmp(teleno,getphno()))
{
fout.write((char*)this,sizeof(*this));
j++;
}
// found++
file.read((char*)this,sizeof(*this));
}
fout.close();
file.close();
remove("phone.txt");
rename("tempfileph.txt","phone.txt");
if(j<i)cout<<"\n\nRECORD DELETED\n\n"<<endl;
else cout<<"RECORD NOT FOUND"<<endl;
}
}
}





File Named main.cpp
#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip>
#include "phonebook.h"

using namespace std;
using namespace register1;
int main()
{
phoneBook rec;
int choice;

while(1)
{
char nm[20];
cout<<"\n*****Phone Book -Developed by CPPSECRETS *****\n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search Telephone No.\n";
cout<<"4) Search Person Name\n";
cout<<"5) Update Telephone No.\n";
cout<<"6) Delete a record \n";
cout<<"7) Exit\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice)
{
case 1 : rec.newrecord();
break;

case 2 : rec.display();
break;

case 3 :rec.searchpname();
break;

case 4 :
cout<<"\n\nEnter Name : ";
cin>>nm;
rec.display(nm);
break;

case 5 :rec.updatetelno();
break;

case 6: rec.deleterec();
break;

case 7: exit(1);

default:
cout<<"Invalid Option!!"<<endl;

}

}


return 0;
}







More Articles of Khitish Panigrahi:

Name Views Likes
Find and Replace Pattern 656 3
C++ implementation to find All combinations that add up to a given sum 583 0
Implementation of Donut.c 4903 1
Hamiltonian Cycle 2168 3
Add Lists 488 0
Unique Paths 501 3
Minimum Falling Path Sum 624 3
Word Break 585 3
Working of Internet 418 1
String Compression 1125 0
Palindromic Substrings 500 4
Formation of Process from Program 392 1
3Sum 462 3
Invert Binary Tree 739 4
0-1knapsack 610 2
Evaluate Reverse Polish Notation 1702 3
Permutations 405 4
Find Peak Element 1067 4
Maximum Subarray 436 5
Integer to Roman 2415 2
C++ Library for timing (Chrono Library) 1120 1
Programmatical termination of processes 495 1
Perfect Number 516 3
Number of Operations to Make Network Connected 1088 2
C++ implementation of Odd Even Linked List 832 0
Distance Between Bus Stops 571 3
Check if Binary Tree is BST 426 5
Partitioning the List 390 0
Function Pointer Typedef 1344 1
IOT SMART HOME 722 0
Friend Circles 1102 2
Remove duplicates from an unsorted linked list 529 0
Cycle in Undirected Graph 967 2
Multiple Word Searching Algorithm 469 8
Palindrome Partitioning 424 3
Find All Anagrams in a String 860 3
Path with Maximum Gold 701 3
Working of Home Networks 490 2
Container With Most Water 576 3
Loop Detection in LinkedList 326 0
Cheapest Flights Within K Stops 1440 2
C++ implementation of Partition Equal Subset Sum 423 0
C++ implementation to find Excel sheet Column Number 300 0
Longest Palindromic Subsequence (Print Subsequence) 987 2
Letter Tile Possibilities 638 3
Connect all siblings in a Binary Search Tree 777 2
Minimum Path Sum 642 3
C++ Hebb Learning Implementation 2010 0
Complete Employee Management System 6888 1
Combinations 394 7
C++ implementation of Longest Increasing Subsequence 406 0
Permutation of each other 287 0
Making HTTP Requests and Handling Callbacks 736 0
Valid Anagram 392 8
Multiply Strings 1678 5
C++ implementation to detect all the cycles in an undirected graph 1009 0
Longest Palindromic Subsequence (print only length) 415 2
Edit Distance 462 2
C++ implementation of Phone Book Management System 10250 0
Rotate Image 2131 1
C++ implementation of Road Transportation Management System 2658 0
Populating Next Right Pointers in Each Node 473 2
Last Stone Weight 1892 2
C++ implementation of Word Search 738 0
C++ implementation of Subsets with duplicates 325 0
Intersection of Two Linked Lists 412 3
Operator Overloading concepts in C++ 412 0
C++ implementation of Validate IP Address 2680 0
C++ implementation of Indian Railways Reservation System 1041 0
C++ implementation of college and Attendance management 3834 0
Maximum Depth of Binary Tree 526 3
Fibonacci Number 439 5
C++ implementation to Add Strings 411 0
Variadic function in C/ C++ 658 1
Boats to Save People 878 3
C++ implementation to detect cycle in a directed graph 2152 0
Pointer to Struct 571 1
House Robber with houses arranged in circular manner 871 3
Find Single Non-repeated Number 531 3
Setting up server 334 0
The k-th Lexicographical String of All Happy Strings of Length n 692 3
Unique Paths with obstacles 650 2
longest palindromic substring(improved) 408 2
Factorial Trailing Zeroes 407 2
String Rotation 916 0
C++ implementation of Bus Booking System 3601 0
3Sum Closest 505 2
kth to last element of a singly linked list 541 0
Jump Game 723 3
Maximum Product Subarray 376 4
Longest Common Subsequence (Print only Length) 608 2
ZigZag Conversion 753 4
Implement an algorithm to determine if a string has all unique characters 390 0
Relationship between IoT and cloud computing 927 1
Last Stone Weight II(any weight can be picked) 390 0
Commutable Islands 1518 2
Longest Substring Without Repeating Characters 490 4
Longest Arithmetic Sequence 803 3
Multiple Booting Guide with detail explanation of working 397 1
Minimum number of Perfect square needed to form a number 476 7
C++ implementation of Minimum number of Perfect square needed to form a number 363 0
Combination Sum 1117 4
Max Area of Island 525 3
Letter Combinations of a Phone Number 808 4
One Distance Away 432 0
Remove Nth Node From End of List 484 6
Concept of Signed and Unsigned Numbers 619 1
Time Needed to Inform All Employees 881 2
Best Time to Buy and Sell Stock 1506 3
Top K Frequent Elements 622 7
Intersection in LinkedList 569 0
Detailed analysis of Complex number using C++ 405 1
Implement strStr() 1109 4
Check Palindrome Permutation 361 0
Maximum Length of a Concatenated String with Unique Characters 555 3
Hamiltonian Path 2381 3
Relative Sort Array 418 3
Sequential Digits 593 3
Binary Tree Zigzag Level Order Traversal 385 4
C++ implementation of Shortest Path Problem 405 0
Roman to Integer 845 2
Delete a node from BST in C++ 451 3
Number of Matching Subsequences 539 3
C++ MOVIE TICKET BOOKING SYSTEM 12844 0
Number of Islands 487 3
Course Schedule II (print the order) 412 2
Permutation Sequence 515 16
Stack Pushing Game(Push Dominoes Game) 922 3
Inheritance implementation in C++ 456 0
Complete Restaurant/canteen management system 7708 0
Valid Palindrome 842 4
Course Schedule 886 2
Longest Palindromic Substring 389 2
C++ implementation of finding Kth Smallest Element in a BST 453 0
Travelling Salesman Problem 1797 3
Dijkstra 970 2
C++ implementation of Permutation with duplicates 528 0
Priority Queue 476 0
Deadlock Simulator in C++ 2758 0
Permutation Sequence 378 4
Maximal Square 486 3
C++ implementation of Is Subsequence Program 1205 0
C++ implementation of Coin Change 989 0
House Robber 628 3
4Sum 451 3
Minimum Size Subarray Sum 504 3
C++ implementation of parking management system 4877 0
Location Based Weather Predictor 1747 0
Climbing Stairs 474 5
String to Integer (atoi) 391 4
Children Sum Parent 470 5
C++ HEALTH MANAGEMENT SYSTEM 1731 0
Triangle 476 3
Majority Element 427 2
Longest Mountain in Array 780 4
Subarray Sum Equals K 541 3
Swap Nodes in Pairs 443 5
Restore IP Addresses 597 4
Convert String to URL 957 0
Create Ranges 497 3
Blood Bank Management System 3252 0
Linked List Cycle 374 6
Delete Middle Node in LinkedList 567 1
Smart and Safe Cab Finder Algorithm 1244 3
Chess Game in C++ 17493 1
Delete Node in Linked List without a head pointer 693 2
C++ Adaline Implementation 1071 0
Permutation in String 406 3
Merge Intervals 604 3
Gas Station 716 7
Increasing Triplet Subsequence 717 5
Longest Common Subsequence (Print the Subsequence) 554 2

Comments