CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy How to check whether a buffer contains a double number?

    Hello, everyone!

    How to use C (not C++) to check whether a character buffer (char*) contains a double or float number? I want to know how to check it with standard C (not C++) method and not VC++ specific method.

    For example,

    ----------
    char buf1 [5] = "123.4";
    char buf2 [5] = "123.m";
    ----------

    What the result I want to get is, when I input buf1, I can get TRUE and when I input buf2, I can get FALSE.

    I find the function atof is not working properly.


    Thanks in advance,
    Geo

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How to check whether a buffer contains a double number?

    Originally posted by George2
    I find the function atof is not working properly.
    What do you mean with 'not working properly'?

  3. #3
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284
    I guess I know what you mean with
    atof is not working properly.
    for atof the string "123." is a valid float.( even "123" is valid although it is more like an integer ).

    if you want different behavior you have to set up your own parser. use regular expressions.
    kurt

  4. #4
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Re: Re: How to check whether a buffer contains a double number?

    Thanks, Andreas buddy!

    I want atof returns an error status other than 123. when the input is 123.m.

    But the function atof returns the most longest string that matches a double/float number rule. It can be used to check whether the beginning of a string is a double/float number, but can not be used to check whether the whole string is a legal double/float number.

    Can you help?


    regards,
    George
    Originally posted by Andreas Masur
    What do you mean with 'not working properly'?

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy

    Thanks, ZuK buddy!

    Yes, it is just what I mean. Is there a standard C method that can be used to meet my purpose other than regular expression? I think the library of regular expression is too large for a tiny program which is only several thousand lines.


    regards,
    George

    Originally posted by ZuK
    I guess I know what you mean with

    for atof the string "123." is a valid float.( even "123" is valid although it is more like an integer ).

    if you want different behavior you have to set up your own parser. use regular expressions.
    kurt

  6. #6
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    You can write a function to check every char in the array is either a number char or '.' char before feeding it to atof()

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    You can use stdtod() ... the only caveat, depending on what
    you want, you might need to remove trailing blanks from
    the string if you don't want bthat flagged as an error ...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        double x;
        char * endptr;
        char * str = "1.0m";
    
        x = strtod(str,&endptr);
        if (endptr != NULL)
            printf("stopped scan at: %s\n",endptr);
    
        return 0;
    }

  8. #8
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy

    Thanks, CBasicNet buddy!

    Geo
    Originally posted by CBasicNet
    You can write a function to check every char in the array is either a number char or '.' char before feeding it to atof()

  9. #9
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking

    Thanks, Philip buddy!

    Geo

    Originally posted by Philip Nicoletti
    You can use stdtod() ... the only caveat, depending on what
    you want, you might need to remove trailing blanks from
    the string if you don't want bthat flagged as an error ...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        double x;
        char * endptr;
        char * str = "1.0m";
    
        x = strtod(str,&endptr);
        if (endptr != NULL)
            printf("stopped scan at: %s\n",endptr);
    
        return 0;
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured