Originally Posted by
whiteflags
So just make a conditional return, at the end of the function. Either <min or >max could be a normal unrecoverable error. It just so happens the only unrecoverable errors are:
Code:
if (ferror (stdin) || feof (stdin)) {
clearerr (stdin);
return min - 1;
}
else {
return return_value;
}
I would stick to comparing errno with 0 or errno.h macros. errno is a standardized variable, so use your own flags rather than extending errno.
This brings up another point I am wrestling with. Is is better to do all the error checking inside the function and force reentry from there or is it better to do that outside. I could pass a pointer to the function to change the value of the variable I want to contain the input number and then use the return for any error condition that occurred.
Code:
int main(void){
int number, min, max, errorCode;
do{
UserInput(&number, min, max);
}
while (errorCode);
}
int UserInput( int *pNumb, min, max){
int errorCode;
char buffer[BUFSIZ];
char *pEnd;
errorCode = 0;
// get the input with fgets
*pNumb = strtol(buffer,&pEnd,10);
// check if valid then set errorCode
return errorCode;
}
or force the reentry inside the function.
Code:
int main(void){
int number, min, max
number = UserInput(min, max);
}
int UserInput(min, max){
int errorCode;
int input
char buffer[BUFSIZ];
char *pEnd;
errorCode = 0;
do{
// get the input with fgets
input = strtol(buffer,&pEnd,10);
// check if valid then set errorCode
}
while (errorCode);
return input;
}
Does it even matter?? My thought is that the force of reentry is better inside the function and returning input not errorCode.