Cover Image for Contact Management System in C
54 views

Contact Management System in C

A contact management system in C allows you to create, store, retrieve, and manage contact information such as names, phone numbers, and email addresses. Below is a basic implementation of a contact management system in C:

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CONTACTS 100

// Structure to represent a contact
struct Contact {
    char name[100];
    char phone[15];
    char email[100];
};

// Function to add a new contact
void addContact(struct Contact contacts[], int *contactCount) {
    if (*contactCount >= MAX_CONTACTS) {
        printf("Contact list is full. Cannot add more contacts.\n");
        return;
    }

    struct Contact newContact;

    printf("Enter Name: ");
    scanf("%s", newContact.name);

    printf("Enter Phone: ");
    scanf("%s", newContact.phone);

    printf("Enter Email: ");
    scanf("%s", newContact.email);

    contacts[*contactCount] = newContact;
    (*contactCount)++;

    printf("Contact added successfully.\n");
}

// Function to display all contacts
void displayContacts(struct Contact contacts[], int contactCount) {
    if (contactCount == 0) {
        printf("No contacts to display.\n");
        return;
    }

    printf("Name\tPhone\tEmail\n");
    for (int i = 0; i < contactCount; i++) {
        printf("%s\t%s\t%s\n", contacts[i].name, contacts[i].phone, contacts[i].email);
    }
}

int main() {
    struct Contact contacts[MAX_CONTACTS];
    int contactCount = 0;
    int choice;

    while (1) {
        printf("\nContact Management System Menu:\n");
        printf("1. Add Contact\n");
        printf("2. Display Contacts\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addContact(contacts, &contactCount);
                break;
            case 2:
                displayContacts(contacts, contactCount);
                break;
            case 3:
                exit(0);
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;
}

In this code, we’ve created a simple contact management system with the following features:

  1. You can add new contacts, including their name, phone number, and email address.
  2. You can display the details of all contacts in the system.
  3. The system uses an array of structures (struct Contact) to store contact data.
  4. The maximum number of contacts is defined by MAX_CONTACTS.
  5. The program provides a menu to interact with the system, allowing you to add contacts, display contact records, and exit the program.

This is a basic implementation for educational purposes. In a real-world scenario, you may want to enhance the system with error handling, data validation, the ability to edit or delete contacts, and data persistence (e.g., saving and loading contact data from files).

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS