-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.cpp
140 lines (127 loc) · 2.86 KB
/
contact.cpp
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
// including header files
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
//global variables
string fname,lname,phone_num;
// Functions
void addContact();
void searchContact();
void self_exit();
bool check_digits(string);
bool check_numbers(string);
int main()
{
short int choice;
system("cls");
system("color 0A");
cout << "\n\n\n\t\t\tContact Management.";
cout << "\n\n\t1. Add Contact\n\t2. Search Contact\n\t3. Exit\n\t> ";
cin >> choice;
switch(choice)
{
case 1:
addContact();
break;
case 2:
searchContact();
break;
case 3:
self_exit();
break;
default:
cout << "\n\n\tInvalid Input !";
cout << "\n\tPress Any Key To Continue..";
getch();
main();
}
return 0;
}
void self_exit()
{
system("cls");
cout << "\n\n\n\t\tThank You For Using !";
exit(1);
}
void addContact()
{
ofstream phone("number.txt", ios::app);
system("cls");
cout << "\n\n\tEnter First Name : ";
cin >> fname;
cout << "\n\tEnter Last Name : ";
cin >> lname;
cout << "\n\tEnter 10-Digit Phone Number : ";
cin >> phone_num;
if(check_digits(phone_num) == true)
{
if(check_numbers(phone_num) == true)
{
if(phone.is_open())
{
phone << fname << " " << lname << " " << phone_num << endl;
cout << "\n\tContact Saved Successfully !";
}
else
{
cout << "\n\tError Opening File !";
}
}
else
{
cout << "\n\tA Phone Number Must Contain Numbers Only !";
}
}
else
{
cout << "\n\tA Phone Number Must COntain 10 Digits.";
}
phone.close();
}
void searchContact()
{
bool found = false;
ifstream myfile("number.txt");
string keyword;
cout << "\n\tEnter Name To Search : ";
cin >> keyword;
while(myfile >> fname >> lname >> phone_num)
{
if(keyword == fname || keyword == lname)
{
system("cls");
cout << "\n\n\n\t\tContact details..";
cout << "\n\n\tFirst Name : " << fname;
cout << "\n\tLast Name : " << lname;
cout << "\n\tPhone Number : " << phone_num;
found = true;
break;
}
}
if(found == false)
cout << "\n\tNo Such Contact Found";
}
bool check_digits(string x)
{
if(x.length() == 10)
return true;
else
return false;
}
bool check_numbers(string x)
{
bool check = true;
for(int i=0; i < x.length(); i++)
{
if(!(int(x[i]) >= 48 && int(x[i]) <= 57))
{
check = false;
break;
}
}
if(check == true)
return true;
if(check == false)
return false;
}