Address Book Management System Development C++ Implementation

Posted by AbraCadaver on Thu, 06 Jan 2022 18:03:25 +0100

Preface

Today, I was invited by a friend to implement the development of an address book management system. The function requirements are: complete address book functions, including new, find, display, modify, delete, empty and other basic functions. After careful consideration, it is not very difficult, just need to use the basic knowledge of C++ to complete, in addition, You can also extend some preprocessing capabilities for errors to enhance usability.

On this basis, we also put forward some other requirements, such as the function needs to be written in a file, function separation, the same function must be implemented in the same function, and so on. This still requires some clarity of our thinking. Next, we start to analyze.

If you are in a hurry, you can skip to the full code at the end, copy and paste it and run it directly to see the results.

1. Idea Analysis

The whole project is roughly divided into three files, contacts.h Stores header files and contact structures, along with declarations of all functions, contacts_function stores all function implementations, contacts_main is used to store the main function. In fact, many projects can use this idea to subfile code.

First, we will analyze all the functions needed by the whole project. First, we can output the menu for the user to choose and read the user's options with one function. New, find, display, modify, delete and empty functions are all completed with one function. Many of these functions are based on search. If you want to integrate these functions into one lookup function, This function then requires an additional parameter to separate functions to accommodate different invocations.

The main function only needs to call the corresponding function according to the function selected by the user to implement the corresponding function. The ideas are basically organized, so let's start writing code based on the current ideas.

2. Function Implementation

1. Contact structure

Storing a lot of information about your contacts, there's no doubt that we need a structure array. First, we build a contact structure that contains the name, gender, age, number, and address information of your contacts.

The code is as follows:

struct contact
{
	// The name defaults to Undefined
	string name = "Undefined";
	
	// For easy storage, tentative: female-0, male-1
	bool gender = 0;
	int age = 0;
	string phone_number;
	string address;
};

2. Menu Functions

The menu function is mainly responsible for outputting the user selection interface and reading in the user selection function. The code structure is simple.

The code is as follows:

/* Output menu directory for user selection, return key */
int menu()
{
	cout << "Welcome to the Alkaid#Address Book Management System \n\n";
	cout << "1.New Contact\n";
	cout << "2.Show Contacts\n";
	cout << "3.find contact\n";
	cout << "4.Modify Contacts\n";
	cout << "5.Delete Contact\n";
	cout << "6.Empty Contact\n";
	cout << "0.Exit Address Book\n\n";
	cout << "Please select the function you want: ";

	int key = 0;

	cin >> key;

	return key;
}

3. New Contact Function

This function is responsible for adding contacts in the specified location, so the corresponding address is required to modify the corresponding content.

The code is as follows:

