SoFunction
Updated on 2025-03-11

C++ implements address book management system

This article shares the specific code of the C++ address book management system for your reference. The specific content is as follows

#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
 
struct Person
{
 string m_Name;
 int m_Sex;
 int m_Age;
 string m_Phone;
 string m_Addr;
};
 
struct Addressbooks
{
 struct Person personArray[MAX];
 int m_Size;
};
 
void addPerson(Addressbooks * abs)
{
 if (abs->m_Size == MAX)
 {
 cout << "The address book is full and cannot be added!" << endl;
 return;
 }
 else
 {
 string name;
 cout << "Please enter your name:" << endl;
 cin >> name;
 abs->personArray[abs->m_Size].m_Name = name;
 
 cout << "Please enter gender:" << endl;
 cout << "1 --- Male" << endl;
 cout << "2 --- Female" << endl;
 int sex = 0;
 
 while (true)
 {
 cin >> sex;
 if (sex == 1 || sex == 2)
 {
 abs->personArray[abs->m_Size].m_Sex = sex;
 break;
 }
 cout << "If the input is incorrect, please re-enter!" << endl;
 }
 
 cout << "Please enter age:" << endl;
 int age = 0;
 cin >> age;
 abs->personArray[abs->m_Size].m_Age = age;
 
 cout << "Please enter the contact number:" << endl;
 string phone;
 cin >> phone;
 abs->personArray[abs->m_Size].m_Phone = phone;
 
 cout << "Please enter your home address:" << endl;
 string address;
 cin >> address;
 abs->personArray[abs->m_Size].m_Addr = address;
 
 abs->m_Size++;
 
 cout << "Added successfully" << endl;
 
 system("pause");
 system("cls");
 }
}
 
void showPerson(Addressbooks * abs)
{
 if (abs->m_Size == 0)
 {
 cout << "The current record is empty" << endl;
 
 }
 else
 {
 for (int i = 0; i < abs->m_Size; i++)
 {
 cout << "Name:" << abs->personArray[i].m_Name << "\t";
 cout << "gender:" << (abs->personArray[i].m_Sex == 1 ? "male":"female" ) << "\t";
 cout << "age:" << abs->personArray[i].m_Age << "\t";
 cout << "Telephone:" << abs->personArray[i].m_Phone << "\t";
 cout << "address:" << abs->personArray[i].m_Addr << endl;
 
 }
 }
 system("pause");
 system("cls");
 
}
 
int isExist(Addressbooks * abs, string name)
{
 for (int i = 0; i < abs->m_Size; i++)
 {
 if (abs->personArray[i].m_Name == name)
 {
 return i;
 }
 }
 return -1; //Not found}
 
//Delete the contactvoid deletePerson(Addressbooks * abs)
{
 cout << "Please enter the contact person you want to delete:" << endl;
 string name;
 cin >> name;
 
 int ret = isExist(abs, name);
 
 if (ret != -1)
 {
 for (int i = ret; i < abs->m_Size; i++)
 {
 abs->personArray[i] = abs->personArray[i + 1];
 }
 abs->m_Size--;
 cout << "Delete successfully!" << endl;
 }
 system("pause");
 system("cls");
}
 
//Find contactvoid findPerson(Addressbooks * abs)
{
 cout << "Please enter the contact person you want to find:" << endl;
 string name;
 cin >> name;
 
 int ret = isExist(abs, name);
 
 if (ret != -1)
 {
 cout << "Name:" << abs->personArray[ret].m_Name << "\t";
 cout << "gender:" << (abs->personArray[ret].m_Sex == 1 ? "male" : "female") << "\t";
 cout << "age:" << abs->personArray[ret].m_Age << "\t";
 cout << "Telephone:" << abs->personArray[ret].m_Phone << "\t";
 cout << "address:" << abs->personArray[ret].m_Addr << endl;
 }
 else
 {
 cout << "No such person" << endl;
 }
 system("pause");
 system("cls");
}
 
//Modify the contactvoid modifyPerson(Addressbooks * abs)
{
 cout << "Please enter the contact person to modify:" << endl;
 string name;
 cin >> name;
 
 int ret = isExist(abs, name);
 
 if (ret != -1)
 {
 string name;
 cout << "Please enter your name:" << endl;
 cin >> name;
 abs->personArray[ret].m_Name = name;
 
 cout << "Please enter gender:" << endl;
 cout << "1 --- Male" << endl;
 cout << "2 --- Female" << endl;
 int sex = 0;
 
 while (true)
 {
 cin >> sex;
 if (sex == 1 || sex == 2)
 {
 abs->personArray[ret].m_Sex = sex;
 break;
 }
 cout << "If the input is incorrect, please re-enter!" << endl;
 }
 
 cout << "Please enter age:" << endl;
 int age = 0;
 cin >> age;
 abs->personArray[ret].m_Age = age;
 
 cout << "Please enter the contact number:" << endl;
 string phone;
 cin >> phone;
 abs->personArray[ret].m_Phone = phone;
 
 cout << "Please enter your home address:" << endl;
 string address;
 cin >> address;
 abs->personArray[ret].m_Addr = address;
 
 cout << "Modification was successful" << endl;
 }
 else
 {
 cout << "No such person" << endl;
 }
 system("pause");
 system("cls");
}
 
//Clear the contactvoid cleanPerson(Addressbooks * abs)
{
 abs->m_Size = 0;
 cout << "The address book has been cleared" << endl;
 
 system("pause");
 system("cls");
}
//Show menuvoid showMenu()
{
 cout << "*************************" << endl;
 cout << "*** 1. Add a contact ****" << endl;
 cout << "*** 2. Show contact ****" << endl;
 cout << "*** 3. Delete contact ****" << endl;
 cout << "*** 4. Find contact ****" << endl;
 cout << "*** 5. Modify the contact ****" << endl;
 cout << "*** 6. Clear contact ****" << endl;
 cout << "*** 0. Exit address book ****" << endl;
 cout << "*************************" << endl;
 
}
 
int main() 
{
 Addressbooks abs;
 abs.m_Size = 0;
 int select = 0;
 
 while (true)
 {
 showMenu();
 
 cin >> select;
 switch (select)
 {
 case 1: //Add a contact addPerson(&abs);
 break;
 case 2: //Show contacts showPerson(&abs);
 break;
 case 3: //Delete the contact /*{
  cout << "Please enter the name of the delete contact: " << endl;
  string name;
  cin >> name;
  if (isExist(&abs, name) == -1)
  {
  cout << "No such person is found" << endl;
  }
  else
  {
  cout << "Finding this person" << endl;
  }
  }*/
 deletePerson(&amp;abs);
 break;
 case 4: //Find contact findPerson(&amp;abs);
 break;
 case 5: //Modify the contact modifyPerson(&amp;abs);
 break;
 case 6: //Clear the contact cleanPerson(&amp;abs);
 break;
 case 0:
 cout &lt;&lt; "Welcome to use next time" &lt;&lt; endl;
 system("pause");
 return 0;
 break;
 default:
 break;
 }
 }
 
 system("pause");
 return 0;
}

For more study materials, please pay attention to the special topic "Management system development》。

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.