Free Coding Ebook 👉 Get Now
Creating an Address Book Project in C++
Introduction
Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create an Address Booklist using C++. An Address booklist consists of people’s names, addresses, phone numbers, and other pertinent data. The user uses it as a reference so they can find and reach out to family, clients, friends, and coworkers in an efficient manner.
ADVERTISEMENT
This address booklist application will let you add user contact details, edit the contact, deleted contacts, view all the contacts, Search for a particular contact from the list and exit from the program if you don’t wish to perform any further function.
Overview of the UI:
This address booklist will let the user enter details and keep records for users. This application will let you:
- Add Contact
- Edit Contact
- Delete Contact
- View all Contacts
- Search Address book
- Exit
Creating an Employee Database Management project using C++
We will create functions to perform all these actions. Each of the operation will have further UI which we will see now:
1) Add Contact
- Enter First Name:
- Enter Last Name:
2) Edit Contact
- Please type the Entry number that you wish to edit:
3) Delete contact
- Please type the Entry number that you wish to delete:
4) View all contacts
- It will display all the contacts in the address booklist
5) Search Address Book
- 1.) First name
- 2.) Last name
- 3.) Address
- 4.) Contact
6) Exit
- Program Terminated
#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; // Function prototypes void addContacts(); void viewContacts(); void searchContact(); void editContact(); void deleteContact(); int main() { //Main Function bool run=true; do{ int Option; //Main menu cout << "----------------------Address Book-----------------------------" << endl; cout << "\n"; cout << "What would you like to do?" << endl; cout << "1.) Add Contact" << endl; cout << "2.) Edit Contact" << endl; cout << "3.) Delete Contact" << endl; cout << "4.) View All Contacts" << endl; cout << "5.) Search Address Book" << endl; cout << "6.) Exit" << endl << endl; cout << "Choose an option: "; cin >> Option; cin.ignore(); switch (Option){ case 1: addContacts(); break; case 2: editContact(); break; case 3: deleteContact(); break; case 4: viewContacts(); break; case 5: searchContact(); break; case 6: run = false; break; default: cout << "Please Choose between 1 to 6" <<endl; } } while(run); cout << "Program Terminated"; } void addContacts(){ //Function for adding contacts. system ("cls"); string Fname, Lname, Address, Contact, list, name, Fname2, Lname2, Address2, Contact2; double counter, number; cout << "----------------------Address Book-----------------------------" << endl << endl; cout << "Do not use spaces if entry has 2 or more words ex: KyleAdrian" << endl; cout << "Enter 'quit' at First name to quit" << endl << endl; cout << "Enter First Name: "; getline(cin, Fname); if (Fname == "quit") main(); cout << "Enter Last Name: "; getline(cin, Lname); cout << "Enter Address: "; getline(cin, Address); cout << "Enter Contact Number: "; getline(cin, Contact); ifstream asd("AddressBook.txt"); while(asd >> counter >> Fname2 >> Lname2 >> Address2 >> Contact2){ if (counter == 100){ cout << "Invalid Max number of contacts reached (100)."; main (); } else number = counter; } ofstream adb("AddressBook.txt", ios::app); number = number + 1; adb << number << " " << Fname << " " << Lname << " " << Address << " " << Contact << endl; system("pause"); system("cls"); } void viewContacts(){ //Show all entries in the data base. system("cls"); double counter; string Fname, Lname, Address, Contact; ifstream addressbook("AddressBook.txt"); cout << "Entry #" << setw(17) << "First Name" << setw(23)<< "Last Name" << setw(23) <<"Address"<< setw(29)<<"Contact"<< endl << endl; while (addressbook >> counter >> Fname >> Lname >> Address >> Contact){ cout << setw(3)<< counter << setw(18)<< Fname << setw(25) << Lname << setw(25) << Address << setw(30) << Contact << endl; } cout << endl; system ("pause"); system ("cls"); } void searchContact(){ //Allow to specific entry. system("cls"); int choice; double counter, number; string Fname, Lname, Address, Contact, Fname2, Lname2, Address2, Contact2; cout << "----------------------Address Book-----------------------------" << endl << endl; cout << "---Search Address Book---" << endl; cout << "1.) First name" << endl; cout << "2.) Last name" << endl; cout << "3.) Address" << endl; cout << "4.) Contact " << endl; cout << "Enter Choice: "; cin >> choice; switch (choice){ case 1: cout << "Enter First Name: "; cin >> Fname; cout << endl; break; case 2: cout << "Enter Last Name: "; cin >> Lname; cout << endl; break; case 3: cout << "Enter Address: "; cin >> Address; cout << endl; break; case 4: cout << "Enter Contact: "; cin >> Contact; cout << endl; break; default: cout << "Please Enter choice from 1 to 4"; searchContact(); } ifstream search("AddressBook.txt"); if (choice==1){ while (search >> counter >> Fname2 >> Lname2>> Address2 >> Contact2){ if(Fname == Fname2){ cout << counter << " " << Fname2 << " " << Lname2 << " " << Address2 << " " << Contact2 << endl << endl; } } } if (choice==2){ while (search >> counter >> Fname2 >> Lname2>> Address2 >> Contact2){ if(Lname == Lname2){ cout << counter << " " << Fname2 << " " << Lname2 << " " << Address2 << " " << Contact2 << endl << endl; } } } if (choice==3){ while (search >> counter >> Fname2 >> Lname2>> Address2 >> Contact2){ if(Address == Address2){ cout << counter << " " << Fname2 << " " << Lname2 << " " << Address2 << " " << Contact2 << endl <<endl; } } } if (choice==4){ while (search >> counter >> Fname2 >> Lname2>> Address2 >> Contact2){ if(Contact == Contact2){ cout << counter << " " << Fname2 << " " << Lname2 << " " << Address2 << " " << Contact2 << endl << endl; } } } system ("pause"); system ("cls"); } void editContact(){ //This part allows you to edit the entries. system("cls"); int choice; double counter, number; string Fname, Lname, Address, Contact, Fname2, Lname2, Address2, Contact2, choice2, choice3; ifstream edit("AddressBook.txt"); ofstream temp("Temp.txt", ios::app); cout << "Please type the Entry number that you wish to edit: "; cin >> choice; cout << endl; if (choice==0 || choice > 100){ cout << "Error, wrong entry"; system("pause>0"); editContact(); } while (edit >> counter >> Fname2 >> Lname2>> Address2 >> Contact2){ if (counter==choice){ cout << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl<<endl; cout << "Is this the contact that you wish to edit? (y or n) "; cin >> choice3; cout <<endl; } if (choice3=="n") { main(); } if (choice3=="y"){ if (counter<choice){ temp << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl; } if (counter==choice){ cout << "Enter New First name: "; cin >> Fname; cout << "Enter New Last name: "; cin >> Lname; cout << "Enter New Address: "; cin >> Address; cout << "Enter New Contact: "; cin >> Contact; temp << choice << " " << Fname << " "<< Lname << " " << Address << " " << Contact << endl; } if (counter > choice){ temp << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl; } } } edit.close(); temp.close(); if (remove("AddressBook.txt")==0){ cout << "Succesful Removing File" << endl; }else{ cout << "Error removing"<< endl; } if(rename("Temp.txt", "AddressBook.txt")==0){ cout << "Succesful Renaming file"<< endl; }else{ cout << "Error renaming"<<endl; } system("pause"); system("cls"); } void deleteContact(){ //This function allow to delete entries one by one. system("cls"); int choice; double counter, number; string Fname, Lname, Address, Contact, Fname2, Lname2, Address2, Contact2, choice2,choice3; ifstream edit("AddressBook.txt"); ofstream temp("Temp.txt", ios::app); cout << "Please type the Entry number that you wish to delete: "; cin >> choice; cout << endl; while (edit >> counter >> Fname2 >> Lname2>> Address2 >> Contact2){ if (counter==choice){ cout << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl<<endl; cout << "Is this the contact that you wish to delete? (y or n) "; cin >> choice3; cout << endl; } if (choice3=="n") { main(); } if (counter<choice){ temp << counter << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl; } if (counter > choice){ temp << counter - 1 << " " << Fname2 << " "<< Lname2 << " " << Address2 << " " << Contact2 << endl; } } edit.close(); temp.close(); if (remove("AddressBook.txt")==0){ cout << "Succesful Removing File" << endl; }else{ cout << "Error removing"<< endl; } if(rename("Temp.txt", "AddressBook.txt")==0){ cout << "Succesful Renaming file"<< endl; }else{ cout << "Error renaming"<<endl; } system("pause"); system("cls"); }
Now let us understand the code:-
After writing the header of the code with the required libraries, we have declared functions, the functions are – addContacts(), viewContacts(), searchContact(), editContact(), deleteContact() and the main() function.
The main() function :
- system(“cls”); – Used to clear the screen
- A do-while loop is used to first display all the functions inside the address booklist which the user can select from to perform the functions- add, edit, delete, view and search contacts. The user will enter the choice.
- Using a switch-case control statement to select the function to be performed.
Build Tic Tac Toe Game Code in C++ Using Array
The addContacts() function:
- This is a function created to add contacts in the address booklist.
- We will declare variables with their required data-types to input the values from the user.
- These following variable values you will need to enter to add a contact in the address booklist- First Name, Last Name, Address, Contact Number.
- The maximum number of contacts you can add is 100 but you can changes the number according to your need.
-
Do not use spaces if entry has 2 or more words.
The viewContacts() function:
- This function will show all entries in the database.
- This function displays all the contacts in a table like format.
- ifstream is used used to create files and to write data to files. This class describes an input stream, a program that reads data from files and displays it.
The searchContact() function:
- This function allows you to search for a particular entry from the address book list by entering a choice of first name or last name or address or contact. The function will return that entry as a result.
- Can be beneficial to use when we have a lot of entries in the booklist.
- This function can be performed after adding the contacts.
The editContact() function:
-
This function allows you to edit the entries. We can changes the values of the entries in the address booklist.
- The contact will be edited by entering the entry number that you wish to edit.
- If statement is used to check for the entry that will be edited upon your choice. It basically works by removing that contact entry and replacing it with new information entered.
-
This function can be performed after adding the contacts.
The deleteContact() function:
-
This function allows you to delete entries one by one permanently from the database.
- It will ask the user – Is this the contact that you wish to delete? (y or n). The user can select (y) for yes and (n) for no.
Conclusion
We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.
Electricity Bill Generator Project In C++(Source Code)
If you enjoyed the article and learned something new today, let us know in the comments.
Thank you.
Happy Reading! 🙂
ADVERTISEMENT