Code:
std::istringstream iss(myString);
int n;
if(iss >> n && iss.eof()) {
// myString is a properly formated number with no trailing characters or whitespace
// leading whitespace is ignored
} else {
iss.clear(); // allows us to work with the string again.
iss.get(); // is the character that goofed things up.
iss.tellg(); // is the position in the string of the offending character.
}
If you want to allow trailing whitespace, but not characters or more numbers or a floating
point number you could use
Code:
if(iss >> n >> std::ws && iss.eof()) ...