C++ - Phonebook Application

Phonebook Application Project in C++

Introduction

A C++ phonebook application is a simple program that allows users to store, retrieve, update, and delete contact information such as names, phone numbers, and addresses. The main purpose of this project is to provide hands-on experience with file handling, arrays or vectors, and basic CRUD (Create, Read, Update, Delete) operations in C++.

Project Features

  • Adding Contacts: The application allows users to add new contacts to the phonebook.
  • Displaying Contacts: The phonebook displays all stored contacts, including their names, phone numbers, and addresses.
  • Searching Contacts: Users can search for a contact by name and view the corresponding details.
  • Deleting Contacts: Users can remove a contact from the phonebook by name.
  • Updating Contacts: Users can update the details of an existing contact.
  • File Handling: The contact information is stored in a file, allowing users to retain the data even after the program is closed.

Project Structure

The project can be structured as follows:

  • Contact Class: A class to represent a contact with attributes like name, phone number, and address.
  • File Handling: Functions to read from and write to a text or binary file for storing contact data.
  • Menu System: A menu-driven interface to allow users to perform actions such as add, display, search, update, and delete contacts.

Contact Class

The Contact class stores the data for each contact. It includes member variables for the contact's name, phone number, and address, as well as getter and setter functions.

Contact Class Structure


class Contact {
private:
    std::string name;
    std::string phone;
    std::string address;

public:
    // Constructor to initialize contact details
    Contact(std::string name, std::string phone, std::string address)
        : name(name), phone(phone), address(address) {}

    // Getters and setters
    std::string getName() { return name; }
    std::string getPhone() { return phone; }
    std::string getAddress() { return address; }

    void setName(std::string name) { this->name = name; }
    void setPhone(std::string phone) { this->phone = phone; }
    void setAddress(std::string address) { this->address = address; }

    // Function to display contact information
    void display() {
        std::cout << "Name: " << name << "\nPhone: " << phone << "\nAddress: " << address << std::endl;
    }
};
    

File Handling

The phonebook application must store the contact data in a file. You can use file handling functions in C++ such as ofstream for writing and ifstream for reading files. Contacts will be read from and written to a text file or binary file.

Writing to a File


#include 

void writeToFile(std::vector& contacts) {
    std::ofstream file("phonebook.txt");

    for (const auto& contact : contacts) {
        file << contact.getName() << "\n";
        file << contact.getPhone() << "\n";
        file << contact.getAddress() << "\n";
    }

    file.close();
}
    

Reading from a File


#include 
#include 

void readFromFile(std::vector& contacts) {
    std::ifstream file("phonebook.txt");
    std::string name, phone, address;

    while (std::getline(file, name) && std::getline(file, phone) && std::getline(file, address)) {
        contacts.push_back(Contact(name, phone, address));
    }

    file.close();
}
    

Menu System

The application will present a menu to the user, allowing them to select an action. This can be implemented using a simple loop with switch or if statements to handle user input.

Menu Example


#include 
#include 

void displayMenu() {
    std::cout << "1. Add Contact\n";
    std::cout << "2. Display Contacts\n";
    std::cout << "3. Search Contact\n";
    std::cout << "4. Update Contact\n";
    std::cout << "5. Delete Contact\n";
    std::cout << "6. Exit\n";
}

int main() {
    std::vector contacts;
    int choice;

    while (true) {
        displayMenu();
        std::cin >> choice;

        switch (choice) {
            case 1:
                // Add a new contact
                break;
            case 2:
                // Display all contacts
                break;
            case 3:
                // Search a contact by name
                break;
            case 4:
                // Update a contact's information
                break;
            case 5:
                // Delete a contact
                break;
            case 6:
                std::cout << "Exiting the program...\n";
                return 0;
            default:
                std::cout << "Invalid option. Try again.\n";
                break;
        }
    }

    return 0;
}
    

Adding Contacts

To add a contact, prompt the user for input, create a new Contact object, and add it to the list of contacts.

Adding a Contact Example


void addContact(std::vector& contacts) {
    std::string name, phone, address;

    std::cout << "Enter name: ";
    std::cin.ignore();
    std::getline(std::cin, name);
    
    std::cout << "Enter phone number: ";
    std::getline(std::cin, phone);
    
    std::cout << "Enter address: ";
    std::getline(std::cin, address);
    
    contacts.push_back(Contact(name, phone, address));
}
    

