How to check std::string if its indeed an integer?

c++, integer, string, type-conversion, typechecking

Solution

WhozCraig's approach is much nicer and I wanted to expand on it using the approach that the C++ FAQ uses which is as follows:

#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>

class BadConversion : public std::runtime_error {
public:
  BadConversion(std::string const& s)
    : std::runtime_error(s)
    { }
};



inline int convertToInt(std::string const& s,
                              bool failIfLeftoverChars = true)
{
  std::istringstream i(s);
  int x;
  char c;
  if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
    throw BadConversion("convertToInt(\"" + s + "\")");
  return x;
}


int main()
{
    std::cout << convertToInt( "100" ) << std::endl ;
    std::cout << convertToInt( "-100" ) << std::endl ;
    std::cout << convertToInt( "  -100" ) << std::endl ;
    std::cout << convertToInt( "  -100  ", false ) << std::endl ;

    // The next two will fail
    std::cout << convertToInt( "  -100  ", true ) << std::endl ;
    std::cout << convertToInt( "H" ) << std::endl ;
}

This is robust and will know if the conversion fails, you also can optionally choose to fail on left over characters.

Problem

The following code converts an `std::string` to `int` and the problem lies with the fact that it cannot discern from a true integer or just a random string. Is there a systematic method for dealing with such a problem? ``` #include <cstring> #include <iostream> #include <sstream> int main() { std::string str = "H"; int int_value; std::istringstream ss(str); ss >> int_value; std::cout<<int_value<<std::endl; return 0; } ``` EDIT: This is the solution that I liked because it is very minimal and elegant! It doesn't work for negative numbers but I only needed positive ones anyways. ``` #include <cstring> #include <iostream> #include <sstream> int main() { std::string str = "2147483647"; int int_value; std::istringstream ss(str); if (ss >> int_value) std::cout << "Hooray!" << std::endl; std::cout<<int_value<<std::endl; str = "-2147483648"; std::istringstream negative_ss(str); if (ss >> int_value) std::cout << "Hooray!" << std::endl; std::cout<<int_value<<std::endl; return 0; } ```

Original source