Re: Converting a substring to Integer
sparkydarky <markdueck.bz@gmail.com> wrote:
I thought I knew C++ a little, but I realized that I don't because I
can barely follow what you guys posted. I tried to put some of the
code into Borland C++ Builder, but seems like Borland does not follow
the standards very well. Thanks for all your help. This will give me
really good base to work on and see if I can get it to work.
How about if I add all of the assumed bits? Copy the code below exactly
as written and paste it in. See if it compiles.
=== begin code ===
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
int extractNumber( const std::string& str )
{
using namespace std;
int result = 0;
stringstream ss( str );
while ( !( ss >> result ) ) {
if ( !ss.eof() ) {
ss.clear();
ss.ignore( numeric_limits<streamsize>::max(), ' ' );
}
else {
throw runtime_error( "no number" );
}
}
return result;
}
=== end code ===