C + + address book management system

1 system requirements

Address book is a tool that can record the information of relatives and friends.

The functions to be realized in the system are as follows:

  • Add contact: add a new person to the address book. The information includes (name, gender, age, contact number, home address) and records up to 1000 people
  • Show contacts: displays all contact information in the address book
  • Delete contact: delete the specified contact by name
  • Find contact: view the specified contact information by name
  • Modify contact: modify the specified contact according to the name
  • Empty contact: clear all information in the address book
  • Exit address book: launches the currently used address book

2 create project

  • Create a new project
  • Add file

3 menu functions

Function Description: the interface for users to select functions

Steps:

  • The encapsulated function displays this interface, such as void showMenu()
  • Invoke encapsulated functions in the main function
/*
Address book management system
 Dark horse tutorial
2021/11/16
*/
#include<iostream>
using namespace std;

void showMenu()
{
	cout << "***************************" << endl;
	cout << "*****  1,Add a Contact   *****" << endl;
	cout << "*****  2,Show contacts  *****" << endl;
	cout << "*****  3,Delete Contact   *****" << endl;
	cout << "*****  4,find contact   *****" << endl;
	cout << "*****  5,Modify contact  *****" << endl;
	cout << "*****  6,Empty contacts  *****" << endl;
	cout << "*****  0,Exit address book  *****" << endl;
	cout << "***************************" << endl;
}
int main() {

	showMenu();


	system("pause");
	return 0;
}

4 exit function

Function Description: exit the address book system.

Idea: according to different choices of users and different functions, you can choose the switch branch structure to build the whole architecture.

When the user selects 0, he / she will exit. If he / she chooses other options, he / she will not exit the program.

int main() {

	int select = 0;

	while (true)
	{
		showMenu();

		cin >> select;

		switch (select)
		{
		case 1://1. Add contact
			break;
		case 2://2. Show contacts
			break;
		case 3://3. Delete contact
			break;
		case 4://4. Find contacts
			break;
		case 5://5. Modify contact
			break;
		case 6://6. Empty contacts
			break;
		case 0://0. Exit the address book
			cout << "Welcome to use next time" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}

	system("pause");
	return 0;
}

5 add contact

Function Description: realize the function of adding contacts. The maximum number of contacts is 1000. The contact information includes (name, gender, age, contact phone number and home address)

Implementation steps for adding contacts:

  • Design contact structure
  • Design address book structure
  • Create address book in main function
  • Encapsulate add contact function
  • Test add contact function

5.1 design contact structure

Contact information includes: name, gender, age, contact number and home address

//Contact structure
struct Person
{
	string m_Name;//full name
	int m_Sex;//Gender: 1 male and 2 female
	int m_Age;//Age
	string m_Phone;//Telephone
	string m_Addr;//address
};

5.2 design address book structure

During design, an array of contacts with a capacity of 1000 can be maintained in the address book structure, and the number of contacts in the current address book can be recorded.

#define MAX 1000 / / maximum number of people in the address book

//Address book structure
struct Addressbooks
{
	Person personArray[MAX];//Contact array saved in address book
	int m_size;//Number of people in the address book
};

5.3 create address book in main function

After the add contact function is encapsulated, create an address book variable in the main function, which is the address book we need to maintain all the time.

stay main Function start add	

    //Create address book structure variable
	Addressbooks abs;
	//Initialize the current number of people in the address book
	abs.m_Size = 0;

5.4 encapsulating and adding contact functions

Idea: before adding a contact, first judge whether the address book is full. If it is full, it will not be added. If it is not full, add the new contact information to the address book one by one.

//Add a Contact 
void addPerson(Addressbooks* abs)
{
	//Judge whether the address book is full. If it is full, it will not be added
	if (abs->m_Size == MAX)
	{
		cout << "The address book is full and cannot be added!" << endl;
		return;
	}
	else
	{
		//Add specific contacts

		//full name
		string name;
		cout << "Please enter your name" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name = name;

		//Gender
		cout << "Please enter gender:" << endl;
		cout << "1 --- male" << endl;
		cout << "2 --- female" << endl;
		string sex;

		while (true)
		{
			cin >> sex;
			if (sex == "1" || sex == "2")
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "Input error, please re-enter" << endl;
		}
		
		//Age
		cout << "Please enter age:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;

		//Telephone
		cout << "Please enter the phone number:" << endl;
		int phone = 0;
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;

		//Home address
		cout << "Please enter your home address:" << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;

		//Number of contacts updated
		abs->m_Size++;

		cout << "Successfully added!" << endl;

		system("pause");//Please press any key to continue
		system("cls");//Screen clearing operation
	}
}

6 Display contacts

Function Description: displays the existing contact information in the address book

Display contact implementation steps:

  • Encapsulate display contact function
  • Test display contact function

6.1 encapsulated display contact function

Idea: if there is no person in the current address book, it will prompt that the record is empty and the number of people is greater than 0, and the information in the address book will be displayed.

//Show contacts
void showPerson(Addressbooks * abs)
{
	//When the address book is empty, the prompt record is empty; otherwise, the address book information is displayed
	if (abs->m_Size == 0)
	{
		cout << "Current record is empty!" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "full 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 << "\t";
		}
	}

	system("pause");//press any key to continue
	system("cls");//Clear screen
}

