is there a C++ IsNumeric function as in VB?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: is there a C++ IsNumeric function as in VB?

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    is there a C++ IsNumeric function as in VB?

    I'm looking fo a function that will act like IsNumeric at VB
    meaning: Returns True if an expression can be evaluated as a number

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Short answer: No there isn't, but you could write your own (assuming you are talking about strings)

    Long answer: Why do you need a function like that ? Many times in VB programmers tend to use strings for a host of different things, since it's a pain to define more appropriate data structures or I/O routines. I'd say that you probably don't need a function like this if you think about what your code should do and how it should be designed from a C++ point of view.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    well actually I do need this function
    I'm getting a string from a server and need to know if it's numeric...

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    How involved does the function have to be ? VBs IsNumeric is actually a bit more complex than it first appears, since it will recognize number formats dependent on the current thread locale. For example on an english system, IsNumeric("3,182,263.4746") will return True. If you need that kind of handling then have a look at GetNumberFormat in MSDN and work from there.

    If your numbers are only composed of decimal digits (no decimal dot, no comma, no hexadecimal numbers, no Unicode numbers like arabic etc.), then it's much easier.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    what about this?

    Code:
    bool is_numeric( const std::string& s )
    {
    std::stringstream ss(s);
    double d;
    ss >> d;
    return ss.good();
    }

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Look at strtod, strtol and strtoul. If you use the endptr parameter then you might have a very simple way to do what you need to do.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  7. #7
    Join Date
    Sep 2003
    Posts
    815
    mwilliamson

    what is 'good' function?

  8. #8
    Join Date
    Sep 2003
    Posts
    815
    is that enough or that there are more options for numeric value?
    I don't have arabic numbers

    bool IsNumeric(char ch)
    {
    if(ch >= '0' && ch <= '9')
    return true;
    return false;
    }

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by avi123
    what is 'good' function?
    'good()' return true if the stream is okay (the goodbit is 'set')...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by avi123
    is that enough or that there are more options for numeric value?
    I don't have arabic numbers
    Well...that should be fine...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center