COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
phonebook.cpp File Reference
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
Include dependency graph for phonebook.cpp:

Go to the source code of this file.

Classes

struct  PhoneRec
 

Functions

int add_record (PhoneRec pb[], int nRec)
 
void grow_phonebook (PhoneRec *&pb, int &pb_size, int n)
 
int load_phonebook (string filename, PhoneRec *&pb, int &pb_size)
 
int main ()
 
void print_phonebook (PhoneRec pb[], int nRec)
 
int save_phonebook (string filename, PhoneRec pb[], int nRec)
 
int search_phonebook (string str, PhoneRec pb[], int nRec)
 
char selection_menu ()
 
void sort_phonebook (PhoneRec pb[], int nRec)
 
string upper_case (string str)
 

Function Documentation

◆ add_record()

int add_record ( PhoneRec pb[],
int nRec )

Definition at line 254 of file phonebook.cpp.

255{
256 char ans;
257 string str;
258
259 getline(cin, str); // flush the keyboard buffer
260 cout << "Please enter a name: ";
261 getline(cin, pb[nRec].name);
262 cout << "Please enter a phone number: ";
263 getline(cin, pb[nRec].phone_no);
264 cout << endl;
265 cout << "Name:\t" << pb[nRec].name << endl;
266 cout << "Phone#:\t" << pb[nRec].phone_no << endl << endl;
267 cout << "Add to phonebook (y/n)? ";
268 cin >> ans;
269 if (ans == 'y')
270 {
271 cout << "1 record added." << endl;
272 nRec++;
273 }
274
275 return nRec;
276}

References PhoneRec::name, and PhoneRec::phone_no.

Referenced by main().

Here is the caller graph for this function:

◆ grow_phonebook()

void grow_phonebook ( PhoneRec *& pb,
int & pb_size,
int n )

Definition at line 290 of file phonebook.cpp.

291{
292 // create a new dynamic array with a new size = max_size + n
293 PhoneRec * pb_new = new PhoneRec[pb_size + n];
294
295 // copy all the records from the original array to the new dynamic array
296 for (int i = 0; i < pb_size; i++)
297 {
298 pb_new[i] = pb[i];
299 }
300
301 // destroy the old dynamic array to free up the memory allocated to it
302 delete [] pb;
303 // assign the pointer to the new dynamic array to the pointer variable
304 pb = pb_new;
305 // update the size of the array
306 pb_size += n;
307
308 cout << "---> phonebook size enlarged to hold a maximum of " << pb_size << " records." << endl;
309
310 return;
311}

Referenced by load_phonebook(), and main().

Here is the caller graph for this function:

◆ load_phonebook()

int load_phonebook ( string filename,
PhoneRec *& pb,
int & pb_size )

Definition at line 138 of file phonebook.cpp.

139{
140 ifstream fin;
141 fin.open(filename.c_str());
142 if (fin.fail())
143 {
144 cout << "Error in file opening." << endl;
145 return 0;
146 }
147
148 int i = 0;
149 string line;
150 while (getline(fin, line))
151 {
152 if (i >= pb_size)
153 grow_phonebook(pb, pb_size, 3);
154
155 // extract a name and a phone no. from a line
156 if (i < pb_size) {
157 istringstream iss(line);
158 if (!getline(iss, pb[i].name,','))
159 break;
160 if (!getline(iss, pb[i].phone_no))
161 break;
162 ++i;
163 }
164
165 }
166
167 fin.close();
168 return i;
169}
void grow_phonebook(PhoneRec *&pb, int &pb_size, int n)

References grow_phonebook().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 47 of file phonebook.cpp.

48{
49 int phonebook_size = 3;
50 PhoneRec * phonebook = new PhoneRec[phonebook_size];
51
52 int num_records = 0, count;
53 string str;
54
55 char choice = selection_menu();
56 while (choice != '0')
57 {
58 switch (choice)
59 {
60 case '1':
61 cout << "Please enter the filename: ";
62 cin >> str;
63 num_records = load_phonebook(str, phonebook, phonebook_size);
64 cout << endl << num_records << " record(s) loaded." << endl << endl;
65 break;
66
67 case '2':
68 print_phonebook(phonebook, num_records);
69 break;
70
71 case '3':
72 sort_phonebook(phonebook, num_records);
73 print_phonebook(phonebook, num_records);
74 break;
75
76 case '4':
77 cout << "Please enter a name: ";
78 cin >> str;
79 cout << endl;
80 count = search_phonebook(str, phonebook, num_records);
81 cout << count << " record(s) found." << endl << endl;
82 break;
83
84 case '5':
85 cout << "Please enter the filename: ";
86 cin >> str;
87 count = save_phonebook(str, phonebook, num_records);
88 cout << endl << count << " record(s) saved." << endl << endl;
89 break;
90
91 case '6':
92 if (num_records >= phonebook_size)
93 grow_phonebook(phonebook, phonebook_size, 3);
94
95 if (num_records < phonebook_size)
96 num_records = add_record(phonebook, num_records);
97 cout << "There are now " << num_records <<
98 " record(s) in the phonebook." << endl << endl;
99 break;
100
101 default:
102 cout << "Invalid input!" << endl;
103 }
104 choice = selection_menu();
105 }
106
107 cout << "Goodbye!" << endl << endl;
108
109 delete [] phonebook;
110
111 return 0;
112}
void sort_phonebook(PhoneRec pb[], int nRec)
int add_record(PhoneRec pb[], int nRec)
int search_phonebook(string str, PhoneRec pb[], int nRec)
int load_phonebook(string filename, PhoneRec *&pb, int &pb_size)
void print_phonebook(PhoneRec pb[], int nRec)
int save_phonebook(string filename, PhoneRec pb[], int nRec)
char selection_menu()

