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
| #include <iostream>
#include <fstream>
#include <string>
#include <limits>
using namespace std;
template<typename T>
void saisies_secure(T &variable)
{
while(!(cin >> variable))
cout <<"Entrez incorrecte ,recommence" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
template<typename T,typename Predicat>
void saisies_secure(T &variable,Predicat predicat)
{
while(!(cin >> variable)||!predicat(variable))
{
cout <<"Saisie incorrecte.Recommence " <<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
struct Informations
{
string prenom;
string nom ;
string sexe;
int age;
};
Informations demandeInfo()
{
Informations infos{};
cout << "Quel es votre prenom ?"<< endl;
saisies_secure(infos.prenom,[](string prenom){ return prenom.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");});
cout <<"Quel est votre nom ?"<< endl;
saisies_secure(infos.nom);
cout << "Quel est votre sexe ?" << endl;
saisies_secure(infos.sexe);
cout <<"Quel est votre age ?" << endl;
saisies_secure(infos.age , [](int &age){return age >0 && age < 100 &&
age.find_first_not_of("123456789");});
return infos;
}
string enregistre(Informations const &infos)
{
string nom_fichier { infos.prenom + "." + infos.nom + ".csv" };
ofstream fichier { nom_fichier };
if(fichier.is_open())
{
fichier << infos.nom << ',' << infos.prenom << ',' << infos.sexe << ',' << infos.age;
}
else cout << "Unable to open file" << endl;
return nom_fichier;
}
void lecture( Informations const &infos )
{
string line;
ifstream nom_fichier(infos.prenom + "." + infos.nom + ".csv");
if(nom_fichier.is_open())
{
cout << "Le contenu de ton fichier est :" << endl;
while(getline(nom_fichier,line))
{
cout << line << endl;
}
}else cout << "Unable to open file " << endl;
}
int main()
{
auto infos = demandeInfo();
auto nom_fichier= enregistre(infos);
cout << "Le fichier "<< nom_fichier << " a été enregistré " << endl;
lecture(infos);
return 0;
} |