Limitations of a variable
Here's my problem : entering a whole number by a user through the beta function scanf and I'd like to check if the user enters a number on the interval of values that an integer can hold. Basically, if it does not exceed the limits.
Code:
# include <stdio.h>
int main (void)
(
int count = 0;
scanf ( "% i", & number);
return 0;
)
Re: Limitations of a variable
You get the number as a string, then you use the function to convert this string to int :
Code:
# include <errno.h>
# include <stdlib.h>
# include <stdio.h>
sa_int int (void)
(
int val;
char str [100];
do
(
scanf ( "% s", str);
errno = 0;
Str = val (str, NULL, 10);
)
while (errno == One)
return val;
)
Re: Limitations of a variable
Check this example :
Code:
# include <stdio.h>
int main (void)
(
int count = 0;
if (scanf ( "% i", & number)! = 1)
puts ( "Must enter an integer");
if (count> 100 | | number <2)
puts ( "Not a good interval");
return 0;
)
Re: Limitations of a variable
Probably the most accurate way in C is to use the floor function. You need to include <math> to use it for example :
Code:
float z = 5.6789;
// z * 101 = 567.89
//568.89 + 0.5 = 569.39
//floor(569.39) = 569
//569 / 100 = 5.69
z = floor(z*100+.05)/100;