Thread: Valid Number

  1. #1
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52

    Cool Valid Number

    How can you validate a number input by a user. The object is to avoid wrong entries like abc, iab, ab1, a1b. I have got a lengthy method. Is there any simple method?
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  2. #2
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    I once wrote a long ass piece for this simple shizzle too

    ctype.h -> isdigit() oder isalpha()

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Wasn't this just answered in another thread?

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >Is there any simple method?
    There is indeed:
    Code:
    #include <stdlib.h>
    #include <limits.h>
    
    int valid ( const char *number )
    {
      char *end;
      long result = strtol ( number, &end, 0 );
    
      /* Did we look at the entire string? */
      if ( end != number && ( *end == '\n' || *end == '\0' ) ) {
        /* Is it in the range of int? */
        if ( result >= INT_MIN && result <= INT_MAX )
          return 1;
      }
    
      return 0;
    }
    That's assuming you're looking for integers. It's just as simple for other types, especially if you have a C reference handy to look up strtoul, strtod, and strtold.

  5. #5
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52

    Thumbs down

    Quote Originally Posted by mako
    I once wrote a long ass piece for this simple shizzle too

    ctype.h -> isdigit() oder isalpha()
    But friend,

    You shold note the point that if the input is lik 12a,12ad2 (number followed by alphabet or alphabet between numbers)
    the function isdigit() tells that they are integers. But actually they are wrong entries.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  6. #6
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    The solutions treat entries like 12h,12j2 .... as numbers.
    I need a simple filter for these too.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  7. #7
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >The solutions treat entries like 12h,12j2 .... as numbers.
    Mine doesn't, it makes sure that the entire string participates in the conversion and then that the result is within the range of an int. Is that not the functionality that you want?

  8. #8
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Does any one have an effective solution to this?
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  9. #9
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    friend:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
      int ok=1;
      int i=0;
      char str_2_b_checkd[10]="1a23";
    
      while( i<strlen(str_2_b_checkd)&&ok==1)
      {
        if(isdigit(str_2_b_checkd[i])) ok=1;
        else ok=0;
        i++;
      }
    
      if(ok==1) printf("NUMBER");
      else printf("NOTTA NUMBER");
    
      getch();
      return 0;
    }
    Last edited by mako; 01-06-2006 at 08:53 AM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Think Slacker's solution is quite ok. Only trouble is that it doesn't detect over/underflow condition. ( trouble is long and int have same range on ia32 ).
    Another thing I noticed is that strtol() just consumes every digit and doesn't fail on overflow. ( maybe there is a flag somewhere that I don't know about ).
    I came up with a hack that works ( ? ).
    Try this
    Code:
    #include <stdlib.h>
    #include <limits.h>
    
    int valid_int ( const char *number ) {
       char *end;
       char *dend;
       double dresult = strtod ( number, &dend );
       long result = strtol ( number, &end, 0 );
    
       /* Did we look at the entire string? */
       if ( end != number && ( *end == '\n' || *end == '\0' ) && end == dend ) {
          /* overflow ?? */
          return (double)result == dresult;
       }
       return 0;
    }
    
    int valid ( const char *number ) {
       char *end;
       long result = strtol ( number, &end, 0 );
    
       /* Did we look at the entire string? */
       if ( end != number && ( *end == '\n' || *end == '\0' ) ) {
          /* Is it in the range of int? */
          if ( result >= INT_MIN && result <= INT_MAX )
             return 1;
       }
       return 0;
    }
    
    
    int main() {
       const char * number = "3334567891";
       printf("%s is %sa valid integer\n", number, valid(number) ? "":"not " );
       printf("%s is %sa valid integer\n", number, valid_int(number) ? "":"not " );
    }
    my output
    Code:
    3334567891 is a valid integer
    3334567891 is not a valid integer
    Kurt

  11. #11
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Let me explain it more clearly

    Condider the code:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
         int n;
         printf("Enter a number:");
         scanf("%d",&n);
         
    }
    The requirement is to check n is an integer or not.

    n is an integer if the input has only integers and no alphabets
    example for valid numbers are 1,23,5353 etc

    The invalid entries include the following pattern of entries:
    abc
    abc12
    a123
    a12b

    The program is required to detect the above 5 invalid entries.

    I think now it is clear.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  12. #12
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Zuk the code is too long for such a simple operation.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you know a shorter one, let me know.
    K.

  14. #14
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    I didn't want to scare him with a rock solid solution when he might not need it. strtol does support tests for overflow and such, but the code gets more complicated:
    Code:
    #include <stdlib.h>
    #include <limits.h>
    #include <errno.h>
    
    int valid ( const char *number )
    {
      char *end;
      long result;
    
      errno = 0;
      result = strtol ( number, &end, 0 );
    
      /* Did we look at the entire string? */
      if ( end != number && ( *end == '\n' || *end == '\0' ) ) {
        /* Overflow? */
        if ( errno == ERANGE )
          return 0;
    
        /* Is it in the range of int? */
        if ( result >= INT_MIN && result <= INT_MAX )
          return 1;
      }
    
      return 0;
    }
    >I came up with a hack that works ( ? ).
    I'm not sure off-hand, but I'd end up checking the standard before using it because it doesn't look squeaky clean at a glance. Of course, I'm usually paranoid and it's probably perfectly safe and elegant except for calling two conversion functions where only one is needed.

    >The requirement is to check n is an integer or not.
    n is guaranteed to be an integer if scanf succeeds. You should test for that:
    Code:
    if ( scanf ( "%d", &n ) == 1 ) {
      /* Success! */
    }
    >The program is required to detect the invalid entries.
    Then you shouldn't use scanf to read an integer. It'll stop the conversion when it reaches an invalid character or whitespace, but if it converted any characters before that it'll say it succeeded. That's the design of scanf and it's working as intended. What you want is to read a whitespace delimited token and test it for being a valid integer, in which case you want to use string input so you have more control:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <errno.h>
    
    int valid ( const char *number )
    {
      char *end;
      long result;
    
      errno = 0;
      result = strtol ( number, &end, 0 );
    
      /* Did we look at the entire string? */
      if ( end != number && ( *end == '\n' || *end == '\0' ) ) {
        /* Overflow? */
        if ( errno == ERANGE )
          return 0;
    
        /* Is it in the range of int? */
        if ( result >= INT_MIN && result <= INT_MAX )
          return 1;
      }
    
      return 0;
    }
    
    int main ( void )
    {
      char n[256];
    
      printf ( "Enter a number: " );
    
      if ( scanf ( "%255s", n ) == 1 && valid ( n ) )
        puts ( "It's a number!" );
      else
        puts ( "Not a number" );
    
      return 0;
    }
    >Zuk the code is too long for such a simple operation.
    It's not as simple an operation as you think.
    Last edited by Slacker; 01-06-2006 at 09:08 AM.

  15. #15
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Thank You Slacker for your effort. But I dont think this is effective.
    As you know, most of the programs accept an integer using scanf and an invalid entry from the user can make your program fail. To avoid this we need to ask the user again to enter a valid one.
    Understood the importance.

    Any more solutions??
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 08:57 PM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM