# Verifying single digit input

Printable View

• 05-31-2005
Syked4
Verifying single digit input
I was waondering if anyone could tell me how to verify a single digit input. I've been able to catch an Alpha and also a number greater than nine and less than zero but just can't seem to put the two together. This is a school assignment and we've been told that verification is not necessary but this has really been bugging me. :confused:
• 05-31-2005
^xor
Verify what? Why can't you use isdigit?
• 05-31-2005
quzah
Or even better, isalnum. :)

Quzah.
• 05-31-2005
^xor
I thought he wanted to check whether the input was a digit or not. :p

Did you read my PM by the way? :)
• 05-31-2005
Syked4
Thanks for the quik reply. I'm afraid your dealing with a complete n00b here. I tried that but was probably running into 'type' problems. If I understand it right this requites a char argument, right. and returns a 1 if true
• 05-31-2005
Syked4
I think a light has come on. If I use isdigit and also want catch a double digit I need to use a sting and test the second character as well. Am I getting close? :confused:
• 05-31-2005
Syked4
Thanks for your help and letting me ramble. I think I got it. This seems to work :D

Code:

```#include <stdio.h> int main() {   char num[10];   int test = 0;     printf ("Enter a single digit:> ");   scanf("%s", &num);   while (test == 0) {     if (isdigit(num[0])&&(num[1]==NULL)) {         printf("The number you entered was %c.\n", num[0]);         test = 1;              }     else {       printf("Not a single digit\nTry again:> ");       scanf("%s", &num);       }   }   system("PAUSE");          return(0); }```
• 05-31-2005
itsme86
This is wrong in a scary way:
Code:

`num[1]==NULL`
NULL should only be used when dealing with pointers. It's confusing because you hear strings are "null-terminated". Well, in that case it's talking about the ASCII value 0, not the NULL macro definition, which is generally defined as (void *)0. Any of these alternatives would be a whole lot better:
Code:

```!num[1] num[1]==0 num[1]=='\0'```
• 05-31-2005
Syked4
Thankyou :o