Implementing a Contact Management System Using C++ Structs
Struct Design for Contact Managemant
A contact management system requires two main data structures: one for individual contacts and another for the contact list itself.
Contact Structure Definition
struct ContactEntry {
string fullName;
int genderCode; // 1 for male, 2 for female
int ageValue;
string phoneNumber;
string homeAddress;
};
Address Book Structure Definition
#define MAX_CONTACTS 100
struct ContactDirectory {
ContactEntry entries[MAX_CONTACTS];
int currentCount;
};
User Interface and Menu System
Menu Display Function
void displayMenu() {
cout << "1. Add New Contact" << endl;
cout << "2. Display All Contacts" << endl;
cout << "3. Remove Contact" << endl;
cout << "4. Search Contact" << endl;
cout << "5. Update Contact" << endl;
cout << "6. Clear All Contacts" << endl;
cout << "0. Exit Program" << endl;
}
Main Program Loop
int main() {
int userChoice;
ContactDirectory directory;
directory.currentCount = 0;
while (true) {
displayMenu();
cin >> userChoice;
switch (userChoice) {
case 1: addContact(&directory); break;
case 2: showContacts(&directory); break;
case 3: removeContact(&directory); break;
case 4: searchContact(&directory); break;
case 5: updateContact(&directory); break;
case 6: clearDirectory(&directory); break;
case 0:
cout << "Goodbye!" << endl;
system("pause");
return 0;
default: break;
}
}
return 0;
}
Core Function Implementations
Adding a New Contact
void addContact(ContactDirectory* dir) {
if (dir->currentCount == MAX_CONTACTS) {
cout << "Directory is full" << endl;
return;
}
ContactEntry newContact;
cout << "Enter full name: " << endl;
cin >> newContact.fullName;
int genderInput;
while (true) {
cout << "Enter gender (1=Male, 2=Female): " << endl;
cin >> genderInput;
if (genderInput == 1 || genderInput == 2) {
newContact.genderCode = genderInput;
break;
}
cout << "Invalid input, try again" << endl;
}
cout << "Enter age: " << endl;
cin >> newContact.ageValue;
cout << "Enter phone number: " << endl;
cin >> newContact.phoneNumber;
cout << "Enter address: " << endl;
cin >> newContact.homeAddress;
dir->entries[dir->currentCount] = newContact;
dir->currentCount++;
cout << "Contact added successfully" << endl;
system("pause");
system("cls");
}
Displaying All Contacts
void showContacts(ContactDirectory* dir) {
if (dir->currentCount == 0) {
cout << "No contacts available" << endl;
return;
}
for (int i = 0; i < dir->currentCount; i++) {
cout << "Name: " << dir->entries[i].fullName << "\t";
cout << "Gender: " << (dir->entries[i].genderCode == 1 ? "Male" : "Female") << "\t";
cout << "Age: " << dir->entries[i].ageValue << "\t";
cout << "Phone: " << dir->entries[i].phoneNumber << "\t";
cout << "Address: " << dir->entries[i].homeAddress << endl;
}
system("pause");
system("cls");
}
Contact Search Utility
int findContactIndex(ContactDirectory* dir, string targetName) {
if (dir->currentCount == 0) {
cout << "Directory is empty" << endl;
return -1;
}
for (int i = 0; i < dir->currentCount; i++) {
if (dir->entries[i].fullName == targetName) {
return i;
}
}
return -1;
}
Removing a Contact
void removeContact(ContactDirectory* dir) {
string targetName;
cout << "Enter name to remove: " << endl;
cin >> targetName;
int index = findContactIndex(dir, targetName);
if (index == -1) {
cout << "Contact not found" << endl;
return;
}
for (int i = index; i < dir->currentCount - 1; i++) {
dir->entries[i] = dir->entries[i + 1];
}
dir->currentCount--;
cout << "Contact removed successfully" << endl;
system("pause");
system("cls");
}
Searching for a Contact
void searchContact(ContactDirectory* dir) {
string targetName;
cout << "Enter name to search: " << endl;
cin >> targetName;
int index = findContactIndex(dir, targetName);
if (index == -1) {
cout << "Contact not found" << endl;
return;
}
cout << "Name: " << dir->entries[index].fullName << "\t";
cout << "Gender: " << (dir->entries[index].genderCode == 1 ? "Male" : "Female") << "\t";
cout << "Age: " << dir->entries[index].ageValue << "\t";
cout << "Phone: " << dir->entries[index].phoneNumber << "\t";
cout << "Address: " << dir->entries[index].homeAddress << endl;
system("pause");
system("cls");
}
Upadting Contact Information
void updateContact(ContactDirectory* dir) {
string targetName;
cout << "Enter name to update: " << endl;
cin >> targetName;
int index = findContactIndex(dir, targetName);
if (index == -1) {
cout << "Contact not found" << endl;
return;
}
cout << "Enter new name: " << endl;
cin >> dir->entries[index].fullName;
int genderInput;
while (true) {
cout << "Enter new gender (1=Male, 2=Female): " << endl;
cin >> genderInput;
if (genderInput == 1 || genderInput == 2) {
dir->entries[index].genderCode = genderInput;
break;
}
cout << "Invalid input, try again" << endl;
}
cout << "Enter new age: " << endl;
cin >> dir->entries[index].ageValue;
cout << "Enter new phone: " << endl;
cin >> dir->entries[index].phoneNumber;
cout << "Enter new address: " << endl;
cin >> dir->entries[index].homeAddress;
cout << "Contact updated successfully" << endl;
system("pause");
system("cls");
}
Clearing All Contacts
void clearDirectory(ContactDirectory* dir) {
dir->currentCount = 0;
cout << "All contacts cleared" << endl;
system("pause");
system("cls");
}