RosettaCodeData/Task/Determine-if-a-string-is-numeric/C++/determine-if-a-string-is-numeric-2.cpp

29 lines
564 B
C++
Raw Permalink Normal View History

2024-04-19 16:56:29 -07:00
#include <sstream> // for istringstream
using namespace std;
2023-07-01 11:58:00 -04:00
bool isNumeric( const char* pszInput, int nNumberBase )
{
2024-04-19 16:56:29 -07:00
istringstream iss( pszInput );
if ( nNumberBase == 10 )
{
double dTestSink;
iss >> dTestSink;
}
else if ( nNumberBase == 8 || nNumberBase == 16 )
{
int nTestSink;
iss >> ( ( nNumberBase == 8 ) ? oct : hex ) >> nTestSink;
}
else
return false;
// was any input successfully consumed/converted?
if ( ! iss )
return false;
2023-07-01 11:58:00 -04:00
2024-04-19 16:56:29 -07:00
// was all the input successfully consumed/converted?
return ( iss.rdbuf()->in_avail() == 0 );
2023-07-01 11:58:00 -04:00
}