/* Add contacts at specified locations */
void add_contact(contact* p_added)
{
	// Preprocessing of input errors, if the pointer is empty, determines user input errors, does not operate
	if (p_added == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	cout << "Please enter the name of the contact:";
	cin >> p_added->name;

	cout << "Please enter the contact's gender (female)-0 male-1):";
	cin >> p_added->gender;

	cout << "Please enter the contact's age:";
	cin >> p_added->age;

	cout << "Please enter a contact number:";
	cin >> p_added->phone_number;

	cout << "Please enter the address of the contact:";
	cin >> p_added->address;

	cout << "Save successfully!\n";

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

	return;
}

4. Show Contact Functions

Considering that there are two kinds of road conditions for displaying contacts: one is to find the corresponding contacts and display them according to the user's selection, the other is to display all the contacts stored in the address book, so an additional parameter is needed to control the function status and make different outputs.

The code is as follows:

/* Display specific contacts by address according to specified criteria */
/* ord==0,Show all contacts in turn */
/* ord==1,Show Designated Contacts */
void display_contacts(contact* p_show, int ord)
{
    // Preprocessing of input errors, if the pointer is empty, determines user input errors, does not operate
	if (p_show == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	if (ord == 0)
	{
		for (int i = 0; p_show->name != "Undefined"; i++)
		{
			cout << "No." << i + 1 << "The first contact is:\n";
			cout << p_show->name << "  " << p_show->gender << "  " << p_show->age << "  " << p_show->phone_number << "  " << p_show->address << endl;
			p_show++;
		}
	}

	if (ord == 1)
	{
		cout << "The contact you want to see is:\n";
		cout << p_show->name << "  " << p_show->gender << "  " << p_show->age << "  " << p_show->phone_number << "  " << p_show->address << endl;
	}

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

	return;
}

5. Delete Contact Function

We only need to find the address where the contact is stored based on the name of the contact provided by the user and initialize the address there.

The code is as follows:

/* Delete contacts by address */
void delete_contact(contact* p_deleted)
{
	if (p_deleted == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	p_deleted->name = "Undefined";

	cout << "Delete succeeded!\n";

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

	return;
}

5. Modify Contact Functions

Modifying a contact is easy. We only need to find the user-specified contact address and re-read the data from that address.

The code is as follows:

/* Modify contacts based on address */
void modify_contact(contact* p_modified)
{
	if (p_modified == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	cout << "Please enter the name of the modified contact:";
	cin >> p_modified->name;

	cout << "Please enter the gender of the modified contact (female)-0 male-1):";
	cin >> p_modified->gender;

	cout << "Please enter the age of the modified contact:";
	cin >> p_modified->age;

	cout << "Please enter the contact number of the modified contact:";
	cin >> p_modified->phone_number;

	cout << "Please enter the address of the modified contact:";
	cin >> p_modified->address;

	cout << "Successfully modified!\n";

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

	return;
}

6. Empty Contact Function

Simply get the first address of the structure array, initialize all arrays, and empty the address book.

The code is as follows:

/* Empty all contacts */
void empty_contact(contact* p_emptied)
{
	for (int i = 0; p_emptied->name != "Undefined"; i++)
	{
		p_emptied->name = "Undefined";
		p_emptied++;
	}

	cout << "Empty!\n";

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

	return;
}

7. Find Functions

Find functions, even though they play a role in many functions, also use an instruction parameter to classify the functions to find corresponding addresses under different conditions. It is important to note that the return value of the lookup function is an address somewhere in the structure array, so it can be used directly as a parameter to some function.

/* Find the contact location for the specified criteria and return the address for that location */
/* ord==0,Find locations to add */
/* ord==1,Location to delete based on name */
/* ord==2,Find the location you want to show by name */
/* ord==3,Location to change based on name */
contact* find(contact alkaid[100], int ord)
{
	contact* p = alkaid;
	string name_chosen = "Undefined";

	if (ord == 0)
	{
		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "Address book is full, please delete contact\n";
				p = NULL;
				break;
			}
		}
	}

	if (ord == 1)
	{
		cout << "Please enter the name of the contact you want to delete:";
		cin >> name_chosen;

		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "No contacts to delete were queried\n";
				p = NULL;
				break;
			}
		}

	}

	if (ord == 2)
	{
		cout << "Please enter the name of the contact you want to find:";
		cin >> name_chosen;

		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "No contacts were queried to find\n";
				p = NULL;
				break;
			}
		}

	}

	if (ord == 3)
	{
		cout << "Please enter the name of the contact you want to modify:";
		cin >> name_chosen;

		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "No contact was queried for modification\n";
				p = NULL;
				break;
			}
		}

	}
	return p;
}

8. Principal Function

On the basis of having completed all the function functions, order can call the corresponding function in the main function.

The code is as follows:

int main()
{
	// The alkaid array holds information for 100 contacts
	contact alkaid[100];

	// Print menu, provides options for the user, and uses key to indicate the function selected by the user
	int key = 0;
	
	for (;;)
	{
		key = menu();

		if (key == 1)
		{
			add_contact(find(alkaid, 0));
		}
		else if (key == 2)
		{
			display_contacts(alkaid, 0);
		}
		else if (key == 3)
		{
			display_contacts(find(alkaid, 2), 1);
			
		}
		else if (key == 4)
		{
			modify_contact(find(alkaid, 3));
		}
		else if (key == 5)
		{
			delete_contact(find(alkaid, 1));
		}
		else if (key == 6)
		{
			empty_contact(alkaid);
		}
		else if (key == 0)
		{
			break;
		}
	}
	return 0;
}

3. Complete Code

For your convenience, I organized the code into a file, you just need to copy and paste to run, you can rest assured that you can use it, run results will not be shown here.

The code is as follows:

/* Alkaid#3529 */

#include<iostream>
#include<string>
#include<windows.h>
using namespace std;

struct contact
{
	// The name defaults to Undefined
	string name = "Undefined";

	// For easy storage, tentative: female-0, male-1
	bool gender = 0;
	int age = 0;
	string phone_number;
	string address;
};

/* Output menu directory for user selection, return key */
int menu();

/* Find the contact location for the specified criteria and return the address for that location */
/* ord==0,Find locations to add */
/* ord==1,Location to delete based on name */
/* ord==2,Find the location you want to show by name */
/* ord==3,Location to change based on name */
contact* find(contact alkaid[100], int ord);

/* Add contacts at specified locations */
void add_contact(contact* p_added);

/* Display specific contacts by address according to specified criteria */
/* ord==0,Show all contacts in turn */
/* ord==1,Show Designated Contacts */
void display_contacts(contact* p_show, int ord);

/* Delete contacts by address */
void delete_contact(contact* p_deleted);

/* Modify contacts based on address */
void modify_contact(contact* p_modified);

/* Empty all contacts */
void empty_contact(contact* p_emptied);

