-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
221 lines (184 loc) · 4.52 KB
/
Copy pathdatabase.cpp
File metadata and controls
221 lines (184 loc) · 4.52 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<fstream>
using namespace std;
struct Person
{
string name;
string surname;
short age;
string telephone;
};
short peopleInDatabase;
Person people[50];
void requireEnter();
void addPerson();
void showPerson();
void savePeopleToFile();
void loadPeopleFromTheFile();
void searchDatabase();
void removePersonFromDatabase();
int main()
{
char test;
loadPeopleFromTheFile();
do{
cout<<"Number Of People in Database: "<<peopleInDatabase<<endl;
cout<<"MENU:"<<endl;
cout<<"1.Add Person"<<endl;
cout<<"2.Show all people"<<endl;
cout<<"3.Save People To File"<<endl;
cout<<"4.Load People From The File"<<endl;
cout<<"5.Search From Database"<<endl;
cout<<"6.Remove Person From Database"<<endl;
cout<<endl;
test=getch();
switch(test)
{
case '1':
addPerson();
break;
case '2':
showPerson();
break;
case '3':
savePeopleToFile();
break;
case '4':
loadPeopleFromTheFile();
break;
case '5':
searchDatabase();
break;
case '6':
removePersonFromDatabase();
}
requireEnter();
system("cls");
}while(test!=27);
return 0;
}
void requireEnter()
{
cout<<"Click Enter To Continue...."<<endl;
while(getch()!=13);
}
void addPerson()
{
cout<<"Type Name: "<<endl;
cin>>people[peopleInDatabase].name;
cout<<"Type Surname: "<<endl;
cin>>people[peopleInDatabase].surname;
cout<<"Type Age: "<<endl;
cin>>people[peopleInDatabase].age;
cout<<"Type Mobile Number: "<<endl;
cin>>people[peopleInDatabase].telephone;
peopleInDatabase++;
}
void showPerson()
{
if(peopleInDatabase>0)
{
for(int i=0;i<peopleInDatabase;i++)
{
cout<<"Person Index "<<i+1<<endl;
cout<<"Name: "<<people[i].name<<endl;
cout<<"Surname: "<<people[i].surname<<endl;
cout<<"Age: "<<people[i].age<<endl;
cout<<"Mobile Number: "<<people[i].telephone<<endl<<endl<<endl;
}
}
else
cout<<"Nobody is in the Database"<<endl;
}
void savePeopleToFile()
{
ofstream file("database.txt");
if(file.is_open())
{
file<<peopleInDatabase<<endl;
for(int i=0;i<peopleInDatabase;i++)
{
file<<people[i].name<<endl;
file<<people[i].surname<<endl;
file<<people[i].age<<endl;
file<<people[i].telephone<<endl;
}
file.close();
cout<<"Saved Successfully.."<<endl;
}
else
cout<<"Could Not Save To File"<<endl;
}
void loadPeopleFromTheFile()
{
ifstream file("database.txt");
if(file.is_open())
{
file>>peopleInDatabase;
if(peopleInDatabase>0)
{
for(int i=0;i<peopleInDatabase;i++)
{
file>>people[i].name;
file>>people[i].surname;
file>>people[i].age;
file>>people[i].telephone;
}
cout<<"All peoples were loaded fine."<<endl;
}
else
cout<<"Database is Empty"<<endl;
}
else
cout<<"This File Does Not Exist"<<endl;
}
void searchDatabase()
{
if(peopleInDatabase>0)
{
string name;
cout<<"Type a name of person you want to look for"<<endl;
cin>>name;
for(int i=0;i<peopleInDatabase;i++)
{
if(name==people[i].name)
{
cout<<"Person Index "<<i+1<<endl;
cout<<"Name: "<<people[i].name<<endl;
cout<<"Surname: "<<people[i].surname<<endl;
cout<<"Age: "<<people[i].age<<endl;
cout<<"Mobile Number: "<<people[i].telephone<<endl<<endl<<endl;
}
}
}
else
cout<<"Nothing in the database to search for"<<endl;
}
void removePersonFromDatabase()
{
if(peopleInDatabase>0)
{
short index;
cout<<"Who do you want to remove type index:"<<endl;
cin>>index;
if(peopleInDatabase>=index)
{
for(short k=index;k<peopleInDatabase;k++)
{
people[k-1].name=people[k].name;
people[k-1].surname=people[k].surname;
people[k-1].age=people[k].age;
people[k-1].telephone=people[k].telephone;
}
peopleInDatabase--;
cout<<"Removed Successfully...."<<endl;
savePeopleToFile();
}
else
cout<<"There is nobody like that"<<endl;
}
else
cout<<"There is nothing to remove"<<endl;
}