I copied this code from my projet and don't have to check whether input is number or not because *.dat file from which I read is created by the same program which writes ONLY numerical information out to the file.
If OP has problem with that then this is better one:
1 2 3 4 5 6 7
template <typename T>
inline T ToNumber(const string& text)
return ss >> result ? result : 0;
The same result and side behaviour is achived by using atoi function, but it is not template and works on integral values, and because OP asked for generics I gave him a template :)