-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.cpp
More file actions
155 lines (138 loc) · 4.58 KB
/
contact.cpp
File metadata and controls
155 lines (138 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Use C++ to realize an address book management system
The following functions need to be implemented in the system:
* Add a contact: Add a new person to the address book. The information includes name, gender, age, phone number, and home address. A maximum of 1000 people are recorded
* Display contacts: Displays information about all contacts in the address book
* Delete contact: Deletes a contact by name
* Contact Search: Displays information about a specified contact by name
* Modify Contact: Modify the specified contact by name
* Delete contacts: Deletes all information in the address book
* Exit address book: Exit the current address book
*/
#include<iostream>
using namespace std;
struct contact
{
string name;
string sex;
int age;
string phone;
string address;
};
int main(){
contact arr[1000];
int n=0;
int length=sizeof(arr)/sizeof(arr[0]);
string nameinput;
int i=0;
while(true){
cout<<"Welcome to your address book"<<endl;
cout<<"*****************************"<<endl;
cout<<"1.Add a contact"<<endl;
cout<<"2.Display contacts"<<endl;
cout<<"3.Delete contact"<<endl;
cout<<"4.Contact Search"<<endl;
cout<<"5.Modify Contact"<<endl;
cout<<"6.Delete contacts"<<endl;
cout<<"7.Exit address book"<<endl;
cout<<"*****************************"<<endl;
cout<<"Please choose the above functions"<<endl;
int option=0;
cin>>option;
switch (option)
{
case 1:
if(n<1000){
string name;
cout<<"Please add name"<<endl;
cin>>name;
arr[n].name=name;
cout<<"Please add sex"<<endl;
cin>>arr[n].sex;
cout<<"Please add age"<<endl;
cin>>arr[n].age;
cout<<"Please add number"<<endl;
cin>>arr[n].phone;
cout<<"Please add address"<<endl;
cin>>arr[n].address;
}
n++;
break;
case 2:
if(n==0){
cout<<"please enter contact first"<<endl;
}else{
for(int i=0;i<n;i++){
cout<<"contact name: "<<arr[i].name<<
", contact sex: "<<arr[i].sex<<
", contact age: "<<arr[i].age<<
", contact phone: "<<arr[i].phone<<
", contact address: "<<arr[i].address<<endl;
}
}
break;
case 3:
if(n==0){
cout<<"please enter contact first"<<endl;
}else{
cout<<"please enter one name"<<endl;
cin>>nameinput;
for(;i<n;i++){
if(nameinput.compare(arr[i].name)==0){
for(int j=i;j<n-1;j++){
arr[j]=arr[j+1];
}
n--;
}else{
cout<<"this contact doesn't exist."<<endl;
}
}
}
break;
case 4:
if(n==0){
cout<<"please enter contact first"<<endl;
}else{
cout<<"please enter one name"<<endl;
cin>>nameinput;
for(;i<n;i++){
if(nameinput.compare(arr[i].name)==0){
cout<<"contact name: "<<arr[i].name<<
"contact sex"<<arr[i].sex<<endl;
}else{
cout<<"this contact doesn't exist."<<endl;
}
}
}
break;
case 5:
if(n==0){
cout<<"please enter contact first"<<endl;
}else{
cout<<"please enter one name"<<endl;
cin>>nameinput;
for(;i<n;i++){
if(nameinput.compare(arr[i].name)==0){
cout<<"please enter name again"<<endl;
cin>>arr[i].name;
cout<<"please enter age again"<<endl;
cin>>arr[i].age;
}else{
cout<<"this contact doesn't exsit"<<endl;
}
}
}
break;
case 6:
n=0;
cout<<"all information deleted"<<endl;
break;
case 7:
cout<<"system exits..."<<endl;
return 0;
break;
default:
break;
}
}
}