By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,869 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,869 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Sep 18 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Sep 18, 2:19 pm, frohlin...@gmail.com wrote:
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



Sep 18 '07 #2

P: n/a
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.

Sep 18 '07 #3

P: n/a
<fr********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
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.


Sep 18 '07 #4

P: n/a
On 2007-09-18 05:19:51 -0400, fr********@gmail.com said:
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.

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

Sep 18 '07 #5

P: n/a
fr********@gmail.com wrote:
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::ostringstream 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

--
Thanks
Barry
Sep 18 '07 #6

P: n/a
On Sep 18, 11:19 am, frohlin...@gmail.com wrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.