I'm building a program that calculates wether the entered value is a prime number or not. This is the setValue method:
Value is declared in the header file, it's of type long long. When -15 is inserted, the output is as expected:
Type in a number to check if it's prime.
-15
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
However, when entering a number that's not of type long long, i.e. 0.2, scanf() seems to be skipped:
Type in a number to check if it's prime.
0.2
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
Etcetera.
I've searched for ways to check wether the value is a value my program can deal with (type long long) and to actually wait for another value to be entered, but I found nothing suitable. I also tried a lot, like using another variable called input that was of type long double, but nothing worked.
Also, when I enter a number higher than 2^63, i.e. 100000000000000000000, my program doesn't tell it's actually too big:
Type in a number to check if it's prime.
100000000000000000000
No, 9223372036854775807 is not a prime number, 9223372036854775807 = 7^2 x 73 x 127 x 337 x 92737 x 649657.
Help would definitely be appreciated.
Code:
-(void)setValue
{
Boolean instructionsNeeded = NO;
printf("\nType in a number to check if it's prime.\n");
for ( ; ; )
{
if (instructionsNeeded == YES)
{
printf("Please insert a positive integer, smaller than 2^63 (%li).\n", 9223372036854775807);
}
scanf("%lli", &value);
if (value < 1)
{
printf("Oops, your value is too small. ");
instructionsNeeded = YES;
}
else if (value > 9223372036854775807)
{
printf("Oops, your value is too big. ");
instructionsNeeded = YES;
}
else
break;
}
[self primeTester:value];
}
Value is declared in the header file, it's of type long long. When -15 is inserted, the output is as expected:
Type in a number to check if it's prime.
-15
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
However, when entering a number that's not of type long long, i.e. 0.2, scanf() seems to be skipped:
Type in a number to check if it's prime.
0.2
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
Oops, your value is too small. Please insert a positive integer, smaller than 2^63 (9223372036854775807).
Etcetera.
I've searched for ways to check wether the value is a value my program can deal with (type long long) and to actually wait for another value to be entered, but I found nothing suitable. I also tried a lot, like using another variable called input that was of type long double, but nothing worked.
Also, when I enter a number higher than 2^63, i.e. 100000000000000000000, my program doesn't tell it's actually too big:
Type in a number to check if it's prime.
100000000000000000000
No, 9223372036854775807 is not a prime number, 9223372036854775807 = 7^2 x 73 x 127 x 337 x 92737 x 649657.
Help would definitely be appreciated.