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

Discussion in 'C++' started by frohlinger@gmail.com, Sep 18, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. On Sep 18, 2:19 pm, 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
     
    Reetesh Mukul, Sep 18, 2007
    #2
    1. Advertising

  3. Guest

    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, 2007
    #3
  4. Jim Langston Guest

    <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Sep 18, 2007
    #4
  5. Pete Becker Guest

    On 2007-09-18 05:19:51 -0400, 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)
     
    Pete Becker, Sep 18, 2007
    #5
  6. Guest

    On Sep 18, 3:09 pm, Pete Becker <> wrote:
    > On 2007-09-18 05:19:51 -0400, 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.
     
    , Sep 18, 2007
    #6
  7. Pete Becker Guest

    On 2007-09-18 09:20:14 -0400, 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)
     
    Pete Becker, Sep 18, 2007
    #7
  8. Barry Guest

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



    --
    Thanks
    Barry
     
    Barry, Sep 18, 2007
    #8
  9. On Tue, 18 Sep 2007 06:20:14 -0700, 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
     
    Roland Pibinger, Sep 18, 2007
    #9
  10. James Kanze Guest

    On Sep 18, 11:19 am, 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:
    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
     
    James Kanze, Sep 19, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,355
    Ivan Vecerina
    Mar 17, 2005
  2. Replies:
    5
    Views:
    955
    X-Centric
    Jun 30, 2005
  3. Jeffrey Walton
    Replies:
    10
    Views:
    956
    Mathias Gaunard
    Nov 26, 2006
  4. darrel
    Replies:
    4
    Views:
    841
    darrel
    Jul 19, 2007
  5. Mitja Semolic

    howto: identify a mobile phone user

    Mitja Semolic, Aug 9, 2003, in forum: ASP .Net Mobile
    Replies:
    2
    Views:
    145
    Michal A. Valasek
    Aug 9, 2003
Loading...

Share This Page