C + + address book management system

Posted by HavokDelta6 on Sun, 07 Nov 2021 19:52:25 +0100

preface

After watching the video of the dark horse during this period, summarize from the case and review the old to know the new.

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: exit the currently used address book

2, Menu function

We need to create the same interface as the following figure: Here we only need to understand the function creation, call and the use of standard output stream cout in C + +, specifically:

#include<iostream>
using namespace std;

//Menu interface
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;
}
  • Summary of knowledge points:
    The first line is used to tell the compiler that we want to use the iostream library. The name in angle brackets (iostream in this example) indicates a header file.
    using namespace std; Where namespace means namespace, which can help avoid inadvertent name conflicts. The name of the C + + standard library definition is in the namespace STD.
    The namespace used by std standard library. std::cout means that we want to use the name cout defined in the namespace std. You can use cout directly by declaring that you use std.
    cout is an ostream object used to write data to standard output. It is usually used for the normal output of the program.
    < <: indicates the output operator.

3, Exit function

Function Description: exit the address book system

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

When the user selects 0, exit will be executed. If you select other options, you will not exit the program.
The operation effect is shown in the figure:

code:

int main() {

	int select = 0;//Create user selected input variables

	while (true)
	{
		showMenu();//Menu call

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

	system("pause");

	return 0;
}

The implementation of this function involves the use of while loop, switch statement and cin.
cin - an istream object used to read data from standard input.
>>: enter the operator.

4, Add contact

Function Description:

Realize the function of adding contacts, the maximum number of contacts is 1000, and 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

Function effect:

4.1 design contact structure

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

The design is as follows:

#Include < string > / / add string header file
//Contact structure
struct Person
{
	//full name
	string m_Name;
	//Gender: 1 male and 2 female
	int m_Sex;
	//Age
	int m_Age;
	//Telephone
	int m_Phone;
	//address
	string m_Addr;
};

This section is mainly about the creation and use of structures

4.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

The design is as follows

#define MAX 1000 / / maximum number of people - constant

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

};

Here we deal with the creation and use of structures and the use of define macro constants

4.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

//Add at the beginning of the mian function:

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

4.4 encapsulating and adding contact function

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 contact Code:

//1. Add contact information
void addPerson(Addressbooks * abs)
{
	//Determine whether the address book is full
	if (abs->m_Size == MAX)
	{
		cout << "The address book is full and cannot be added!" << endl;
		return;
	}
	else
	{
		string name;//Define name variable
		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;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "Error in input, please re-enter:" << endl;
		}
		//Age
		int age = 0;
		cout << "Please enter age:" << endl;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;
		//Telephone
		int phone = 0;
		cout << "Please enter the phone number:" << endl;
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;
		//address
		string address;
		cout << "Please enter the address:" << endl;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;
		//Number of updates
		abs->m_Size++;
		cout << "Input complete" << endl;

		system("pause");
		system("cls");
	}
}

Analysis of key points of adding contact function:
To define a function, we should first understand the following knowledge points: the form of function definition:
The return value consists of function name (formal parameter) {function body}
The return value of this function is void, the function name is addPerson, and the formal parameter is the structure pointer (Addressbooks * abs)
With this in mind, we can begin to write our function body:
We judge whether the address book is full. If it is full, we will not add it. If it is not full, we will add it.

4.5 test add contact function

In the selection interface, if the player selects 1, it means to add a contact. We can test this function

In the switch case statement, add the following to case1:

case 1:  //Add a Contact 
	addPerson(&abs);
	break;

5, Show contacts

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

Display contact implementation steps:

  • Encapsulate display contact function
  • Test display contact function

5.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

Display contact Code:

//2. Show all contact information
void showPerson(Addressbooks * abs)
{
	//Judge whether the number of people in the address book is 0. If it is 0, the prompt record is empty
	//If it is not 0, the recorded contact information will be 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 << endl;
		}
	}
	
	system("pause");
	system("cls");

}

The ternary operator is used here
expression? Expressions: expressions;
This example is: (ABS - > personarray [i]. M_sex = = 1? "Male": "female")
If you enter 1, you will return male, otherwise you will return female

5.2 test and display contact function

In the switch case statement, add case 2

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

6, 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

6.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, it will prompt the user that there is no contact to delete
    Therefore, we can package the detection of whether a contact exists into a function. If it exists, it returns the position of the contact in the address book, and if it does not exist, it returns - 1

Check whether the contact code exists:

//Judge whether there is a person to query. If there is, return the index position in the array. If not, return - 1
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;
}

6.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.

//3. Delete specified contact information
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 succeeded" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}

	system("pause");
	system("cls");
}

6.3 test the function of deleting contacts

In the switch case statement, add the following to case3:

case 3:  //Delete Contact 
	deletePerson(&abs);
	break;

7, Find contacts

Function Description: view the specified contact information by name

Find contact implementation steps

  • Encapsulate find contact function
  • Test find specified contact

7.1 encapsulating the contact search 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 Code:

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

	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;
	}

	system("pause");
	system("cls");

}

7.2 test find designated contact

In the switch case statement, add the following to case4:

case 4:  //find contact 
	findPerson(&abs);
	break;

8, 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

