Howto identify a string value vs. a numeric value in std::string

F

frohlinger

Hi,
I need to perform some numeric calculations on a numeric float value,
that is received as wstring.
I would like to perform a check before converting the wstring to
float, checking that indeed the wstring contains a numeric value.
This is the actual conversion:
double fValue = _wtof(strValue.c_str());
if strValue contains some characters (e.g.: 'aaa'), _wtof returns 0,
therefore I do not really know that the input was initially invalid.
Any ideas how to detect it?
Thanks, Gabi.
 
R

Reetesh Mukul

Hi,
I need to perform some numeric calculations on a numeric float value,
that is received as wstring.
I would like to perform a check before converting the wstring to
float, checking that indeed the wstring contains a numeric value.
This is the actual conversion:
double fValue = _wtof(strValue.c_str());
if strValue contains some characters (e.g.: 'aaa'), _wtof returns 0,
therefore I do not really know that the input was initially invalid.
Any ideas how to detect it?
Thanks, Gabi.

You can use boost::lexical_cast ...

double fValue;
try
{
fValue = boost::lexical_cast<double>(strValue.c_str());
}

catch(boost::bad_lexical_cast&)
{
///do_error_checks
}

You can look into this page:- http://www.boost.org/libs/conversion/lexical_cast.htm

Regards,
Mukul
 
F

frohlinger

Thanks, but I'm not sure I can use libraries, except MFC.
This is a dll, part of a bigger product at work, with restrictions.
Boost libraries are not Microsoft's, right?
Gabi.
 
J

Jim Langston

Thanks, but I'm not sure I can use libraries, except MFC.
This is a dll, part of a bigger product at work, with restrictions.
Boost libraries are not Microsoft's, right?

Well, you can use stringstream like this:

std::string MyString("123.45");
std::stringstream Foo;
Foo << MyString;
double MyDouble;
Foo >> MyDouble;
At this point you can see if Foo is in a good state.
if ( !Foo )
// There wasn't a double

However. This will only check the beginning of hte string. If hte string
was actually "123.45x" then MyDouble would contain 123.45 and x would be
left in the stream. If you want to see if there's any extra characters you
could try to grab them off.

std::string Rest;
Foo >> Rest;
Now, if Foo is in a good state, that means it pulls some more chars off and
there wre extra chars. Or if Rest.size() > 0.
 
P

Pete Becker

Hi,
I need to perform some numeric calculations on a numeric float value,
that is received as wstring.
I would like to perform a check before converting the wstring to
float, checking that indeed the wstring contains a numeric value.
This is the actual conversion:
double fValue = _wtof(strValue.c_str());

I don't know where you got wtof from, but the standard way of
converting would be to use wcstof, which gives you the information you
need.
 
F

frohlinger

I don't know where you got wtof from, but the standard way of
converting would be to use wcstof, which gives you the information you
need.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Hi Pete,
I could not find a single documentation on wcstof.
Are you sure this is the write method?
Gabi.
 
P

Pete Becker

I could not find a single documentation on wcstof.
Are you sure this is the write method?

The first six hits I got on Google include five man pages that cover wcstof.
 
B

Barry

Thanks, but I'm not sure I can use libraries, except MFC.
This is a dll, part of a bigger product at work, with restrictions.
Boost libraries are not Microsoft's, right?
Gabi.

#include <sstream>
#include <iostream>
#include <typeinfo> // bad_cast

template <class To, class From>
To LexicalCast(From const& from)
{
std::eek:stringstream oss;
oss << from;
if (!oss)
throw std::bad_cast("Lexical cast error");

std::istringstream iss(oss.str());
To to;
iss >> to;
if (!(iss && iss.get() == std::char_traits<char>::eof()))
throw std::bad_cast("Lexical cast error");

return to;
}

int main(int argc, char* argv[])
{
try {
int i = LexicalCast<int>(argc == 1 ? "1234" : argv[1]);
std::cout << i << std::endl;
}
catch (std::bad_cast const& ex) {
std::cerr<< ex.what() << std::endl;
}
}


if you don't like stringstream way

you can do some extra check (like using strspn), then do the casting
 
R

Roland Pibinger

I could not find a single documentation on wcstof.
Are you sure this is the write method?

Try wcstod which certainly is available for Visual C++. Beware, the
strto. and wcsto. functions are tricky to use!
 
J

James Kanze

I need to perform some numeric calculations on a numeric float value,
that is received as wstring.
I would like to perform a check before converting the wstring to
float, checking that indeed the wstring contains a numeric value.
This is the actual conversion:
double fValue = _wtof(strValue.c_str());
if strValue contains some characters (e.g.: 'aaa'), _wtof returns 0,
therefore I do not really know that the input was initially invalid.
Any ideas how to detect it?

Fallible< double >
convertToDouble(
std::wstring const& value )
{
std::wistringstream s( strValue ) ;
double d ;
return s >> d >> std::ws && s.get() == EOF
? Fallible< double >( d )
: Fallible< double >() ;
}

If the returned Fallible is valid, the string contained a legal
floating point value, possibly with leading and/or trailing
whitespace, but with nothing else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top