int main()
{
	// The alkaid array holds information for 100 contacts
	contact alkaid[100];

	// Print menu, provides options for the user, and uses key to indicate the function selected by the user
	int key = 0;

	for (;;)
	{
		key = menu();

		if (key == 1)
		{
			add_contact(find(alkaid, 0));
		}
		else if (key == 2)
		{
			display_contacts(alkaid, 0);
		}
		else if (key == 3)
		{
			display_contacts(find(alkaid, 2), 1);

		}
		else if (key == 4)
		{
			modify_contact(find(alkaid, 3));
		}
		else if (key == 5)
		{
			delete_contact(find(alkaid, 1));
		}
		else if (key == 6)
		{
			empty_contact(alkaid);
		}
		else if (key == 0)
		{
			break;
		}
	}
	return 0;
}

int menu()
{
	cout << "Welcome to the Alkaid#Address Book Management System \n\n";
	cout << "1.New Contact\n";
	cout << "2.Show Contacts\n";
	cout << "3.find contact\n";
	cout << "4.Modify Contacts\n";
	cout << "5.Delete Contact\n";
	cout << "6.Empty Contact\n";
	cout << "0.Exit Address Book\n\n";
	cout << "Please select the function you want: ";

	int key = 0;

	cin >> key;

	return key;
}

contact* find(contact alkaid[100], int ord)
{
	contact* p = alkaid;
	string name_chosen = "Undefined";

	if (ord == 0)
	{
		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "Address book is full, please delete contact\n";
				p = NULL;
				break;
			}
		}
	}

	if (ord == 1)
	{
		cout << "Please enter the name of the contact you want to delete:";
		cin >> name_chosen;

		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "No contacts to delete were queried\n";
				p = NULL;
				break;
			}
		}

	}

	if (ord == 2)
	{
		cout << "Please enter the name of the contact you want to find:";
		cin >> name_chosen;

		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "No contacts were queried to find\n";
				p = NULL;
				break;
			}
		}

	}

	if (ord == 3)
	{
		cout << "Please enter the name of the contact you want to modify:";
		cin >> name_chosen;

		for (int i = 0; alkaid[i].name != name_chosen; i++)
		{
			p++;
			if (i == 99)
			{
				cout << "No contact was queried for modification\n";
				p = NULL;
				break;
			}
		}

	}


	return p;
}

void add_contact(contact* p_added)
{
	// Preprocessing of input errors, if the pointer is empty, determines user input errors, does not operate
	if (p_added == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	cout << "Please enter the name of the contact:";
	cin >> p_added->name;

	cout << "Please enter the contact's gender (female)-0 male-1):";
	cin >> p_added->gender;

	cout << "Please enter the contact's age:";
	cin >> p_added->age;

	cout << "Please enter a contact number:";
	cin >> p_added->phone_number;

	cout << "Please enter the address of the contact:";
	cin >> p_added->address;

	cout << "Save successfully!\n";

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

	return;
}

void display_contacts(contact* p_show, int ord)
{
	if (p_show == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	if (ord == 0)
	{
		for (int i = 0; p_show->name != "Undefined"; i++)
		{
			cout << "No." << i + 1 << "The first contact is:\n";
			cout << p_show->name << "  " << p_show->gender << "  " << p_show->age << "  " << p_show->phone_number << "  " << p_show->address << endl;
			p_show++;
		}
	}

	if (ord == 1)
	{
		cout << "The contact you want to see is:\n";
		cout << p_show->name << "  " << p_show->gender << "  " << p_show->age << "  " << p_show->phone_number << "  " << p_show->address << endl;
	}

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

	return;
}

void delete_contact(contact* p_deleted)
{
	if (p_deleted == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	p_deleted->name = "Undefined";

	cout << "Delete succeeded!\n";

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

	return;
}

void modify_contact(contact* p_modified)
{
	if (p_modified == NULL)
	{
		system("pause");
		system("cls");
		return;
	}

	cout << "Please enter the name of the modified contact:";
	cin >> p_modified->name;

	cout << "Please enter the gender of the modified contact (female)-0 male-1):";
	cin >> p_modified->gender;

	cout << "Please enter the age of the modified contact:";
	cin >> p_modified->age;

	cout << "Please enter the contact number of the modified contact:";
	cin >> p_modified->phone_number;

	cout << "Please enter the address of the modified contact:";
	cin >> p_modified->address;

	cout << "Successfully modified!\n";

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

	return;
}

void empty_contact(contact* p_emptied)
{
	for (int i = 0; p_emptied->name != "Undefined"; i++)
	{
		p_emptied->name = "Undefined";
		p_emptied++;
	}

	cout << "Empty!\n";

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

	return;
}

summary

I'm in a hurry. I've also been blogging for this long time for the first time. I haven't made any further improvements to this address book management system, I haven't had time to add more detailed comments, and I have a lot more reasonable points to handle. If you're interested, I'll update a more complete and clear code later.

Developing this system also takes me a little bit of work. If you think it's good, you might as well give me a compliment. Thank you.

Finally, I'm Alkaid3529, looking forward to your attention!

Topics: C++ Back-end