6.2 test and display contact function

Call function
		case 2://2. Show contacts
			showPerson(&abs);
			break;

  7 delete contact

Function Description: delete the specified contact by name

To delete a contact:

  • Package to detect whether the contact exists
  • Encapsulate delete contact function
  • Test delete contact function

7.1 whether the package inspection contact exists

Design idea: before deleting a contact, we need to judge whether the contact entered by the user exists. If it exists, there is no prompt that the user has no contact to delete. Therefore, we can package the detection of whether the contact exists in a function. If it exists, we return the position of the contact in the address book, and if it does not exist, we return - 1.

//Check whether the contact exists. If it exists, return the position of the contact in the address book. If it does not exist, return - 1.
int isExist(Addressbooks* abs, string name)//Valuepoint 1 address book valuepoint 2 Comparison name
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
		return -1;
	}
}

7.2 encapsulating and deleting contact function

Judge whether there is this person in the address book according to the contact entered by the user;

Find it and delete it, and prompt that the deletion is successful;

No prompt found, no one found.

//Delete specified contact
void deletePerson(Addressbooks* abs)
{
	cout << "Please enter the contact you want to delete" << endl;

	string name;
	cin >> name;

	//ret==-1 not found
	//ret!=- I found it
	int ret = isExist(abs, name);

	if (ret != -1)
	{
		//Find this person and delete it
		for (int i = ret; i < abs->m_Size; i++)
		{
			//data migration 
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;//Update the number of people in the address book
		cout << "Delete succeeded" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}
}

8 find contacts

Function Description: view the specified contact information by name

Find contact implementation steps

  • Encapsulate find contact function
  • Test find specified contact

8.1 encapsulating the contact lookup function

Implementation idea: judge whether the contact specified by the user exists. If there is a display information, it will prompt that there is no such person.

//find contact 
void findPerson(Addressbooks* abs)
{
	cout << "Please enter the contact you want to find" << endl;
	string name;
	cin >> name;

	//Determine whether the contact exists in the address book
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "full name:" << abs->personArray[ret].m_Name << "\t";
		cout << "Gender:" << abs->personArray[ret].m_Sex << "\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 one was found" << endl;
	}

	//Press any key to clear the screen
	system("pause");
	system("cls");
}

8.2 test find designated contact

		case 4://4. Find contacts
			findPerson(&abs);
			break;

9 modify contact

Function Description: re modify the specified contact according to the name

Implementation steps of modifying contact:

  • Encapsulating and modifying contact functions
  • Test and modify contact function

9.1 encapsulating and modifying contact functions

Implementation idea: find the contact entered by the user. If the search is successful, modify it, and if the search fails, you will be prompted that there is no such person

//Modify contact
void modifyPerson(Addressbooks* abs)
{
	cout << "Please enter the contact you want to modify:" << endl;
	string name;
	cin >> name;

	//Determine whether the contact exists in the address book
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		//full name
		string name;
		cout << "Please enter your name" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;

		//Gender
		cout << "Please enter gender:" << endl;
		cout << "1 --- male" << endl;
		cout << "2 --- female" << endl;
		string sex;

		while (true)
		{
			cin >> sex;
			if (sex == "1" || sex == "2")
			{
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "Input error, please re-enter" << endl;
		}

		//Age
		cout << "Please enter age:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[ret].m_Age = age;

		//Telephone
		cout << "Please enter the phone number:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;

		//Home address
		cout << "Please enter your home address:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_Addr = address;
	}
	else
	{
		cout << "No one was found" << endl;
	}

	//Press any key to clear the screen
	system("pause");
	system("cls");
}

10 empty contacts

Function Description: clear all information in the address book

Implementation steps of clearing contacts:

  • Encapsulate empty contact function
  • Test empty contacts

10.1 encapsulate empty contact function

Implementation idea: clear all contact information in the address book. Just set the number of contacts recorded in the address book to 0 and clear it logically

//Empty all contacts
void cleanPerson(Addressbooks* abs)
{
	abs->m_Size = 0;
	cout << "The address book has been emptied!" << endl;
	system("pause");
	system("cls");
}

10.2 test empty contact

		case 6://6. Empty contacts
			cleanPerson(&abs);
			break;

Overall Code:

/*
Address book management system
 Dark horse tutorial
2021/11/16
*/
#include<iostream>
using namespace std;
#include<string>

//Contact structure
struct Person
{
	string m_Name;//full name
	string m_Sex;//Gender: 1 male and 2 female
	int m_Age;//Age
	string m_Phone;//Telephone
	string m_Addr;//address
};

#define MAX 1000 / / maximum number of people in the address book

//Address book structure
struct Addressbooks
{
	Person personArray[MAX];//Contact array saved in address book
	int m_Size;//Number of people in the address book
};

