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.
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; }
I'm A Farmer
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.