Try to read this article: http://www.cplusplus.com/forum/articles/9645/
There I described the way I use and at the bottom of the 1st post are links to articles describing other ways
#include <boost/lexical_cast.hpp>
try {
int x = boost::lexical_cast<int>( "123" );
} catch( boost::bad_lexical_cast const& ) {
std::cout << "Error: input string was not valid" << std::endl;
}
Another C-like solution is strtol(), which is along the lines of atoi() except strtol() can
report conversion errors whereas in practice atoi() cannot.