How to make search program in phone book by number

// Phonebook project
//===============================

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;


//prototypes
void printline(char, int);
bool name_valid(string);
bool mob_valid(string);

class contact
{
string name;
string mob;

public:
//Initialize the contact by a default value
contact(): name(""), mob("")
{}

// Shows all contacts
bool show()
{
if(name != "")
{
cout << name << "\t" << mob << endl;
return 1; //Indicates success
}
else
return 0; //Indicates failure
}

//Search
bool show(string search_term)
{
if(search_term == name)
{
cout << name << "\t" << mob << endl;
return 1;
}
else
return 0;
}

//Checks whether the name exists or not
bool name_exists(string tname)
{
if(tname == name)
return 1;
else
return 0;
}

//The contact object is initialized by valid values
bool add(string new_name, string new_mob)
{
if(name=="")
{
name = new_name;
mob = new_mob;
return 1; // Success
}
else
return 0; // Failure

}

//Edits the contact details
bool edit(string);

//Sets the contact details to default values
//That is, the contact details are thus erased
bool erase(string new_name)
{
if(new_name==name)
{
name = "";
mob = "";
return 1;
}
else
return 0;
}
};


//Edits the contact
bool contact :: edit(string new_name)
{
string new_mob;
if(new_name==name)
{
cout << "Enter new name: "; cin >> new_name;
cout << "Enter new mobile no: "; cin >> new_mob;

name = new_name;
mob = new_mob;
return 1;
}
else
return 0;
}
int main()
{
contact person[100];

string temp_name, temp_mob;
int choice, i, counter;
bool flag;
bool cancel_flag;

cout << "**** PHONEBOOK ******" << endl;


do
{
cout << "\n\n\n";
printline('-', 20);
cout << "1. Memasukkan Data" << endl
<< "2. Menampilkan Data" << endl
<< "3. Mencari Data Berdasar Nama" << endl
<< "4. Mencari Data Berdasar No. Telfon" << endl
<< "5. Keluar" << endl
<< "Masukkan Pilihan Anda : ";
cin >> choice;

system("cls");
printline('-', 20);
cancel_flag = 0;
flag = 0;
counter = 0;

switch (choice)
{
case 1:
cout << "Add New Contact\t\t\t\tpress $ to cancel" << endl;
printline('-', 20);
counter = 0;

//Berulang hingga nama dan no benar
do
{
flag = 0;
if (counter)
cout << "Try again\t\t\t\tpress $ to cancel"
<< endl;

//counts how many times the do-while loop executes
counter++;

cout << "Name: "; cin >> temp_name;

//Cancel operation
if (temp_name == "$")
{
cancel_flag = 1;
break;
}
cout << "Mobile No.: "; cin >> temp_mob;

//Cancel operation
if (temp_mob == "$")
{
cancel_flag = 1;
break;
}

//Check whether name exists
for (i = 0; i<100; i++)
if (person[i].name_exists(temp_name))
{
cout << "Kontak sudah ada!"
"Masukkan nama lain."
<< endl;
flag = 1;
break;
}

} while (!name_valid(temp_name) ||
flag ||
!mob_valid(temp_mob));

if (cancel_flag)
{
system("cls");
break;
}


//Memasukkan kontak ke buku telfon
for (i = 0; i<100; i++)
if (person[i].add(temp_name, temp_mob))
{
cout << "Berhasil Menambah Kontak" << endl;
flag = 1;
break;
}

if (!flag)
cout << "Memory Kontak Penuh! Hapus Beberapa Kontak."
<< endl;
break;

case 2:
cout << "Menampilkan Kontak" << endl;
printline('-', 20);

for (i = 0; i<100; i++)
if (person[i].show())
flag = 1;

if (!flag)
cout << "Tidak ada kontak yang ditemukan!" << endl;
break;

case 3: //search contact by name
do
{
if (counter)
cout << "Coba lagi" << endl;
counter++;
cout << "Cari Nama : \t\t\t\tpress $ to cancel\n";
cin >> temp_name;

//Cancel Operation
if (temp_name == "$")
{
system("cls");
break;
}

for (i = 0; i<100; i++)
if (person[i].show(temp_name))
{
flag = 1;
break;
}

if (!flag)
cout << "Kontak tidak ditemukan" << endl;
} while (!flag);

break;

case 4: //search contact by number
do
{
if (counter)
cout << "Coba lagi" << endl;
counter++;
cout << "Cari Nomor : \t\t\t\tpress $ to cancel\n";
cin >> temp_mob;

//Cancel Operation
if (temp_mob == "$")
{
system("cls");
break;
}

for (i = 0; i<100; i++)
if (person[i].show(temp_name))
{
flag = 1;
break;
}

if (!flag)
cout << "Kontak tidak ditemukan" << endl;
} while (!flag);

break;

case 5:
return 0;
break;

}
} while (1);

_getch();
return 0;
}
//prints a line
void printline(char ch, int size)
{
for (int i = 0; i<size; i++)
cout << ch;
cout << "\n";
}


//Contact name validation
bool name_valid(string tname)
{

if (tname.size()>20)
{
cout << "Nama Salah!\nMasukkan Maks 20 Karakter dan Tanpa Spasi!"
<< endl;
return 0;
}
else if (tname == "")
{
cout << "Nama Salah!\nNama tidak boleh kosong!" << endl;
return 0;
}
else
return 1;
}

//mobile number validation
bool mob_valid(string tmob)
{
if (tmob.size()>13 || tmob.size()<10)
{
cout << "Nomor salah\n"
"Masukkan Nomor Antara 10 - 13 digit" << endl;
return 0;
}
else if (tmob == "")
{
cout << "No salah\n"
"Nomor tidak boleh kosong" << endl;
return 0;
}
else
return 1;
}


Hey guys, in case 3, it's for searching contact in phone book by name, and i have problem in case 4, it's for searching contact in phone book by number, can you help me plz, thankss
In case 3 you
1
2
3
4
5
6
for (i = 0; i<100; i++)
  if ( person[i].show(temp_name) )
  {
    flag = 1;
    break;
  }

Use bool contact::show(string) to check whether current person has temp_name.
The contact::show() is part of the interface of the class contact.

In case 4 you should use another interface function of the contact that relates to the mob number. If the contact offers no way to access the mob, then the interface of contact is insufficient and should be improved.

There are two possibilities:
1
2
3
4
5
// return true, if contact has mob that matches key
bool contact::hasnumber(string key) const

// return the mob. Let the caller to match it to key
string contact::number() const




EDIT: Can you tell what this does:
1
2
bool flag = (person+100) != std::find_if( person, person+100,
         [temp_name](contact& p){return p.show(temp_name);} );
Last edited on
Topic archived. No new replies allowed.