Thread: How to check whether input is positive...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    How to check whether input is positive...

    Hi Guys,

    I am trying to read in TWO unsigned long long variables, however, I would like to know whether the user actually entered two positive numbers within the range of an
    unsigned long long variable.

    Is there a simple way to achieve this? I have tried assigning the input to a string and then trying to separate that into two numbers but without any luck.

    Thanks for your help!

    Kind Regards,

    Giri

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Giri View Post
    Hi Guys,

    I am trying to read in TWO unsigned long long variables, however, I would like to know whether the user actually entered two positive numbers within the range of an
    unsigned long long variable.

    Is there a simple way to achieve this? I have tried assigning the input to a string and then trying to separate that into two numbers but without any luck.

    Thanks for your help!

    Kind Regards,

    Giri
    #include <limits.h>

    print up the MAX_ULL value (I believe that's the right macro, but if it's not, read through limits.h (it's a text file), and find the macro your compiler is using.

    then:
    Code:
    if(input > -1 && input < MAX_ULL)
       print("range of input is good\n");

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    "unsigned long long" being the largest type, any unsigned integer variable "i" will necessarily be within range of [0, ULLONG_MAX],
    and your compiler should warn you of a condition such as 0<=i && i<=ULLONG_MAX being useless.
    You have to compare the string representation of the two values against the upper bound ("stringified" ULLONG_MAX).

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Giri View Post
    Is there a simple way to achieve this? I have tried assigning the input to a string and then trying to separate that into two numbers but without any luck.
    That must mean you're not doing it right. To do what you want, one sensible way is to read input into a string, and interpret contents of the string. Simple logic says that the string must contain only digits (assuming you work only in base 10) and no minus sign. Then just work out a way to convert a string of digits to a value, while checking for overflow.

    Oh, under C99, the maximum value of a unsigned long long is ULLONG_MAX. Older versions of C did not support long long types.
    Last edited by grumpy; 09-03-2012 at 03:43 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Adak View Post
    print up the MAX_ULL value (I believe that's the right macro, but if it's not, read through limits.h (it's a text file), and find the macro your compiler is using.
    It's ULLONG_MAX and it's not compiler specific but defined in the standard.

    Code:
    if(input > -1 && input < MAX_ULL)
       print("range of input is good\n");
    That doesn't really work:
    Assuming "input" is of type unsigned long long, you are comparing an unsigned type with a signed type (you should get a warning from your compiler). Thus the signed type is converted to the unsigned type, i.e. -1 is converted to unsigned long long and will be -1 + (ULLONG_MAX + 1) which is ULLONG_MAX. So you are comparing
    Code:
    if (input > ULLONG_MAX && input < ULLONG_MAX)
    which is always false.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    When you have read your input as a string you can use strtoull() to convert it to a unsigned long long. If there was an overflow, errno will be set to ERANGE, but you can test for errno != 0.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    On my old compiler there WAS no ULLONG_MAX. No unsigned long long's at all. So I'd call it compiler specific. Calling it C standard specific works.

    Using Subsonics or Grumpy's idea for this, sounds perfect. The "wrap around" when the ULL variable goes out of range, causes problems when you try to check it, as an ULL.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by Adak View Post
    On my old compiler there WAS no ULLONG_MAX. No unsigned long long's at all. So I'd call it compiler specific. Calling it C standard specific works.
    My old compiler (pre-ANSI K&R C compiler) doesn't have void at all, but that doesn't stop me from using void with a compiler from this century either.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Hi All,

    Thanks so much for the responses! I have been playing around with the below code and have a question regarding it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void){
    
        char str[5] = {0};
        unsigned long long number_1 = 0;
        unsigned long long number_2 = 0;
        int position = 0;
        
        printf("Input two integers:");
    
        scanf("%[^\n\t]s",str);
        
        int x = 0;
        
        while (str[x] != '\0'){
            if (!isdigit(str[x]) && !isspace(str[x])){
                printf("Incorrect Input\n");
                return EXIT_FAILURE;
            }
            ++x;
        }
    
        int a = sscanf(str,"%llu %llu%n",&number_1,&number_2,&position);
        return EXIT_SUCCESS;
        }
    If I run the code with inputs "123 123", this exceeds the number of characters in the array "str", yet, 123 is being stored to both number_1 and number_2.
    I would have thought that since the str array only contained "123 1", number_2 would have a value of 1?

    I also have assigned a value to the variable named "position" in the sscanf function. My intention with this was to check whether "position" exceeded 5 in which case I would know that the user had entered input longer than 5 characters...
    However, if values are being assigned correctly even when more that 5 characters are being assigned, is there any real need for this?

    Please forgive me if my code seems somewhat haphazard.

    Thanks again for your help.

    Kind Regards,

    Giri

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You never test or restrict the length of the string, you only test for '\0'.

    Also, if the highest value allowed is 999, why not just use an int and test if it's between 0 and 999? Then you don't need any strings or anything like that. If your highest value truely is ULLONG_MAX then the length of the string can not be used as indicator if the number is in base 10.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One option is to parse the string into the unsigned long long, then convert that back into a string and check if it matches the original string (leading and trailing whitespace excluded of course). That's a pretty sure way to check that it was in range!
    Only issue would be if they entered a leading plus sign or leading zeros, but I imagine that there's no rule that your program should necessarily accept that, and you could work around it anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get a positive/negative input from user
    By never_lose in forum C Programming
    Replies: 2
    Last Post: 04-12-2011, 06:36 PM
  2. input check
    By fatsrir in forum C Programming
    Replies: 2
    Last Post: 05-06-2010, 02:37 PM
  3. Check if a string is a positive integer, please help.
    By Phantom2303 in forum C Programming
    Replies: 1
    Last Post: 03-16-2009, 09:55 PM
  4. how to check if input is only positive number?
    By hanek in forum C Programming
    Replies: 16
    Last Post: 04-26-2004, 11:57 AM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM