<> Address book management system
Knock the code for hours , It is finally completed. , ah ~~~ On the way, I met many difficulties bug, But after debugging again and again , Or has it successfully made the following functions :
<>0, Exit address book
<>1, Add a Contact
<>2, Show contacts
<>3, Delete Contact
<>4, find contact
<>5, Modify contact
<>6, Clear contacts
Beginners C++ Not long , This is also the first time to try , The code needs to be optimized , It is suitable for beginners , Ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha . Also hope to have a warm-hearted boss , Guys can help me point out my mistakes , I've tested quite a lot , It shouldn't be a big problem .
The source code is as follows , Take away the small partners you need , Of course, it's best to praise and encourage !
#include<iostream> using namespace std; #include<string> #define Max 100
// Create contact structure struct person { string name; int age = 0; int sex = 0;//1-> male 2-> female
string phone; }; // Create address book structure struct addressBooks { // Contact structure array struct person
personArr[Max]; // Number of contacts currently recorded in the address book int size = 0; }; 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, Clear contacts ************************" << endl; cout <<
"************************ 0, Exit address book ************************" << endl; cout <<
"***************************************************************" << endl; }
//1, Add a Contact void addPerson(struct addressBooks* abs) { // Address book full , You can't add any more if (abs->
size== Max) { cout << " Address book full , Unable to add !" << endl; return; } else {// Add specific contacts // full name cout
<< " Please enter your name :" << endl; string name; cin >> name; abs->personArr[abs->size].name =
name; // Gender cout << " Please enter gender :" << endl; cout << "1--- male " << endl; cout << "2--- female " <<
endl; int sex = 0; while (1) { cin >> sex; if (sex == 1 || sex == 2) { abs->
personArr[abs->size].sex = sex; break; } cout << " Wrong input , Please re-enter !" << endl; } // Age
cout<< " Please enter age :" << endl; int age = 0; cin >> age; abs->personArr[abs->size].age
= age; // Telephone cout << " Please enter the phone number :" << endl; string phone; cin >> phone; abs->personArr
[abs->size].phone = phone; // Update the number of members in the address book abs->size++; cout << " Added successfully !" << endl;
system("pause");// suspend system("cls");// Clear screen } } // Check if the contact exists int detectPerson(struct
addressBooks* abs, string name) { for (int i = 0; i < abs->size; i++) { if (name
== abs->personArr[i].name) {// Find a contact return i;// i Returns the location in the contact array } } return -1;
// Contact not found return -1 } //2, Show contacts void showBooks(struct addressBooks abs) { if (abs.size
== 0) { cout << " Contact is empty !" << endl; } else { cout << " Current contact list :" << endl; for (int
i= 0; i < abs.size; i++) { cout << " full name :" << abs.personArr[i].name << "\t Gender :" <<
(abs.personArr[i].sex==1?" male ":" female ")//=1 Then output male Otherwise output female << "\t Age :" << abs.personArr[i]
.age << "\t Telephone :" << abs.personArr[i].phone << endl; } } system("pause");// suspend
system("cls");// Clear screen } //3, Delete contact Renren void deletePerson(struct addressBooks* abs) {
cout<< " Please enter the name of the contact to be deleted :" << endl; string name; int y = 0; cin >> name; y=
detectPerson(abs, name);// Check if the contact exists if (y != -1) {// Find a contact , Delete for (int i = y; i
< abs->size; i++) { // Move data forward one bit abs->personArr[i] = abs->personArr[i + 1]; } abs->
size--;// Number of contacts updated cout << " Successfully deleted !" << endl; } else {// Contact not found cout << " Contact not found " <<
endl; } system("pause");// suspend system("cls");// Clear screen } //4, find contact void findPerson(
struct addressBooks abs) { string name; int n = 0;// To save 0,1 0 The representative didn't find a contact ,1 Representative found contact
cout<< " Enter the name of the contact you want to find :"; cin >> name; for (int i = 0; i < abs.size; i++) { if (name
== abs.personArr[i].name) {// Find a contact cout << " full name :" << abs.personArr[i].name <<
"\t Gender :" << (abs.personArr[i].sex == 1 ? " male " : " female ")//=1 Then output male Otherwise output female << "\t Age :" <<
abs.personArr[i].age << "\t Telephone :" << abs.personArr[i].phone << endl; n = 1; } if
(i+1 == abs.size && n == 1) {// Loop to find the end of the function , You can find out all the contacts with the same name ,n=1 Description contact found system(
"pause");// suspend system("cls");// Clear screen } if (i + 1 == abs.size && n == 0) {//n=0
Description contact not found cout << " Contact not found !" << endl; system("pause");// suspend system("cls");// Clear screen } }
} //5, Modify contact void alterPerson(struct addressBooks* abs) { cout <<
" Please enter the name of the contact you want to modify :" << endl; string name; int y = 0; cin >> name; y = detectPerson(
abs, name);// Check if the contact exists if (y != -1) {// Find a contact , Make changes // full name cout << " Please enter your name :" << endl;
string name; cin >> name; abs->personArr[y].name = name; // Gender cout << " Please enter gender :"
<< endl; cout << "1--- male " << endl; cout << "2--- female " << endl; int sex = 0; while (1
) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArr[y].sex = sex; break;
} cout << " Wrong input , Please re-enter !" << endl; } // Age cout << " Please enter age :" << endl; int age = 0;
cin>> age; abs->personArr[y].age = age; // Telephone cout << " Please enter the phone number :" << endl; string
phone; cin >> phone; abs->personArr[y].phone = phone; cout << " Modified successfully !" << endl; }
else {// Contact not found ! cout << " Contact not found !" << endl; } system("pause");// suspend system("cls");
// Clear screen } //6, Clear contacts void emptyBooks(struct addressBooks *abs) { for (int i = 0; i
<= abs->size; i++) { // Move data forward one bit abs->personArr[i] = abs->personArr[i + 1]; } abs->
size= 0;// After clearing, the contact is 0 individual ; cout << " Contacts cleared !" << endl; system("pause"); system("cls");
} int main() { // Create address book struct addressBooks abs; // Initializes the current number of people in the address book abs.size = 0; int
select= 0; while (1) { showMenu();// Function menu display cout << " Select the function you want to use ( Enter the serial number ):" << endl;
cin>> select; if (select >=0 && select <7) { switch (select) { case 1:addPerson(
&abs); //1, Add a Contact break; case 2:showBooks(abs); //2, Show contacts break; case 3:
deletePerson(&abs); //3, Delete Contact break; case 4:findPerson(abs); //4, find contact break;
case 5:alterPerson(&abs); //5, Modify contact break; case 6:emptyBooks(&abs); //6, Clear contacts
break; case 0: { //0, Exit address book cout << " Welcome to use next time !" << endl; system("pause"); return 0;
} } } else { cout << " There is no such option ! Please re-enter !" << endl; system("pause"); system("cls"); }
} return 0; }
Technology