- Ajout de cotnacts
- Recherche de contacts
- Modification des informations
- Suppression de contacts
- Statistiques sur les contacts
Enterface utilisateur
char Menu::afficher_menu()
{
system("cls");
cout << "\n\t\t***************************************"
<< "\n\t\t* SYSTEME DE GESTION DE CONTACTS *"
<< "\n\t\t* *"
<< "\n\t\t* 1. Ajouter un contact *"
<< "\n\t\t* 2. Rechercher un contact *"
<< "\n\t\t* 3. Supprimer un contact *"
<< "\n\t\t* 4. Modifier un contact *"
<< "\n\t\t* 5. Afficher les statistiques *"
<< "\n\t\t* 6. Quitter *"
<< "\n\t\t***************************************"
<< "\n\t\tVotre choix : ";
char choix;
cin >> choix;
return choix;
}
Ajout d'un contact
void GestionContacts::ajouter_contact()
{
cout << "\nEntrez les informations du nouveau contact :\n";
cout << "Nom : ";
cin >> nom;
cout << "Telephone : ";
cin >> telephone;
cout << "Email : ";
cin >> email;
cout << "Adresse : ";
cin >> adresse;
sauvegarder();
cout << "\nContact ajouté avec succès.\n";
system("pause");
}
Recherche de contacts
void GestionContacts::rechercher()
{
ifstream fichier("contacts.txt", ios::in);
if (!fichier)
{
cout << "\nErreur : fichier introuvable\n";
system("pause");
return;
}
string critere;
cout << "\nEntrez un nom ou numéro de téléphone : ";
cin >> critere;
bool trouve = false;
string ligne;
while (fichier >> nom >> telephone)
{
getline(fichier, ligne);
if (nom == critere || telephone == critere)
{
cout << "\nContact trouvé :\n";
cout << "Nom : " << nom << "\nTelephone : " << telephone << ligne << endl;
trouve = true;
system("pause");
break;
}
}
if (!trouve)
cout << "\nAucun contact correspondant trouvé.\n";
}
Suppression d'un contact
void GestionContacts::supprimer_contact()
{
ofstream temp("temp.txt", ios::out);
ifstream source("contacts.txt", ios::in);
if (!temp || !source)
{
cout << "\nErreur d'accès aux fichiers\n";
system("pause");
return;
}
string critere;
cout << "\nEntrez le nom ou numéro à supprimer : ";
cin >> critere;
bool trouve = false;
string ligne;
while (source >> nom >> telephone)
{
getline(source, ligne);
if (critere == nom || critere == telephone)
{
cout << "\nContact à supprimer :\n";
cout << "Nom : " << nom << "\nTelephone : " << telephone << ligne << endl;
trouve = true;
}
else
{
temp << left << setw(20) << nom << " " << telephone << ligne << endl;
}
}
if (trouve)
{
while (getline(source, ligne))
temp << ligne << endl;
temp.close();
source.close();
remove("contacts.txt");
rename("temp.txt", "contacts.txt");
cout << "\nContact supprimé avec succès.\n";
}
else
{
cout << "\nAucun contact correspondant trouvé.\n";
}
system("pause");
}