Thread: How to stop user from inputting negative number into unsigned long?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    How to stop user from inputting negative number into unsigned long?

    I actually posted this code already for another problem and got some good help on it. But theres one more thing id like to do to make the input even better able to handle user error, but im not sure if its possible or at least easy. I need the function to return a large positive number. As of right now, it can handle users entering characters, but what if the user enters a negative number? is there a way to check to see if what is coming in is negative before the sign gets lost in conversion to unsigned"ness"?

    Code:
    unsigned long getNum(char prompt[80])
    {
       unsigned long darts;
       printf("%s", prompt);
       while((scanf("%lu", &darts)) != 1)
       {
          while(getchar() != '\n')
          ;
          printf("\nThis is not an acceptable entry. please try again.\n\n");
          printf("%s", prompt);
       }
       return darts;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read it as a string
    Try and find a -
    convert the string using sscanf (or strtoul)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Alternately you could echo the input back to them and have them confirm their own typing.

    Enter in a positive integer: -4
    You typed "4294967292" is that correct? y/n N
    Enter a positive integer: 1
    You typed "1" is that correct? y/n Y
    ...

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    scanf and strtoul will both do implicit conversion from negative values to the equivalent unsigned value. So, if you start with just the unsigned value, there's not any way to know that it was originally negative. I would first read it into a signed value and then check whether the signed value is in range.

    Code:
    #include <inttypes.h>
    // ...
    
    // return true if x is not a valid unsigned long
    bool invalid_unsigned_long(int64_t x);
    
    unsigned long getNum(char prompt[80])
    {
       unsigned long darts;
       int64_t x=0;
       printf("%s", prompt);
       while(((scanf("%"SCNd64, &x)) != 1) || invalid_unsigned_long(x))
       {
          while(getchar() != '\n')
            ;
          printf("\nThis is not an acceptable entry. please try again.\n\n");
          printf("%s", prompt);
       }
       darts = (unsigned long)x;
       return darts;
    }
    The function should check whether x is out of range. i.e. less than 0 or greater than ULONG_MAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-18-2011, 09:15 AM
  2. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  3. [question]unsigned number with negative value?
    By learn in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 12:00 AM
  4. Assigning negative number to unsigned variables.
    By sar_mahesh in forum C Programming
    Replies: 10
    Last Post: 09-27-2006, 05:47 PM
  5. Converting an unsigned int to to unsigned long long (64 bit)
    By ninjacookies in forum C Programming
    Replies: 18
    Last Post: 02-11-2005, 12:09 PM