Skip to content

Instantly share code, notes, and snippets.

@Marie-zaj
Created September 25, 2025 18:18
Show Gist options
  • Select an option

  • Save Marie-zaj/90347cba2766f27bd664632c01d9c0fe to your computer and use it in GitHub Desktop.

Select an option

Save Marie-zaj/90347cba2766f27bd664632c01d9c0fe to your computer and use it in GitHub Desktop.
TelephoneDirectory
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class Abonent
{
char* name;
char homePhone[20];
char workPhone[20];
char mobilePhone[20];
char info[100];
public:
Abonent()
{
name = nullptr;
homePhone[0] = workPhone[0] = mobilePhone[0] = info[0] = '\0';
}
Abonent(const char* Name, const char* home, const char* work,
const char* mobile, const char* Info)
{
name = new char[strlen(Name) + 1];
strcpy(name, Name);
strcpy(homePhone, home);
strcpy(workPhone, work);
strcpy(mobilePhone, mobile);
strcpy(info, Info);
}
Abonent(const Abonent& other)
{
if (other.name)
{
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
}
else
{
name = nullptr;
}
strcpy(homePhone, other.homePhone);
strcpy(workPhone, other.workPhone);
strcpy(mobilePhone, other.mobilePhone);
strcpy(info, other.info);
}
Abonent& operator=(const Abonent& other)
{
if (this == &other) return *this;
delete[] name;
if (other.name)
{
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
}
else
{
name = nullptr;
}
strcpy(homePhone, other.homePhone);
strcpy(workPhone, other.workPhone);
strcpy(mobilePhone, other.mobilePhone);
strcpy(info, other.info);
return *this;
}
void Input()
{
char buffer[100];
cout << "Enter name: ";
cin.getline(buffer, 100);
name = new char[strlen(buffer) + 1];
strcpy(name, buffer);
cout << "Enter home phone: ";
cin.getline(homePhone, 20);
cout << "Enter work phone: ";
cin.getline(workPhone, 20);
cout << "Enter mbile phone: ";
cin.getline(mobilePhone, 20);
cout << "Enter info: ";
cin.getline(info, 100);
}
void Output()
{
cout << "Name: " << name << endl
<< "Home: " << homePhone << endl
<< "Work: " << workPhone << endl
<< "Mobile: " << mobilePhone << endl
<< "Info: " << info << endl;
cout << "-----------------------------\n";
}
bool Search(const char* searchName)
{
if (name == nullptr) return false;
int i = 0;
while (name[i] != '\0' && searchName[i] != '\0')
{
if (name[i] != searchName[i]) return false;
i++;
}
return (name[i] == '\0' && searchName[i] == '\0');
}
~Abonent()
{
delete[] name;
}
};
int main() {
const int MAX = 100;
Abonent* book[MAX];
int count = 0;
int choice;
do {
cout << "\n Phonebook \n";
cout << "1. Add abonent\n";
cout << "2. Show all\n";
cout << "3. Find abonent\n";
cout << "4. Delete abonent\n";
cout << "0. Exit\n";
cout << "Choice: ";
cin >> choice;
cin.ignore();
if (choice == 1)
{
book[count] = new Abonent;
book[count]->Input();
count++;
}
else if (choice == 2)
{
for (int i = 0; i < count; i++)
{
book[i]->Output();
}
}
else if (choice == 3)
{
char searchName[100];
cout << "Enter name to find: ";
cin.getline(searchName, 100);
bool found = false;
for (int i = 0; i < count; i++)
{
if (book[i]->Search(searchName))
{
book[i]->Output();
found = true;
}
}
if (!found) cout << "Not found.\n";
}
else if (choice == 4)
{
char delName[100];
cout << "Enter name to delete: ";
cin.getline(delName, 100);
bool deleted = false;
for (int i = 0; i < count; i++)
{
if (book[i]->Search(delName))
{
delete book[i];
for (int j = i; j < count - 1; j++)
{
book[j] = book[j + 1];
}
count--;
deleted = true;
cout << "Deleted.\n";
break;
}
}
if (!deleted) cout << "Not found.\n";
}
} while (choice != 0);
for (int i = 0; i < count; i++)
{
delete book[i];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment