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

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

  1. frohlinger

    frohlinger Guest

    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.
    frohlinger, Sep 18, 2007
    1. Advertisements

  2. You can use boost::lexical_cast ...

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


    You can look into this page:-

    Reetesh Mukul, Sep 18, 2007
    1. Advertisements

  3. frohlinger

    frohlinger 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?
    frohlinger, Sep 18, 2007
  4. frohlinger

    Jim Langston Guest

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

    Pete Becker Guest

    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
    Pete Becker, Sep 18, 2007
  6. frohlinger

    frohlinger Guest

    Hi Pete,
    I could not find a single documentation on wcstof.
    Are you sure this is the write method?
    frohlinger, Sep 18, 2007
  7. frohlinger

    Pete Becker Guest

    The first six hits I got on Google include five man pages that cover wcstof.
    Pete Becker, Sep 18, 2007
  8. frohlinger

    Barry Guest

    #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
    Barry, Sep 18, 2007
  9. Try wcstod which certainly is available for Visual C++. Beware, the
    strto. and wcsto. functions are tricky to use!
    Roland Pibinger, Sep 18, 2007
  10. frohlinger

    James Kanze Guest

    Fallible< double >
    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, Sep 19, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.