COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
phonebook_incomplete.cpp File Reference
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
Include dependency graph for phonebook_incomplete.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 253 of file phonebook_incomplete.cpp.

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

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 289 of file phonebook_incomplete.cpp.

290{
291 // Step 1:
292 // create a new dynamic array with a new size = max_size + n
293
294 // Step 2:
295 // copy all the records from the original array to the new dynamic array
296
297 // Step 3:
298 // destroy the old dynamic array to free up the memory allocated to it
299
300 // assign the pointer to the new dynamic array to the pointer variable
301
302 // Step 4:
303 // update the size of the array
304
305
306 cout << "--->phonebook size enlarged to hold a maximum of " << pb_size << " records." << endl;
307
308 return;
309}

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_incomplete.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 fin.close();
167 return i;
168}
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_incomplete.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 170 of file phonebook_incomplete.cpp.

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

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 233 of file phonebook_incomplete.cpp.

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

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 211 of file phonebook_incomplete.cpp.

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

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

References PhoneRec::name.

Referenced by main().

Here is the caller graph for this function:

◆ upper_case()

string upper_case ( string str)

Definition at line 277 of file phonebook_incomplete.cpp.

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

Referenced by decryption(), encryption(), and search_phonebook().

Here is the caller graph for this function: