Siccome capito spesso (a me) di sbagliare a inserire dati manualmente durante un programma volevo creare un metodo che controllasse il type in input. Ho sfruttato l'idea che mi diede MItaly. Ho cmq un problemino in compilazione:
alla stringa 36 "is>>aux>>std::ws" non riconosce aux e me lo da come undeclared. Come mai? Nel flusso if-else viene sicuramente dichiarato aux. Indipendentemente da questo, se capite l'intento del metodo, cosa cambiereste o come lo impostereste, thx...
codice:
void UserInput(string os, int type) {
/*The method checks if the user inserts a wrong type variable
the input "type" is the type to be checked out
1)int, 2) float, 3) double, 4) char
*/
string s;
istringstream is;
bool check;
if(type==1){
int aux;
check = ((!is && !is.eof())||(((int)aux-aux)!=0));
}
else if(type==2){
float aux;
check = ((!is && !is.eof())||(((float)aux-aux)!=0));
}
else if(type==3){
double aux;
check = (!is && !is.eof());
}
else if(type==4){
char aux;
check = (!is && !is.eof());
}
else string aux;
do{
std::cout << os;
getline(std::cin, s);
is.clear();
is.str(s);
is >> aux >> std::ws;
}while(check);
}
pensandoci mi viene il dubbio che dichiarando dentro gli if-else lo scope sia ivi limitato....non saprei come fare un cast allora, indizi?