Thread: Verifying single digit input

  1. #1
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12

    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.

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    Verify what? Why can't you use isdigit?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or even better, isalnum.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    FOX
    Join Date
    May 2005
    Posts
    188
    I thought he wanted to check whether the input was a digit or not.

    Did you read my PM by the way?

  5. #5
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    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

  6. #6
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    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?

  7. #7
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    Thanks for your help and letting me ramble. I think I got it. This seems to work

    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);
    }

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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'
    If you understand what you're doing, you're not learning anything.

  9. #9
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    Thankyou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM