[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How do I convert and validate numeric data to a string object?
From: |
Pep |
Subject: |
Re: How do I convert and validate numeric data to a string object? |
Date: |
Thu, 14 Feb 2008 03:57:53 -0800 (PST) |
User-agent: |
G2/1.0 |
Thomas Maeder wrote:
> Pep <pepaltavista@yahoo.co.uk> writes:
>
> > I have numeric input which I must convert to strings and validate
> > whilst doing the conversion. I do not want to mix C functions like
> > strtol in to my c++ code, so I'm trying to implement a C++ method
> > using examples found in google but none of them work :(
>
> std::strtol() is a Standard C++ function. Where's the problem?
>
>
lol, sorry I was thinking of the likes of atol there, I had tried
strtol but that did not work either, so I discounted it.
<snip>
Hmm, I was a bit scratchy with my cut & paste there :(
Here's the real working code, followed by actual test results. I have
removed the resetting of the istringstream as that is clearly a bug
which is documented and there is a work around for it.
I'm using g++ 3.4.6 on Debian GNU/Linux 4.0 r1 _Etch_
One point of note about the source is that you said I needed to
include the <istream> and <ostream> headers but they are not needed in
this code. The only header needed for the streams is the <sstream>
header, so that is possibly an incompatibility between our compilers?
#include <sstream>
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
long testLong;
std::string testString;
std::istringstream iss;
iss.exceptions(std::istringstream::eofbit |
std::istringstream::failbit | std::istringstream::badbit);
iss.str(argv[1]);
try
// supposedly this will throw exception if conversion is
non-numeric
{
iss >> testLong;
}
catch(std::istringstream::failure& e)
{
std::cout << "caught a exception geezer! [" << e.what() << "]"
<<
std::endl;
}
if (!iss)
// supposedly this is the way to check for invalid numeric input
{
std::cout << "conversion was illegal geezer!" << std::endl;
}
std::cout << "testLong [" << testLong << "]" << std::endl;
return(EXIT_SUCCESS);
}
test results
// test 12389
./test 12389
caught a exception geezer! [basic_ios::clear]
testLong [12389]
NOTE: I do not expect this to throw an exception
// test A12389
./test A12389
caught a exception geezer! [basic_ios::clear]
conversion was illegal geezer!
testLong [-1210077196]
NOTE: I expect this to throw an exception but how can I trust it as it
is the same exception that was thrown in "test 12389", where I did not
expect to throw an exception.
// test 123A89
./test 123A89
testLong [123]
NOTE: well this certainly does not work how I expect it to :(
//test 12389A
./test 12389A
testLong [12389]
NOTE: I am expecting to see both the exception error message and the
illegal conversion error message here.
In short I am expecting to see the exception and the illegal
conversion error messages for all but the first test "test 12389". So
what am I misunderstanding here?
It is looking like I will have to write a function that iterates the
string checking if each character is a digit before I use the
istringstream or strtol to convert the string to a long because they
do not seem to be able to validate the input themselves.
Thanks for your help thus far,
Pep.