Validate double number

This is a discussion on Validate double number within the C Programming forums, part of the General Programming Boards category; Hi gurus I need to validate an input from the user and to make sure is a valid number, I've ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    Validate double number

    Hi gurus

    I need to validate an input from the user and to make sure is a valid number, I've seemed multiple threads on integer but I really need to validate a double

    123.34 or 345.23 this kind of number and make sure is a valid number


    Thanks a bunch gurus

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Code:
    if (strtod(buf, NULL) == 0 && buf[0] != '0'); /* Invalid number - not a double! */
    Last edited by Elysia; 01-14-2008 at 02:10 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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    if (strtod(buf) == 0 && buf[0] != '0') /* Invalid number - not a double! */ ;
    fixed.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Hehehe. Typically I just put an empty if statement since I have nothing to put in the if. Just to demonstrate
    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. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What about -123.34?

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    well, bad example. I guess the only one that would fail would be -0.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Thanks gurus

    Is there a generic validation in case the user goes as follow

    123.45
    123.00
    123
    -123.45

    Thanks for battling with me here

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Elysia's example will work for all those values. It's a generic validation. Do you understand why / how it works?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    It will only fail for -0, but who in their right mind types that? A little additional logic can fix that, though. Also forgot that strtod takes two arguments. Updated reply.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by robwhit View Post
    Code:
    if (strtod(buf) == 0 && buf[0] != '0') /* Invalid number - not a double! */ ;
    fixed.
    Aha! Checked the docs:
    Code:
    if (strtod(buf, NULL) == 0 && buf[0] != '0') /* Invalid number - not a double! */ ;
    edit: ARRGHGH! beaten.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    If you are really picky about detecting -0, this will work:
    Code:
    if ( strtod(buf, NULL) == 0 && (buf[0] != '0' && (buf[0] != '\0' && buf[0] != '-' && buf[1] != '0')) ) /* Not a double! */;
    Last edited by Elysia; 01-14-2008 at 02:48 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.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Not really T but i case you fill like a brushing your teaching skills with me andexplain I can alway search for it and learn how it works

    thanks again gurus, I though C was going to be easy!!!

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What makes you think it won't accept -0? Or any negative number?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    What the code does is first convert the inputted string (buf) to a double with a call to strtod. Then it checks if the result is 0. Strtod returns 0 in any of the following situations: the string is not a double, or not a number, or the user entered 0. So then I proceed to check the first element in the inputted string (buf[0]) and check if it's '0', too. If it is, then the user entered 0. If it isn't '0', then the user entered a non-number.

    The more complicated example first checks if buf[0] (first input character) is not '0' and also checks if buf[0] is not '-' and the second character, buf[1] is not '0'. That would mean something like, if the first two characters aren't "-0".
    So in essence, if the converted double is 0 AND the first character entered is NOT '0' AND the first two entered character aren't "-0" THEN it's not a valid double.

    Quote Originally Posted by robwhit View Post
    What makes you think it won't accept -0? Or any negative number?
    The first example won't accept -0 because it's the same as 0, so strtod(buf, NULL) == 0 is true BUT buf[0] is NOT 0, it's actually -, so it assumes it's not a double and fails.
    The second example fixes that.
    It does accept negative numbers, however.
    Last edited by Elysia; 01-14-2008 at 02:27 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.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    So ely (Buf) is my user value entry? what does the Null do, now that you are on the roll

    Thanks a bunch again

Page 1 of 3 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