References add_record(), grow_phonebook(), load_phonebook(), print_phonebook(), save_phonebook(), search_phonebook(), selection_menu(), and sort_phonebook().

Here is the call graph for this function:

◆ print_phonebook()

void print_phonebook ( PhoneRec pb[],
int nRec )

Definition at line 171 of file phonebook.cpp.

172{
173 int i;
174
175 for (i = 0; i < nRec; i++)
176 {
177 cout << "Name:\t" << pb[i].name << endl;
178 cout << "Phone#:\t" << pb[i].phone_no << endl << endl;
179 }
180}

References PhoneRec::name, and PhoneRec::phone_no.

Referenced by main().

Here is the caller graph for this function:

◆ save_phonebook()

int save_phonebook ( string filename,
PhoneRec pb[],
int nRec )

Definition at line 234 of file phonebook.cpp.

235{
236 ofstream fout;
237 fout.open(filename.c_str());
238 if (fout.fail())
239 {
240 cout << "Error in file opening." << endl;
241 return 0;
242 }
243
244 int i;
245 for (i = 0; i < nRec; i++)
246 {
247 fout << pb[i].name << "," << pb[i].phone_no << endl;
248 }
249
250 fout.close();
251 return i;
252}

References PhoneRec::name, and PhoneRec::phone_no.

Referenced by main().

Here is the caller graph for this function:

◆ search_phonebook()

int search_phonebook ( string str,
PhoneRec pb[],
int nRec )

Definition at line 212 of file phonebook.cpp.

213{
214 int i, count = 0;
215 for (i = 0; i < nRec; i++)
216 {
217 // extract the name from a record
218 string name = pb[i].name;
219 // search the name for any occurrence of str
220 // both the name and str are converted into upper case to
221 // make the search case insensitive
222 if (upper_case(name).find(upper_case(str)) != string::npos)
223 {
224 // output the record to the screen
225 cout << "Name:\t" << pb[i].name << endl;
226 cout << "Phone#:\t" << pb[i].phone_no << endl << endl;
227 // increase the count
228 count++;
229 }
230 }
231 return count;
232}
Node * find(Node *head, int num)
Definition ex4ex5.cpp:62
string upper_case(string str)

References find(), PhoneRec::name, PhoneRec::phone_no, and upper_case().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ selection_menu()

char selection_menu ( )

Definition at line 114 of file phonebook.cpp.

115{
116 char choice;
117
118 // print selection menu
119 cout << "********************************" << endl;
120 cout << "* Welcome to Phonebook Manager *" << endl;
121 cout << "********************************" << endl;
122 cout << "1. Load a phonebook." << endl;
123 cout << "2. Print all records." << endl;
124 cout << "3. Sort the records by ascending order of the name." << endl;
125 cout << "4. Search the records by partial match of the name." << endl;
126 cout << "5. Save the phonebook." << endl;
127 cout << "6. Add a new record." << endl;
128 cout << "0. Quit. " << endl;
129 cout << "Please enter your choice: ";
130
131 // read user selection
132 cin >> choice;
133 cout << endl;
134
135 return choice;
136}

Referenced by main().

Here is the caller graph for this function:

◆ sort_phonebook()

void sort_phonebook ( PhoneRec pb[],
int nRec )

Definition at line 182 of file phonebook.cpp.

183{
184 int i, j ,idx;
185 string min;
186 // selection sort
187 for (i = 0; i < nRec - 1; i++)
188 {
189 min = pb[i].name;
190 idx = i;
191
192 for (j = i + 1; j < nRec; j++)
193 {
194 if (pb[j].name < min)
195 {
196 min = pb[j].name;
197 idx = j;
198 }
199 }
200
201 if (idx != i)
202 {
203 PhoneRec temp;
204 // swap pb[i] & pb[idx]
205 temp = pb[i];
206 pb[i] = pb[idx];
207 pb[idx] = temp;
208 }
209 }
210}

References PhoneRec::name.

Referenced by main().

Here is the caller graph for this function:

◆ upper_case()

string upper_case ( string str)

Definition at line 278 of file phonebook.cpp.

279{
280 int i, n = str.length();
281 for (i = 0; i < n; i++)
282 {
283 if (str[i] >= 'a' && str[i] <= 'z')
284 str[i] = str[i] - 'a' + 'A';
285 }
286
287 return str;
288}

Referenced by search_phonebook().

Here is the caller graph for this function: