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
| #include <iostream>
#include <string>
#include <sstream>
#include <limits>
void vider_buffer();
void saisir_string(std::string &variable, const std::string &message){
while(true){
std::cout << message;
std::getline(std::cin, variable);
if(std::cin.bad() || std::cin.eof()){
std::cerr << "Erreur, saisie incorrecte." << std::endl;
if(std::cin.eof()){
break;
}
vider_buffer();
}
else if(std::cin.fail()){
std::cerr << "Erreur, saisie incorrecte." << std::endl;
vider_buffer();
}
break;
}
}
bool saisir_int(int &variable, const std::string &message){
std::string temp;
while(true){
std::cout << message;
std::cin >> temp;
if(std::cin.bad() || std::cin.eof()){
std::cerr<<"Une erreur interne est survenue." << std::endl;
if(std::cin.eof()){
return false;
}
vider_buffer();
continue;
}
else if(std::cin.fail()){
std::cerr << "Erreur, saisie incorrecte." << std::endl;
vider_buffer();
continue;
}
vider_buffer();
std::istringstream stream(temp);
stream >> variable;
if(stream.fail() || !stream.eof()){
std::cerr << "Erreur, saisie incorrecte." << std::endl;
}
else{
return true;
}
}
return false;
}
void vider_buffer(){
std::cin.clear();
std::cin.seekg(0, std::ios::end);
if(!std::cin.fail()){
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
}
else
std::cin.clear();
} |