Thread: Validate double number

  1. #31
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think this works pretty good. It uses a regular expression to validate a double. It most likely does NOT illustrate best practices for regular expression use in C.

    Code:
    #include <stdio.h>
    #include <regex.h>
    
    int main (int argc, const char * argv[]) {
    	char data[80] ; 
    	int rc ; 
    	regex_t * myregex ; 
    	regmatch_t arrayOfMatches[2];
    	
    	// Compile the regular expression 
    	rc = regcomp( myregex, 
    		"^ *[-+]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][-+]?[0-9]+)? *$", 
    		REG_EXTENDED | REG_NOSUB ) ; 
    	printf("RC from regcomp() = &#37;d\n", rc); 
    	
    	
    	printf("Enter a double value\n");
    	scanf("%s",data) ; 
    	
    	// Compare the entered value to the regex 
    	if (!regexec(myregex, data, 1 , arrayOfMatches, 0 ) ) {
    		printf("double %s is valid.\n", data) ; 
    	}
    	else printf("double %s is not valid\n", data ); 
    	return 0;
    }
    Todd

  2. #32
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, I think my regex is correct, but it reports 123e as being valid. Methinks the default RE support on a Mac might not be working properly.

    Todd

  3. #33
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    It also reports --123 as being valid. I suppose this might be why folks recommend the perl compatible regular expression (pcre) library over the default lib.

    Todd

  4. #34
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Never mind, unless someone can explain the bogus results. It also says 123ee-4 is valid.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so what's wrong with using this:
    Code:
    int main()
    {
       char buf[40];
       char *endPt;
       double x;
       fgets(buf, sizeof(buf), stdin);
       buf[strlen(buf)-1] = '\0;'
       x = strtod(buf, &endPt);
       if (endPt != '\0') printf("Not a valid number\n");
       else printf("x = %f\n", x);
       return 0;
    }
    It's fairly efficient compared to using regular expressions, and it validates that the entire string is accepted by strtod.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #36
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by matsp View Post
    Right, so what's wrong with using this:
    Code:
    int main()
    {
       char buf[40];
       char *endPt;
       double x;
       fgets(buf, sizeof(buf), stdin);
       buf[strlen(buf)-1] = '\0;'
       x = strtod(buf, &endPt);
       if (endPt != '\0') printf("Not a valid number\n");
       else printf("x = &#37;f\n", x);
       return 0;
    }
    It's fairly efficient compared to using regular expressions, and it validates that the entire string is accepted by strtod.

    --
    Mats
    LOL. Other than it doesn't work, nothing! (I fixed the typo with the semi colon). I entered 123.45 and it says it's not valid.

    Todd

  7. #37
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This fixes it.

    Code:
    int main()
    {
       char buf[40];
       char *endPt;
       double x;
       fgets(buf, sizeof(buf), stdin);
       buf[strlen(buf)-1] = '\0';
       x = strtod(buf, &endPt);
       if (*endPt != '\0') printf("Not a valid number\n");
       else printf("x = &#37;f\n", x);
       return 0;
    }

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, yes, the star was missing. I never actually TRIED it, I just wrote it "from the hip".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #39
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think it is a clever approach - leveraging the second parm to strtod(). I found one case, that of just pressing enter, that didn't work. So, thanks to matsp, here's the final version.

    Code:
    int main()
    {
       char buf[40];
       char *endPt;
       double x;
       fgets(buf, sizeof(buf), stdin);
       buf[strlen(buf)-1] = '\0' ; 
       x = strtod(buf, &endPt);
       if (*endPt != '\0' || strlen(buf)==0 ) printf("Not a valid number\n");
       else printf("x = &#37;f\n", x);
       return 0;
    }

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, 09:13 AM
  4. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 02:23 PM
  5. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 09:54 PM