//Display menu
void showMenu()
{
	cout << "***************************" << endl;
	cout << "*****  1,Add a Contact   *****" << endl;
	cout << "*****  2,Show contacts  *****" << endl;
	cout << "*****  3,Delete Contact   *****" << endl;
	cout << "*****  4,find contact   *****" << endl;
	cout << "*****  5,Modify contact  *****" << endl;
	cout << "*****  6,Empty contacts  *****" << endl;
	cout << "*****  0,Exit address book  *****" << endl;
	cout << "***************************" << endl;
}

//Add a Contact 
void addPerson(Addressbooks* abs)
{
	//Judge whether the address book is full. If it is full, it will not be added
	if (abs->m_Size == MAX)
	{
		cout << "The address book is full and cannot be added!" << endl;
		return;
	}
	else
	{
		//Add specific contacts

		//full name
		string name;
		cout << "Please enter your name" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name = name;

		//Gender
		cout << "Please enter gender:" << endl;
		cout << "1 --- male" << endl;
		cout << "2 --- female" << endl;
		string sex;

		while (true)
		{
			cin >> sex;
			if (sex == "1" || sex == "2")
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "Input error, please re-enter" << endl;
		}
		
		//Age
		cout << "Please enter age:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;

		//Telephone
		cout << "Please enter the phone number:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;

		//Home address
		cout << "Please enter your home address:" << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;

		//Number of contacts updated
		abs->m_Size++;

		cout << "Successfully added!" << endl;

		system("pause");//Please press any key to continue
		system("cls");//Screen clearing operation
	}
}

//Show contacts
void showPerson(Addressbooks * abs)
{
	//When the address book is empty, the prompt record is empty; otherwise, the address book information is displayed
	if (abs->m_Size == 0)
	{
		cout << "Current record is empty!" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "full 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 << "\t";
			cout << endl;
		}
	}

	system("pause");//press any key to continue
	system("cls");//Clear screen
}

//Check whether the contact exists. If it exists, return the position of the contact in the address book. If it does not exist, return - 1.
int isExist(Addressbooks* abs, string name)//Valuepoint 1 address book valuepoint 2 Comparison name
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
		return -1;
	}
}

//Delete specified contact
void deletePerson(Addressbooks* abs)
{
	cout << "Please enter the contact you want to delete" << endl;

	string name;
	cin >> name;

	//ret==-1 not found
	//ret!=- I found it
	int ret = isExist(abs, name);

	if (ret != -1)
	{
		//Find this person and delete it
		for (int i = ret; i < abs->m_Size; i++)
		{
			//data migration 
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;//Update the number of people in the address book
		cout << "Delete succeeded" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}
	
	//Press any key to clear the screen
	system("pause");
	system("cls");
}

//find contact 
void findPerson(Addressbooks* abs)
{
	cout << "Please enter the contact you want to find" << endl;
	string name;
	cin >> name;

	//Determine whether the contact exists in the address book
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "full name:" << abs->personArray[ret].m_Name << "\t";
		cout << "Gender:" << abs->personArray[ret].m_Sex << "\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 one was found" << endl;
	}

	//Press any key to clear the screen
	system("pause");
	system("cls");
}

//Modify contact
void modifyPerson(Addressbooks* abs)
{
	cout << "Please enter the contact you want to modify:" << endl;
	string name;
	cin >> name;

	//Determine whether the contact exists in the address book
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		//full name
		string name;
		cout << "Please enter your name" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;

		//Gender
		cout << "Please enter gender:" << endl;
		cout << "1 --- male" << endl;
		cout << "2 --- female" << endl;
		string sex;

		while (true)
		{
			cin >> sex;
			if (sex == "1" || sex == "2")
			{
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "Input error, please re-enter" << endl;
		}

		//Age
		cout << "Please enter age:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[ret].m_Age = age;

		//Telephone
		cout << "Please enter the phone number:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;

		//Home address
		cout << "Please enter your home address:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_Addr = address;
	}
	else
	{
		cout << "No one was found" << endl;
	}

	//Press any key to clear the screen
	system("pause");
	system("cls");
}

//Empty all contacts
void cleanPerson(Addressbooks* abs)
{
	abs->m_Size = 0;
	cout << "The address book has been emptied!" << endl;
	system("pause");
	system("cls");
}

int main() {

	//Create address book structure variable
	Addressbooks abs;
	//Initialize the current number of people in the address book
	abs.m_Size = 0;

	int select = 0;

	while (true)
	{
		showMenu();

		cin >> select;

		switch (select)
		{
		case 1://1. Add contact
			addPerson(&abs);//Using address passing, you can modify arguments
			break;
		case 2://2. Show contacts
			showPerson(&abs);
			break;
		case 3://3. Delete contact
			deletePerson(&abs);
			break;
		case 4://4. Find contacts
			findPerson(&abs);
			break;
		case 5://5. Modify contact
			modifyPerson(&abs);
			break;
		case 6://6. Empty contacts
			cleanPerson(&abs);
			break;
		case 0://0. Exit the address book
			cout << "Welcome to use next time" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}

	system("pause");
	return 0;
}

Keywords: C++ Back-end

Added by AndyBG on Tue, 16 Nov 2021 16:50:40 +0200