Visualizzazione dei risultati da 1 a 10 su 10

Discussione: [C++] User input check

Visualizzazione discussione

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    41

    [C++] User input check

    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?
    Ultima modifica di Toxotes; 19-11-2013 a 10:46

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.