Updating Contacts

To update a contact, prompt the user for the contact’s name, locate the contact in the list, and update the information as needed.

Updating a Contact Example


void updateContact(std::vector& contacts) {
    std::string name;
    std::cout << "Enter the name of the contact to update: ";
    std::cin.ignore();
    std::getline(std::cin, name);

    for (auto& contact : contacts) {
        if (contact.getName() == name) {
            std::string phone, address;
            std::cout << "Enter new phone number: ";
            std::getline(std::cin, phone);
            std::cout << "Enter new address: ";
            std::getline(std::cin, address);

            contact.setPhone(phone);
            contact.setAddress(address);

            std::cout << "Contact updated successfully.\n";
            return;
        }
    }
    std::cout << "Contact not found.\n";
}
    


The C++ Phonebook Application project is a great way to practice file handling, object-oriented programming (OOP), and basic CRUD operations. By developing this application, you will enhance your understanding of C++ programming, data structures, and file management.

logo

C++

Beginner 5 Hours

Phonebook Application Project in C++

Introduction

A C++ phonebook application is a simple program that allows users to store, retrieve, update, and delete contact information such as names, phone numbers, and addresses. The main purpose of this project is to provide hands-on experience with file handling, arrays or vectors, and basic CRUD (Create, Read, Update, Delete) operations in C++.

Project Features

  • Adding Contacts: The application allows users to add new contacts to the phonebook.
  • Displaying Contacts: The phonebook displays all stored contacts, including their names, phone numbers, and addresses.
  • Searching Contacts: Users can search for a contact by name and view the corresponding details.
  • Deleting Contacts: Users can remove a contact from the phonebook by name.
  • Updating Contacts: Users can update the details of an existing contact.
  • File Handling: The contact information is stored in a file, allowing users to retain the data even after the program is closed.

Project Structure

The project can be structured as follows:

  • Contact Class: A class to represent a contact with attributes like name, phone number, and address.
  • File Handling: Functions to read from and write to a text or binary file for storing contact data.
  • Menu System: A menu-driven interface to allow users to perform actions such as add, display, search, update, and delete contacts.

Contact Class

The Contact class stores the data for each contact. It includes member variables for the contact's name, phone number, and address, as well as getter and setter functions.

Contact Class Structure

