Validate double number

This is a discussion on Validate double number within the C Programming forums, part of the General Programming Boards category; I assume buf is your buffer - the string you read from the user: Code: char buf[255]; fgets(buf, sizeof(buf), stdin); ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    I assume buf is your buffer - the string you read from the user:
    Code:
    char buf[255];
    fgets(buf, sizeof(buf), stdin);
    Null as in NULL?
    Like if (p == NULL)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Ah I see. Thanks for the explanation. I was thinking of just strtod.

    However, if I may:
    Code:
    if ( strtod(buf, NULL) == 0 && (buf[0] != '0' && ((buf[0] != '\0') && buf[0] != '-' && buf[1] != '0')) ) /* Not a double! */;
    ?
    Last edited by robwhit; 01-14-2008 at 02:50 PM.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Ely

    does the same function works for floats also as well?

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Quote Originally Posted by robwhit View Post
    Ah I see. Thanks for the explanation. I was thinking of just strtod.

    However, if I may:
    Code:
    if ( strtod(buf, NULL) == 0 && (buf[0] != '\0') && (buf[0] != '0' && (buf[0] != '-' && buf[1] != '0')) ) /* Not a double! */;
    ?
    Yes! That might be safer!

    Quote Originally Posted by guaro555 View Post
    Ely

    does the same function works for floats also as well?
    Yes, it works for floats as well, just change strtod to strtof.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Well, just call me a fly in the ointment.

    This will fail the test and be called valid: 0DLFKD

    It converts to zero since it's garbage, and the first char is zero, so it would be accepted.

    I know, I know. I'm just stirring the pot.

    Todd

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Fine!
    How about this?
    Code:
    if (d == 0 && strcmp(buf, "0") != 0 && strcmp(buf, "-0") != 0) /* Not a double! */;
    You really are doing your best to thwart my optimization, aren't you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Really, to get the most out of strtod you have to take advantage of all of it's arguments. Knowing where strtod stopped its conversion helps you decide what is acceptable and not acceptable input. Dave Sinkula has illustrated the proper technique in a series of snippets.

    My understanding is that strtod works for floats just fine:

    float fltpt = (float) strtod( example, &endpt );

    http://www.daniweb.com/code/snippet597.html
    Last edited by whiteflags; 01-14-2008 at 02:55 PM.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    I suppose... I'm used to atof/atod/atoi, which only takes one. Hmmm.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Ah you caught my post before I saw my error....

    the strcmp won't work because there might be more characters afterwards.

    lol we are such a waste of bandwidth.

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Code:
    char* pEnd;
    double d = strtod(buf, &pEnd);
    if (pEnd - buf == 0) /* "Not a double!" */;
    That should work fine, too.

    Quote Originally Posted by robwhit View Post
    Ah you caught my post before I saw my error....

    the strcmp won't work because there might be more characters afterwards.

    lol we are such a waste of bandwidth.
    Not sure I get what you mean with strcmp won't work since d will ONLY be 0 if the user entered "0" or "-0" or a non-string, so what's the error?
    Last edited by Elysia; 01-14-2008 at 03:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I'm assuming it's part of a stream.

    I'm beginning to have brain problems...
    Last edited by robwhit; 01-14-2008 at 03:09 PM.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Still unsure
    How do you expect it to fail? Example, please?
    Unless it's part of your "problems"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    for example, you read in a buffer from a file:

    char buf[128];
    fread(buf, sizeof(buf), 1, file);

    then you try to see if it contains a double.

    oops, what if there is more data than the double? It will not have a \0, and so it will not compare equal.
    Last edited by robwhit; 01-14-2008 at 03:17 PM.

  14. #29
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Quote Originally Posted by Todd Burch View Post
    Well, just call me a fly in the ointment.

    This will fail the test and be called valid: 0DLFKD

    It converts to zero since it's garbage, and the first char is zero, so it would be accepted.

    I know, I know. I'm just stirring the pot.

    Todd

    T

    If I understood the statement correctly you have a few && in there so the first option is true as is garbage like you said and the second is true because is 0, so still not a valid double

  15. #30
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    That is correct. I'm working on some logic to validate a double now. Stay tuned.

    Todd

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 01:23 PM
  5. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 08:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21