C++ achieve address book management system

  • 2020-06-23 01:18:00
  • OfStack

This article example Shared C++ address book management system specific code 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, cannot add! " << 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 << " Input error, please re-input! " << endl;
 }
 
 cout << " Please enter your age: " << endl;
 int age = 0;
 cin >> age;
 abs->personArray[abs->m_Size].m_Age = age;
 
 cout << " Please enter your 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 << " Add a success " << 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; // Did not find 
}
 
// Delete contacts 
void deletePerson(Addressbooks * abs)
{
 cout << " Please enter the contact 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 successful! " << endl;
 }
 system("pause");
 system("cls");
}
 
// Find contacts 
void findPerson(Addressbooks * abs)
{
 cout << " Please enter the contact you are looking for: " << 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 contact 
void modifyPerson(Addressbooks * abs)
{
 cout << " Please enter the contact to be modified: " << 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 << " Input error, please re-input! " << endl;
 }
 
 cout << " Please enter your age: " << endl;
 int age = 0;
 cin >> age;
 abs->personArray[ret].m_Age = age;
 
 cout << " Please enter your 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 << " Modify the success " << endl;
 }
 else
 {
 cout << " No such person " << endl;
 }
 system("pause");
 system("cls");
}
 
// Empty contacts 
void cleanPerson(Addressbooks * abs)
{
 abs->m_Size = 0;
 cout << " The address book has been cleared " << endl;
 
 system("pause");
 system("cls");
}
// According to the menu 
void showMenu()
{
 cout << "*************************" << endl;
 cout << "***** 1 , adding contacts  *****" << endl;
 cout << "***** 2 , display contacts  *****" << endl;
 cout << "***** 3 , delete contacts  *****" << endl;
 cout << "***** 4 , find contacts  *****" << endl;
 cout << "***** 5 , modify the contact  *****" << endl;
 cout << "***** 6 , empty contacts  *****" << endl;
 cout << "***** 0 , exit the 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 contacts 
 addPerson(&abs);
 break;
 case 2: // Show contacts 
 showPerson(&abs);
 break;
 case 3: // Delete contacts 
 /*{
 cout << " Please enter delete contact name: " << endl;
 string name;
 cin >> name;
 if (isExist(&abs, name) == -1)
 {
 cout << " No such person " << endl;
 }
 else
 {
 cout << " Find the person " << endl;
 }
 }*/
 deletePerson(&abs);
 break;
 case 4: // Find contacts 
 findPerson(&abs);
 break;
 case 5: // Modify contact 
 modifyPerson(&abs);
 break;
 case 6: // Empty contacts 
 cleanPerson(&abs);
 break;
 case 0:
 cout << " Welcome to use next time. " << endl;
 system("pause");
 return 0;
 break;
 default:
 break;
 }
 }
 
 system("pause");
 return 0;
}

For more information, please pay attention to the topic management System Development.


Related articles: