1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include <string>
#include <iostream>
#include <vector>
#include <cassert>
#include <memory>
#include <limits>
struct contact{
contact(std::string const & n, std::string const & p, int num):
nom{n},prenom{p},numeroTelephone{num}{
}
contact(contact const &) = delete;
contact& operator = (contact const &) = delete;
virtual ~contact() = default;
std::string const nom;
std::string const prenom;
int numeroTelephone;
};
using contact_ptr = std::unique_ptr<contact>;
using repertoire = std::vector<contact_ptr>;
std::unique_ptr<contact> extraireContact(std::istream & in, bool affiche){ // affiche permet de savoir s'il faut afficher les informations
if(affiche)
std::cout<<"Veuillez introduire le nom du contact :";
std::string nom;
in>>nom;
if(affiche)
std::cout<<"Veuillez introduire le prenom du contact : ";
std::string prenom;
std::cin>> prenom;
if(affiche)
std::cout<<"veuillez introduire le numero de telephone du contact : ";
int num;
std::cin>>num;
return std::make_unique<contact>(nom, prenom, num);
}
void ajouterContact(repertoire & r, contact_ptr & c){
r.emplace_back(std::move(c));
}
void afficherMenu(){
std::cout << "\n============================== Menu ================================\n"
<< "\n"
<< "1==>----------------------Ajouter un contact------------------------\n"
<< "2==>----------------------Rechercher un contact---------------------\n"
<< "3==>----------------------Supprimer un contact----------------------\n"
<< "4==>----------------------Modifier un contact-----------------------\n"
<< "5==>----------------------Afficher un contact-----------------------\n"
<< "6==>----------------------Afficher tous les contacts----------------\n"
<< "7==>----------------------Quitter-----------------------------------\n"
<< "\n"
<< "====================================================================\n"
<< "\nTapez votre choix : ";
}
int choixAction(){
while(true){
std::string temp;
std::getline(std::cin, temp);
size_t pos;
try{
int choix = std::stoi(temp,&pos);
if(pos != temp.size()){
std::cout<<"veuillez introduire un chiffre uniquement\n";
}else if(choix <1 || choix >7){
std::cout<<"veuillez choisir un chiffre entre 1 et 7\n";
}else{
return choix;
}
}
catch(std::invalid_argument & e){
std::cout<<"veuillez introduire un chiffre uniquement\n";
}
afficherMenu();
}
return 0;
}
void afficherTousContacts(repertoire const & r){
for(auto const & i : r){
std::cout<<"nom :"<<i.get()->nom<<"\n";
std::cout<<"prenom :"<<i.get()->prenom<<"\n";
std::cout<<"telephone :"<<i.get()->numeroTelephone<<"\n";
}
}
int main(){
repertoire repert;
bool aboutToQuit{false};
while(!aboutToQuit){
afficherMenu();
int choix = choixAction();
switch(choix){
case 1:
{
auto c = extraireContact(std::cin, true);
ajouterContact(repert, c);
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
break;
case 2 : assert(false && "Not yet implemented");
break;
case 3: assert(false && "Not yet implemented");
break;
case 4: assert(false && "Not yet implemented");
break;
case 5: assert(false && "Not yet implemented");
break;
case 6: afficherTousContacts(repert);
break;
case 7: aboutToQuit = true;
}
}
} |
Partager