Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Howto identify a string value vs. a numeric value in std::string

Reply
Thread Tools

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

 
 
frohlinger@gmail.com
Guest
Posts: n/a
 
      09-18-2007
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.

 
Reply With Quote
 
 
 
 
Reetesh Mukul
Guest
Posts: n/a
 
      09-18-2007
On Sep 18, 2:19 pm, (E-Mail Removed) 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







 
Reply With Quote
 
 
 
 
frohlinger@gmail.com
Guest
Posts: n/a
 
      09-18-2007
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.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-18-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.




 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-18-2007
On 2007-09-18 05:19:51 -0400, http://www.velocityreviews.com/forums/(E-Mail Removed) 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)

 
Reply With Quote
 
frohlinger@gmail.com
Guest
Posts: n/a
 
      09-18-2007
On Sep 18, 3:09 pm, Pete Becker <(E-Mail Removed)> wrote:
> On 2007-09-18 05:19:51 -0400, (E-Mail Removed) 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)


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

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-18-2007
On 2007-09-18 09:20:14 -0400, (E-Mail Removed) said:

> 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.

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

 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      09-18-2007
(E-Mail Removed) 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: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



--
Thanks
Barry
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      09-18-2007
On Tue, 18 Sep 2007 06:20:14 -0700, (E-Mail Removed) wrote:
>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!


--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-19-2007
On Sep 18, 11:19 am, (E-Mail Removed) 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int to numeric numeric(18,2) ? jobs ASP .Net 2 07-22-2007 12:32 AM
Arithmetic overflow error converting numeric to data type numeric. darrel ASP .Net 4 07-19-2007 09:57 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM
[HowTo] Enable Numeric property of the textbox in OpenWave UP.Browser 6.x Daisaku ASP .Net Mobile 0 11-20-2003 11:42 AM
howto: identify a mobile phone user Mitja Semolic ASP .Net Mobile 2 08-09-2003 02:52 PM



Advertisments