cout << " ADD TO CONTACT \n-----------------------------\n Enter a contact information.\n\n";
cout << " Enter the name: "; cin >> name;
cout << " Enter the phone number: "; cin >> tell_number;
p.setData(name, tell_number); // setting inputted data
// storing the data in file
out_contacts.write((char*)&p, sizeof(Person));
out_contacts.close(); //closing the file
cout << "\n Successfully added!\n\n";
system("pause");
}
break;
// case 50 lists all Contacts
case50: {
system("cls");
ifstream in_contacts("contacts", ios::binary); // getting data from the file
cout << " ALL CONTACTS\n---------------------------\n";
cout << " Name Phone\n---------------------------\n";
while (in_contacts.read((char*) & p, sizeof(Person))) {
// displaying the data
p.displayData();
}
position = 1;
in_contacts.close(); // closing the file
cout << endl;
system("pause");
}
break;
// to back to main menu
case48: {
main();
}
break;
default: {
cout << " Your choice is not available in Menu.\n Please try one more time\n\n";
system("pause");
}
break;
} // switch
} // for loop
}
voidF_Second_Program() {
Person p; // global object for each cases
for (int k = 0; k < 1000; k++) {
system("cls");
cout << " C O N T A C T S\n" << "--------------------------------------\n" << " 1. Search contact by Phone number\n" << " 2. Search contact by Name\n" << " 3. Delete a contact\n" << " 4. Add a record to a specific position\n" << " 0. Back\n" << " Your choice: \n";
switch (_getch())
{
// case 49 for searching the contact by Phone Number
case49: {
system("cls");
cout << " SEARCH CONTACT BY PHONE NUMBER\n-------------------------------\n";
string search_by_tell_number;
bool isFound = 0;
ifstream in_contacts_list("contacts", ios::binary); // getting data from the file
cout << " List of all Contacts: \n";
cout << " Name Phone\n-------------------------------\n";
while (in_contacts_list.read((char*)&p, sizeof(Person))) {
// displaying the data
p.displayData();
}
position = 1;
in_contacts_list.close();
cout << "\n Enter the phone number: ";
cin >> search_by_tell_number;
ifstream in_contacts("contacts", ios::binary); // getting data from the file
//in_contacts.seekg(0, ios::end);
// cheaking the list for the specific data
cout << " Found Contact(s): \n";
while (in_contacts.read((char*)&p, sizeof(Person))) {
if (search_by_tell_number == p.getTellNumber()) {
isFound = 1;
p.displayData();
}
}
position = 1;
in_contacts.close(); // closing the file
cout << endl;
system("pause");
}
break;
// case 50 for searching the contact by Name
case50: {
system("cls");
cout << " SEARCH CONTACT BY NAME\n-----------------------------\n";
string search_by_name; // for searching the contact with his name
bool isFound = 0;
ifstream in_contacts_list("contacts", ios::binary); // getting data from the file
cout << " List of all Contacts: \n";
cout << " Name Phone\n-----------------------------\n";
while (in_contacts_list.read((char*)&p, sizeof(Person))) {
// displaying the data
p.displayData();
}
position = 1;
in_contacts_list.close();
cout << "\n Enter the Name: ";
cin >> search_by_name;
ifstream in_contacts("contacts", ios::binary); // getting data from the file
//in_contacts.seekg(0, ios::end);
// cheaking the list for the specific data
cout << " Found Contact(s): \n";
while (in_contacts.read((char*)&p, sizeof(Person))) {
if (search_by_name == p.getName()) {
isFound = 1;
p.displayData();
}
}
position = 1;
in_contacts.close(); // closing the file
cout << endl;
system("pause");
}
break;
// case 51 for deleting a contact
case51: {
system("cls");
cout << " DELETE A CONTACT\n---------------------\n";
string tell_number_for_deletion;
ifstream in_contacts_list("contacts", ios::binary); // getting data from the file
cout << " List of all Contacts: \n";
cout << " Name Phone\n---------------------\n";
while (in_contacts_list.read((char*)&p, sizeof(Person))) {
// displaying the data
p.displayData();
}
position = 1;
in_contacts_list.close();
cout << endl;
ofstream out_temp("temp", ios::binary);
ifstream in_contacts("contacts", ios::binary);
cout << " Enter phone number of contact to be deleted: ";
cin >> tell_number_for_deletion;
while (in_contacts.read((char*)&p, sizeof(Person))) {
if (p.getTellNumber() != tell_number_for_deletion) {