As far as i know, the common way in c++ to cast a String into a int is to use sstream:
std::string inputString = "12 34";
std::istringstream istr(inputString);
int i1, i2;
istr >> i1 >> i2;
But if I want to make sure, that my code works for any input, the problem is to decide between the input of a string or 0:
std::string inputString = "TEXT 0";
std::istringstream istr(inputString);
int i1, i2;
istr >> i1 >> i2;
cout << i1 <<" != "<< i2 << endl;
I want to decide, if the user has inputed a String or a zero, in order to perform further manipulation.
Is there a clean way to decide this problem, without using lexical cast or atoi?
best gegards
You could use an invalid default value:
Note: The technique works if
numeric_limits<int>::min()cannot be a valid input