8.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 Code:

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

	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;

		cout << "Please enter gender:" << endl;
		cout << "1 -- male" << endl;
		cout << "2 -- female" << endl;

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

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

		//contact number
		cout << "Please enter the contact 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;

		cout << "Modified successfully" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}

	system("pause");
	system("cls");

}

8.2 test and modify contact function

In the switch case statement, add the following to case 5:

case 5:  //Modify contact
	modifyPerson(&abs);
	break;

9, Empty contacts

Function Description: clear all information in the address book

Empty contact implementation steps

  • Encapsulate empty contact function
  • Test empty contacts

9.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 contact Code:

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

9.2 test empty contact

In the switch case statement, add the following to case 6:

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

10, Complete program

#include<iostream>
using namespace std;
#include<string>
#define MAX 1000 / / macro constant
/*
Implementation steps:
1,Display an interface
2,Enter 0 to exit
3,Add contact (with structure)
*/

//Contact structure
struct Person
{
	//full name
	string m_Name;
	//Gender: 1 male and 2 female
	int m_Sex;
	//Age
	int m_Age;
	//Telephone
	int m_Phone;
	//address
	string m_Addr;
};
//Design address book structure
struct Addressbooks
{
	//Contact array saved in address book
	struct Person personArray[MAX];
	//Number of contacts currently recorded in the address book
	int m_Size;
};


//Encapsulate the function display interface, such as void showMenu()
//- invoke encapsulated functions in the main function

//Menu interface
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;
}

//1. Add contact
void addPerson(Addressbooks * abs)
{
	//full name
	if (abs->m_Size == MAX)
	{
		cout << "The address book is full and cannot be added!" << endl;
		return;
	}
	else
	{
		string name;//Define name variable
		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;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "Error in input, please re-enter:" << endl;
		}
		//Age
		int age = 0;
		cout << "Please enter age:" << endl;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;
		//Telephone
		int phone = 0;
		cout << "Please enter the phone number:" << endl;
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;
		//address
		string address;
		cout << "Please enter the address:" << endl;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;
		//Number of updates
		abs->m_Size++;
		cout << "Input complete" << endl;

		system("pause");
		system("cls");
	}
}
//2. Show all contacts
void showPerson(Addressbooks * abs)
{
	//Judge whether the number of people in the address book is 0. If it is 0, the prompt record is empty
	//If it is not 0, the recorded contact information will be 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 << endl;

		}
		system("pause");
		system("cls");
	}
}

//Delete the contact, and delete the specified contact by name
//Check whether the contact exists. If it exists - return the specific location of the contact in the array. If it does not exist - Return - 1
int isExist(Addressbooks * abs, string name)
{
	//Parameter 1: address book, parameter 2: name comparison
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
	}
	return -1;
}
//3. Delete specified contact
void delPerson(Addressbooks * abs)
{
	cout << "Please enter the contact you want to delete" << endl;
	string name;
	cin >> name;
	//ret==-1, not found, RET=- 1. I found it
	int ret = isExist(abs, name);

	if (ret != -1)
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			//Move the data forward. If you want to delete Li Si, let the people behind overwrite it
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;//Update contacts
		cout << "Contact deleted" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}
}

//4. Find a contact and view the specified contact information by name
void findPerson(Addressbooks * abs)
{
	cout << "Please enter the contact you are looking for" << endl;
	string name;
	cin >> name;
	//Judge whether the specified 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 == 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 one was found" << endl;
	}
	system("pause");
	system("cls");
}

//5. Modify contact information
void modifyPerson(Addressbooks * abs)
{
	cout << "Please enter the contact you need to modify" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1)
	{
		string name;
		//full name
		cout << "Please enter the modified Name:" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;
		//Gender
		cout << "Please enter the modified 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 << "Error in input, please re-enter:" << endl;
		}
		//Age
		int age = 0;
		cout << "Please enter the modification age:" << endl;
		cin >> age;
		abs->personArray[ret].m_Age = age;
		//Telephone
		int phone = 0;
		cout << "Please enter the modified phone number:" << endl;
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;
		//address
		string address;
		cout << "Please enter the modification address:" << endl;
		cin >> address;
		abs->personArray[ret].m_Addr = address;
		cout << "Modified successfully" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}
	system("pause");
	system("cls");
}
//6. Empty all contacts
void cleanPerson(Addressbooks * abs)
{
	abs->m_Size = 0;
	cout << "The address book has been emptied" << endl;
	//press any key to continue
	system("pause");
	system("cls");

}

int main()
{
	//Create address book structure variable
	struct Addressbooks abs;
	//Initialize the current number of people in the address book
	abs.m_Size = 0;

	int select = 0;//Create user selected input variables
	while (true)
	{
		//Menu call
		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
			break;
		case 3://3. Delete contact
			/*{cout << "Please enter the name of the deleted contact: "< endl;
			string name;
			cin >> name;
			if (isExist(&abs, name) == -1)
			{
			cout << "Check that there is no such person "< < endl;
			}
			else
			{
			cout << "Find this person "< < endl;
			}

			break;
			}	*/
			delPerson(&abs);
			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 << "Exit the address book. Welcome to use it next time" << endl;
			system("pause");
			return 0;
			break;

		default:
			break;
		}
	}
	
			


	system("pause");
	return 0;
}

summary

I hope you can communicate with me, leave messages or private letters, study together and make progress together!

Topics: C++