Contact Management System (CMS)
A Contact Management System (CMS) is an essential application for organizing and managing contacts efficiently. Here's a simple implementation of a Contact Management System using C, allows users to perform operations such as adding, viewing, searching, modifying, and deleting contacts through a simple and user-friendly console interface. The CMS provides a practical demonstration of fundamental programming concepts such as arrays, structures, and string manipulation, making it an excellent project for students and beginners in C programming.
Project Overview
The Contact Management System is designed to store basic information about contacts, including:
- Name: name of the person.
- Phone Number: phone number of the contact.
- Email Address: email address of the contact.
With this information, the program offers the following functionalities:
- Add New Contact
- View All Contacts
- Search for a Contact
- Modify a Contact
- Delete a Contact
- Exit the Program
Programming Concepts Used
This project makes use of several important programming concepts in C:
- Structures: A
struct
is used to group related information (name, phone, email) into a single data structure. - Arrays: An array of structures is used to store multiple contacts.
- String Handling: Functions like
fgets
andstrcspn
are used to manage input strings. - Control Structures:
if-else
statements and loops are utilized to manage program flow and user input. - Modular Programming: The program is divided into several functions, each responsible for a specific task.
Features and Functions
Let’s explore each function and feature of the Contact Management System in detail:
1. Add Contact
Function: addContact()
This function allows the user to add a new contact to the system. It prompts the user for the contact’s name, phone number, and email address. If the contact list has not reached its maximum limit (100 contacts), the new contact is stored in the contacts
array.
2. View All Contacts
Function: viewContacts()
This function displays all stored contacts, showing their names, phone numbers, and email addresses in a formatted list. If no contacts are available, it displays a message indicating that the contact list is empty.
3. Search Contact
Function: searchContact()
This function enables users to search for a contact by name. It prompts the user to enter the name of the contact they are looking for and performs a case-insensitive search through the contacts
array. If a match is found, the contact's details will be displayed; if not, a message stating 'Contact not found' will be shown.
4. Modify Contact
Function: modifyContact()
The function allows users to modify the details of an existing contact. It prompts the user to enter the name of the contact they wish to update. If the contact is found, the user can enter new details (name, phone number, and email). The information in the contacts
array is then updated accordingly.
5. Delete Contact
Function: deleteContact()
This function enables the user to remove a contact from the system. The user is prompted to enter the name of the contact they want to delete. If the contact is found, it is removed, and the remaining contacts are shifted up in the array to fill the gap. The contact count is then decremented.
6. Exit Program
Function: exit()
This function is called when the user chooses to exit the program. It terminates the program gracefully.
Program Flow
The main
function is the starting point of the program. It operates in an infinite loop and calls displayMenu()
, which displays the available options. Based on the user’s input, the corresponding function (e.g., addContact
, viewContacts
) is called.
#include
#include
#include
// Define a structure for the contact information
struct Contact {
char name[50];
char phone[15];
char email[50];
};
// Function prototypes
void addContact();
void viewContacts();
void searchContact();
void modifyContact();
void deleteContact();
void displayMenu();
// Global variables
struct Contact contacts[100];
int contactCount = 0;
int main() {
int choice;
while (1) {
displayMenu();
printf("\nEnter your choice: ");
scanf("%d", &choice);
getchar(); // Clear the input buffer
switch (choice) {
case 1:
addContact();
break;
case 2:
viewContacts();
break;
case 3:
searchContact();
break;
case 4:
modifyContact();
break;
case 5:
deleteContact();
break;
case 6:
printf("\nExiting...\n");
exit(0);
break;
default:
printf("\nInvalid choice! Please try again.\n");
}
}
return 0;
}
void displayMenu() {
printf("\n*** Contact Management System ***\n");
printf("1. Add New Contact\n");
printf("2. View All Contacts\n");
printf("3. Search Contact\n");
printf("4. Modify Contact\n");
printf("5. Delete Contact\n");
printf("6. Exit\n");
}
void addContact() {
if (contactCount < 100) {
struct Contact newContact;
printf("\nEnter Name: ");
fgets(newContact.name, sizeof(newContact.name), stdin);
newContact.name[strcspn(newContact.name, "\n")] = '\0'; // Remove newline
printf("Enter Phone Number: ");
fgets(newContact.phone, sizeof(newContact.phone), stdin);
newContact.phone[strcspn(newContact.phone, "\n")] = '\0'; // Remove newline
printf("Enter Email: ");
fgets(newContact.email, sizeof(newContact.email), stdin);
newContact.email[strcspn(newContact.email, "\n")] = '\0'; // Remove newline
contacts[contactCount++] = newContact;
printf("\nContact added successfully!\n");
} else {
printf("\nContact list is full!\n");
}
}
void viewContacts() {
if (contactCount == 0) {
printf("\nNo contacts available.\n");
return;
}
printf("\n*** Contact List ***\n");
for (int i = 0; i < contactCount; i++) {
printf("Contact %d:\n", i + 1);
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
printf("Email: %s\n\n", contacts[i].email);
}
}
void searchContact() {
char searchName[50];
printf("\nEnter the name to search: ");
fgets(searchName, sizeof(searchName), stdin);
searchName[strcspn(searchName, "\n")] = '\0'; // Remove newline
int found = 0;
for (int i = 0; i < contactCount; i++) {
if (strcasecmp(contacts[i].name, searchName) == 0) {
printf("\nContact found:\n");
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
printf("Email: %s\n", contacts[i].email);
found = 1;
break;
}
}
if (!found) {
printf("\nContact not found.\n");
}
}
void modifyContact() {
char modifyName[50];
printf("\nEnter the name of the contact to modify: ");
fgets(modifyName, sizeof(modifyName), stdin);
modifyName[strcspn(modifyName, "\n")] = '\0'; // Remove newline
int found = 0;
for (int i = 0; i < contactCount; i++) {
if (strcasecmp(contacts[i].name, modifyName) == 0) {
printf("\nModifying Contact:\n");
printf("Enter new name: ");
fgets(contacts[i].name, sizeof(contacts[i].name), stdin);
contacts[i].name[strcspn(contacts[i].name, "\n")] = '\0';
printf("Enter new phone number: ");
fgets(contacts[i].phone, sizeof(contacts[i].phone), stdin);
contacts[i].phone[strcspn(contacts[i].phone, "\n")] = '\0';
printf("Enter new email: ");
fgets(contacts[i].email, sizeof(contacts[i].email), stdin);
contacts[i].email[strcspn(contacts[i].email, "\n")] = '\0';
printf("\nContact modified successfully!\n");
found = 1;
break;
}
}
if (!found) {
printf("\nContact not found.\n");
}
}
void deleteContact() {
char deleteName[50];
printf("\nEnter the name of the contact to delete: ");
fgets(deleteName, sizeof(deleteName), stdin);
deleteName[strcspn(deleteName, "\n")] = '\0'; // Remove newline
int found = 0;
for (int i = 0; i < contactCount; i++) {
if (strcasecmp(contacts[i].name, deleteName) == 0) {
for (int j = i; j < contactCount - 1; j++) {
contacts[j] = contacts[j + 1];
}
contactCount--;
printf("\nContact deleted successfully!\n");
found = 1;
break;
}
}
if (!found) {
printf("\nContact not found.\n");
}
}
Output:
*** Contact Management System ***
1. Add New Contact
2. View All Contacts
3. Search Contact
4. Modify Contact
5. Delete Contact
6. Exit
Enter your choice:
Enter your choice: 1
Enter Name: dailyaspirants
Enter Phone Number: 4567893211
Enter Email: kumar@gmail.com
Contact added successfully!
*** Contact List ***
Contact 1:
Name: dailyaspirants
Phone: 4567893211
Email: kumar@gmail.com
Code Implementation
The entire code is written in a modular fashion, making it easier to read, maintain, and expand upon. Here is a snapshot of the main components:
- Structures: A
Contact
structure is defined to hold the details of each contact. - Global Variables: An array
contacts
holds up to 100 contacts, andcontactCount
keeps track of the number of stored contacts. - Functions: Each operation (add, view, search, modify, delete) is implemented as a separate function. This modular approach ensures that each part of the program is responsible for a specific task, making debugging and updates easier.
Advantages of the System
- User-Friendly: The program provides a simple and clear console interface, allowing users to manage their contacts without difficulty.
- Modular Design: Functions are split based on their purposes, improving code organization and readability.
- Efficient Searching: The system uses a simple linear search method, making it effective for small-scale use.
- Expandable: This project can be extended to include additional features like saving contacts to a file, sorting contacts alphabetically, or adding advanced search filters.
Limitations
- Fixed Contact Limit: The program is limited to storing a maximum of 100 contacts. In real-world applications, dynamic memory allocation or linked lists could be used to handle larger datasets.
- No Data Persistence: The current implementation only stores data in memory. When the program is terminated, all contact information is lost. Saving contacts to a file would be a useful enhancement.
- Basic Console Interface: The interface is simple but lacks graphical elements. A future upgrade could involve integrating with a GUI library or developing a web-based interface.
There are several ways to enhance and expand this Contact Management System:
- File Handling: Save contacts to a file so that they persist even after the program is closed. This would involve using file operations (`fopen`, `fwrite` etc.) in C.
Auto-Populate 100 Contacts:
The autoPopulateContacts() function adds 100 sample contacts if the contact file is initially empty.
Export Contacts:
The exportContacts() function exports all stored contacts to a text file (contacts_export.txt).
#include
#include
#include
// Define a structure for the contact information
struct Contact {
char name[50];
char phone[15];
char email[50];
};
// Function prototypes
void addContact(struct Contact newContact);
void autoPopulateContacts();
void viewContacts();
void searchContact();
void modifyContact();
void deleteContact();
void loadContacts();
void saveContacts();
void exportContacts();
void displayMenu();
// Global variables
struct Contact contacts[100];
int contactCount = 0;
const char *filename = "contacts.dat";
const char *exportFilename = "contacts_export.txt";
int main() {
loadContacts(); // Load contacts from file when the program starts
if (contactCount == 0) {
autoPopulateContacts(); // Populate with 100 contacts if empty
}
int choice;
while (1) {
displayMenu();
printf("\nEnter your choice: ");
scanf("%d", &choice);
getchar(); // Clear the input buffer
switch (choice) {
case 1: {
struct Contact newContact;
printf("\nEnter Name: ");
fgets(newContact.name, sizeof(newContact.name), stdin);
newContact.name[strcspn(newContact.name, "\n")] = '\0'; // Remove newline
printf("Enter Phone Number: ");
fgets(newContact.phone, sizeof(newContact.phone), stdin);
newContact.phone[strcspn(newContact.phone, "\n")] = '\0'; // Remove newline
printf("Enter Email: ");
fgets(newContact.email, sizeof(newContact.email), stdin);
newContact.email[strcspn(newContact.email, "\n")] = '\0'; // Remove newline
addContact(newContact);
break;
}
case 2:
viewContacts();
break;
case 3:
searchContact();
break;
case 4:
modifyContact();
break;
case 5:
deleteContact();
break;
case 6:
exportContacts();
break;
case 7:
saveContacts(); // Save contacts to file before exiting
printf("\nExiting...\n");
exit(0);
break;
default:
printf("\nInvalid choice! Please try again.\n");
}
}
return 0;
}
void displayMenu() {
printf("\n*** Contact Management System ***\n");
printf("1. Add New Contact\n");
printf("2. View All Contacts\n");
printf("3. Search Contact\n");
printf("4. Modify Contact\n");
printf("5. Delete Contact\n");
printf("6. Export Contacts to File\n");
printf("7. Exit\n");
}
void addContact(struct Contact newContact) {
if (contactCount < 100) {
contacts[contactCount++] = newContact;
printf("\nContact added successfully!\n");
saveContacts(); // Save the contact immediately
} else {
printf("\nContact list is full!\n");
}
}
void autoPopulateContacts() {
for (int i = 0; i < 100; i++) {
struct Contact tempContact;
snprintf(tempContact.name, sizeof(tempContact.name), "Contact %d", i + 1);
snprintf(tempContact.phone, sizeof(tempContact.phone), "123456789%d", i % 10);
snprintf(tempContact.email, sizeof(tempContact.email), "contact%d@example.com", i + 1);
addContact(tempContact);
}
printf("\n100 sample contacts auto-populated.\n");
}
void viewContacts() {
if (contactCount == 0) {
printf("\nNo contacts available.\n");
return;
}
printf("\n*** Contact List ***\n");
for (int i = 0; i < contactCount; i++) {
printf("Contact %d:\n", i + 1);
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
printf("Email: %s\n\n", contacts[i].email);
}
}
void searchContact() {
char searchName[50];
printf("\nEnter the name to search: ");
fgets(searchName, sizeof(searchName), stdin);
searchName[strcspn(searchName, "\n")] = '\0'; // Remove newline
int found = 0;
for (int i = 0; i < contactCount; i++) {
if (strcasecmp(contacts[i].name, searchName) == 0) {
printf("\nContact found:\n");
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
printf("Email: %s\n", contacts[i].email);
found = 1;
break;
}
}
if (!found) {
printf("\nContact not found.\n");
}
}
void modifyContact() {
char modifyName[50];
printf("\nEnter the name of the contact to modify: ");
fgets(modifyName, sizeof(modifyName), stdin);
modifyName[strcspn(modifyName, "\n")] = '\0'; // Remove newline
int found = 0;
for (int i = 0; i < contactCount; i++) {
if (strcasecmp(contacts[i].name, modifyName) == 0) {
printf("\nModifying Contact:\n");
printf("Enter new name: ");
fgets(contacts[i].name, sizeof(contacts[i].name), stdin);
contacts[i].name[strcspn(contacts[i].name, "\n")] = '\0';
printf("Enter new phone number: ");
fgets(contacts[i].phone, sizeof(contacts[i].phone), stdin);
contacts[i].phone[strcspn(contacts[i].phone, "\n")] = '\0';
printf("Enter new email: ");
fgets(contacts[i].email, sizeof(contacts[i].email), stdin);
contacts[i].email[strcspn(contacts[i].email, "\n")] = '\0';
printf("\nContact modified successfully!\n");
saveContacts(); // Save changes to file
found = 1;
break;
}
}
if (!found) {
printf("\nContact not found.\n");
}
}
void deleteContact() {
char deleteName[50];
printf("\nEnter the name of the contact to delete: ");
fgets(deleteName, sizeof(deleteName), stdin);
deleteName[strcspn(deleteName, "\n")] = '\0'; // Remove newline
int found = 0;
for (int i = 0; i < contactCount; i++) {
if (strcasecmp(contacts[i].name, deleteName) == 0) {
for (int j = i; j < contactCount - 1; j++) {
contacts[j] = contacts[j + 1];
}
contactCount--;
printf("\nContact deleted successfully!\n");
saveContacts(); // Save changes to file
found = 1;
break;
}
}
if (!found) {
printf("\nContact not found.\n");
}
}
void loadContacts() {
FILE *file = fopen(filename, "rb");
if (file != NULL) {
fread(&contactCount, sizeof(int), 1, file);
fread(contacts, sizeof(struct Contact), contactCount, file);
fclose(file);
}
}
void saveContacts() {
FILE *file = fopen(filename, "wb");
if (file != NULL) {
fwrite(&contactCount, sizeof(int), 1, file);
fwrite(contacts, sizeof(struct Contact), contactCount, file);
fclose(file);
} else {
printf("\nError saving contacts!\n");
}
}
void exportContacts() {
// Adjust the path to a location where you have permission to write
FILE *file = fopen(exportFilename, "w");
if (file != NULL) {
for (int i = 0; i < contactCount; i++) {
fprintf(file, "Contact %d:\n", i + 1);
fprintf(file, "Name: %s\n", contacts[i].name);
fprintf(file, "Phone: %s\n", contacts[i].phone);
fprintf(file, "Email: %s\n\n", contacts[i].email);
}
fclose(file);
printf("\nContacts exported to %s successfully!\n", exportFilename);
} else {
perror("Error exporting contacts");
}
}
Conclusion
The Contact Management System project is an excellent example of a beginner-friendly application that covers fundamental programming concepts such as structures, arrays, string manipulation, and modular programming in C. With a simple and intuitive interface, it serves as a useful tool for managing contacts and provides a solid foundation for further enhancements and learning opportunities in C programming.