Search code examples
c++error-handlingintegererror-checking

Checking for legitimate integer input in c++


so i've seen many people ask this and not many solid answers floating around the web. most just check that an integer was placed in place of a string but if a floating point number was entered then it truncates the bottom half or if integers and characters are intered it truncates the characters. i need help writing a piece of code that checks for user input and asks the user to retry if his input is not valid or a combination of valid/invalid. i think the basic idea was to make a string so it accepts anything then use sstream to manipulate and then back to int if the input was legit but i cant really manage to check the other parts. if anyones run accross this or can help me out please link me to it. i'll post my code when i get a good sense of what to do.


Solution

  • Assuming that you can't use boost::lexical_cast, you can write your own version:

    #include <sstream>
    #include <iostream>
    #include <stdexcept>
    #include <cstdlib>
    template <class T1, class T2>
    T1 lexical_cast(const T2& t2)
    {
      std::stringstream s;
      s << t2;
      T1 t1;
      if(s >> std::noskipws >> t1 && s.eof()) {
        // it worked, return result
        return t1;
      } else {
        // It failed, do something else:
        // maybe throw an exception:
        throw std::runtime_error("bad conversion");
        // maybe return zero:
        return T1();
        // maybe do something drastic:
        exit(1);
      }
    }
    
    
    
    int main() {
      std::string smin, smax;
      int imin, imax;
    
      while(std::cout << "Enter min and max: " && std::cin >> smin >> smax) {
        try {
          imin = lexical_cast<int>(smin);
          imax = lexical_cast<int>(smax);
          break;
        } catch(std::runtime_error&) {
          std::cout << "Try again: ";
          continue;
        }
      }
    
      if(std::cin) {
        std::cout << "Thanks!\n";
      } else {
        std::cout << "Sorry. Goodbye\n";
        exit(1);
      }
    }