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?
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
I once wrote a long ass piece for this simple shizzle too
ctype.h -> isdigit() oder isalpha()
Wasn't this just answered in another thread?
>Is there any simple method?
There is indeed:
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.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; }
But friend,Originally Posted by mako
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
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
>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?
Does any one have an effective solution to this?
L GIK wins!!!
Salutes from logicwonder
Enjoy programming
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.
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
my outputCode:#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 " ); }
KurtCode:3334567891 is a valid integer 3334567891 is not a valid integer
Let me explain it more clearly
Condider the code:
The requirement is to check n is an integer or not.Code:#include<stdio.h> int main(void) { int n; printf("Enter a number:"); scanf("%d",&n); }
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
Zuk the code is too long for such a simple operation.
L GIK wins!!!
Salutes from logicwonder
Enjoy programming
If you know a shorter one, let me know.
K.
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:
>I came up with a hack that works ( ? ).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'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:
>The program is required to detect the invalid entries.Code:if ( scanf ( "%d", &n ) == 1 ) { /* Success! */ }
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:
>Zuk the code is too long for such a simple operation.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; }
It's not as simple an operation as you think.
Last edited by Slacker; 01-06-2006 at 09:08 AM.
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