# How to check whether input is positive...

Printable View

• 09-03-2012
Giri
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
• 09-03-2012
Adak
Quote:

Originally Posted by Giri
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");```
• 09-03-2012
root4
"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).
• 09-03-2012
grumpy
Quote:

Originally Posted by Giri
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.
• 09-03-2012
AndiPersti
Quote:

Originally Posted by Adak
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
• 09-03-2012
Subsonics
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.
• 09-03-2012
Adak
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.
• 09-03-2012
christop
Quote:

Originally Posted by Adak
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.
• 09-03-2012
Giri
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
• 09-03-2012
Subsonics
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.
• 09-04-2012
iMalc
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.