class Contact { private: std::string name; std::string phone; std::string address; public: // Constructor to initialize contact details Contact(std::string name, std::string phone, std::string address) : name(name), phone(phone), address(address) {} // Getters and setters std::string getName() { return name; } std::string getPhone() { return phone; } std::string getAddress() { return address; } void setName(std::string name) { this->name = name; } void setPhone(std::string phone) { this->phone = phone; } void setAddress(std::string address) { this->address = address; } // Function to display contact information void display() { std::cout << "Name: " << name << "\nPhone: " << phone << "\nAddress: " << address << std::endl; } };

File Handling

The phonebook application must store the contact data in a file. You can use file handling functions in C++ such as ofstream for writing and ifstream for reading files. Contacts will be read from and written to a text file or binary file.

Writing to a File

#include void writeToFile(std::vector& contacts) { std::ofstream file("phonebook.txt"); for (const auto& contact : contacts) { file << contact.getName() << "\n"; file << contact.getPhone() << "\n"; file << contact.getAddress() << "\n"; } file.close(); }

Reading from a File

#include #include void readFromFile(std::vector& contacts) { std::ifstream file("phonebook.txt"); std::string name, phone, address; while (std::getline(file, name) && std::getline(file, phone) && std::getline(file, address)) { contacts.push_back(Contact(name, phone, address)); } file.close(); }

Menu System

The application will present a menu to the user, allowing them to select an action. This can be implemented using a simple loop with switch or if statements to handle user input.

Menu Example

#include #include void displayMenu() { std::cout << "1. Add Contact\n"; std::cout << "2. Display Contacts\n"; std::cout << "3. Search Contact\n"; std::cout << "4. Update Contact\n"; std::cout << "5. Delete Contact\n"; std::cout << "6. Exit\n"; } int main() { std::vector contacts; int choice; while (true) { displayMenu(); std::cin >> choice; switch (choice) { case 1: // Add a new contact break; case 2: // Display all contacts break; case 3: // Search a contact by name break; case 4: // Update a contact's information break; case 5: // Delete a contact break; case 6: std::cout << "Exiting the program...\n"; return 0; default: std::cout << "Invalid option. Try again.\n"; break; } } return 0; }

Adding Contacts

To add a contact, prompt the user for input, create a new Contact object, and add it to the list of contacts.

Adding a Contact Example

void addContact(std::vector& contacts) { std::string name, phone, address; std::cout << "Enter name: "; std::cin.ignore(); std::getline(std::cin, name); std::cout << "Enter phone number: "; std::getline(std::cin, phone); std::cout << "Enter address: "; std::getline(std::cin, address); contacts.push_back(Contact(name, phone, address)); }

Updating Contacts

To update a contact, prompt the user for the contact’s name, locate the contact in the list, and update the information as needed.

Updating a Contact Example

void updateContact(std::vector& contacts) { std::string name; std::cout << "Enter the name of the contact to update: "; std::cin.ignore(); std::getline(std::cin, name); for (auto& contact : contacts) { if (contact.getName() == name) { std::string phone, address; std::cout << "Enter new phone number: "; std::getline(std::cin, phone); std::cout << "Enter new address: "; std::getline(std::cin, address); contact.setPhone(phone); contact.setAddress(address); std::cout << "Contact updated successfully.\n"; return; } } std::cout << "Contact not found.\n"; }


The C++ Phonebook Application project is a great way to practice file handling, object-oriented programming (OOP), and basic CRUD operations. By developing this application, you will enhance your understanding of C++ programming, data structures, and file management.

Related Tutorials

Frequently Asked Questions for C++

A void pointer is a special type of pointer that can point to any data type, making it versatile for generic data handling.

Dynamic memory allocation in C++ refers to allocating memory at runtime using operators like new and delete, providing flexibility in memory management.

Templates in C++ allow functions and classes to operate with generic types, enabling code reusability and type safety.

Iterators are objects that allow traversal through the elements of a container in the STL, providing a uniform way to access elements.

C++ is an object-oriented programming language that extends C by adding features like classes, inheritance, and polymorphism. Unlike C, which is procedural, C++ supports both procedural and object-oriented paradigms.

An array in C++ is declared by specifying the type of its elements followed by the array name and size in square brackets, e.g., int arr[10];.

The new operator allocates memory dynamically on the heap, while the delete operator deallocates memory, preventing memory leaks.

Type casting in C++ is the process of converting a variable from one data type to another, either implicitly or explicitly.

Inheritance is a feature in C++ where a new class (derived class) acquires properties and behaviors (methods) from an existing class (base class).

Operator overloading enables the redefinition of the way operators work for user-defined types, allowing operators to be used with objects of those types.

Function overloading allows multiple functions with the same name but different parameters to coexist in a C++ program, enabling more intuitive function calls.

In C++, a class is declared using the class keyword, followed by the class name and a pair of curly braces containing member variables and functions.

No, a C++ program cannot execute without a main() function, as it is the designated entry point for program execution.

Vectors are dynamic arrays provided by the STL in C++ that can grow or shrink in size during program execution.

A namespace in C++ is a declarative region that provides a scope to the identifiers (names of types, functions, variables) to avoid name conflicts.

The primary difference is that members of a struct are public by default, whereas members of a class are private by default.

The const keyword in C++ is used to define constants, indicating that the value of a variable cannot be changed after initialization.

Exception handling in C++ is a mechanism to handle runtime errors using try, catch, and throw blocks, allowing a program to continue execution after an error.

The STL is a collection of template classes and functions in C++ that provide general-purpose algorithms and data structures like vectors, lists, and maps.

A reference in C++ is an alias for another variable, whereas a pointer holds the memory address of a variable. References cannot be null and must be initialized upon declaration.

Pointers in C++ are variables that store memory addresses of other variables. They allow for dynamic memory allocation and efficient array handling.

Polymorphism allows objects of different classes to be treated as objects of a common base class, enabling a single function or operator to work in different ways.

Constructors are special member functions that initialize objects when they are created. Destructors are called when objects are destroyed, used to release resources.

These access specifiers define the accessibility of class members. Public members are accessible from outside the class, private members are not, and protected members are accessible within the class and by derived classes.

The main() function serves as the entry point for a C++ program. It is where the execution starts and ends.

line

Copyrights © 2024 letsupdateskills All rights reserved