Checking the state of a iostream operation
Hi, I've got this method to get an int from a string.
Code:
bool UtilityClass::___GetIntFromString( const std::string &valueString, int &result )
{
bool retVal = false;
if (!valueString.empty())
{
std::istringstream ss;
ss.str(valueString);
ss >> result;
bool isGood = ss.good();
std::ios_base::iostate state = ss.rdstate ( );
//if (ss.good())
if ( (ss.rdstate() & std::istringstream::failbit) == 0 )
retVal = true;
}
return retVal;
}
I've been having a rather peculiar problem with its error checking (the ones in bold). I tried inputting an alphanumerical (or even alphabetical) string input but the isGood boolean equals to true and the state value is 0. While when I tried inputting a numerical string value, the isGood boolean equals false and the state is 1. Why does this happen? Am I not doing this correctly? Any idea how to do the errorchecking from this particular code? Thanks in advance.