Is there anyway that I can check if the input collected by scanf() is of type Interger?
eg, If I enter a number, all is good, and the operation continues.
but if I enter the letter 'a' or a special character like '?' it will stop the program.
Printable View
Is there anyway that I can check if the input collected by scanf() is of type Interger?
eg, If I enter a number, all is good, and the operation continues.
but if I enter the letter 'a' or a special character like '?' it will stop the program.
hi
grab the input to a char array and then check the content of that array so it holds only a digits. if all is ok then convert it to int using atoi or something similar.
Thanks, I'll give that a shot.
You could use something like this:
Code:printf("Enter number: ");
while(scanf("%d",&num) != 1 || num < 0 || num > 200)
{
puts("Out of range");
while(getchar() != '\n') continue;
}
Ack! Thanks for the help, but just realised that wasn't my problem. *sigh*
How can I test a number so that I can detect if it will cause an operation down the line to result in an undefined number (larger than can be stored in the space reserved for an INT)?
The problem is that for example, a number like 477218587 will cause such a problem, but numbers like 477218586, 477218588 or 477218589 do not. It isn't based on size nor being odd or even, and there isn't a pattern that I can see.
The program actually does several loops until a certain number is found. In which case, it returns 'Yes the number works and was completed in 15 iterations', or 'No the number doesn't work' if it overflows beyond max_uint or goes below 0.
Numbers are truncated if they are larger than the built in type in which they are stored. You need to use validation like in Wan Valdez's example.
No that's not going to work. I think the only way is to read in the string
keep it in a string while you check if it's between INT_MIN and INT_MAX then
convert it.
Yes it does work. You can use scanf, you don't have to use that damn string bull crap.
Ordinarily, I would use fgets to read in a line from the user (or a file), and then use sscanf to convert that number to an integer.
However, if you're concerned about overflow, then sscanf won't cut it, you would need to use strtod or strtol.
Code:#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
void one ( char *num ) {
long r1;
double r2;
r1 = strtol( num, NULL, 10 );
if ( errno != 0 ) {
perror( "Conversion error" );
} else {
printf( "Value=%ld\n", r1 );
}
r2 = strtod( num, NULL );
if ( errno != 0 ) {
perror( "Conversion error" );
} else {
printf( "Value=%f\n", r2 );
}
}
void two ( char *num ) {
int res, n;
res = sscanf( num, "%d", &n );
if ( res == 1 ) {
printf( "Value=%d\n", n );
} else {
printf( "Not a number\n" );
}
}
int main ( ) {
/* use fgets to read in the input from the user */
/* This number has the value 2 when truncated to 32 bits */
char num[] = "1311768464867721218";
one( num );
two( num );
return 0;
}
Or else just know the range of the built in type and use scanf!
Thanks for the help guys,
I didn't know that I could do something like
while(scanf("%d",&num) != 1....
That helped checking for data being entered, then I just cheated for the second bit and casted the unsigned int into a double, and doing the operations with that and checking it against UINT_MAX before changing it back to an int if errors were not encountered.
Once again, thanks all.