Re: how can we check to not enter the any string or char?



CBFalconer wrote, On 01/11/07 23:47:
Flash Gordon wrote:
CBFalconer wrote, On 01/11/07 16:12:

... snip ...
if (((UINT_MAX - ch) / 10) > value) {
This is meant to be a bounded input, pass in and test against the bounds
allowed rather than some other value.

No, this is intended to ensure the value fits into an unsigned int.

Which is not the OPs requirement.

/* overflow detected, decide what to do */
ch = ch + '0'; /* restore char value */
break;
}
value = 10 * value + ch;
}
ungetc(ch, f); /* keep exit char for the user */
return err;
} /* untested - you check it out */
Still untested by me.

... snip ...
Of course, you have just done most of this chaps homework.

No I haven't. The function is incomplete. Nobody seems to notice
that it doesn't bother to return value, as it should, and that err
should be a further parameter (*err). What I have done is
encourage him to develop a standard reusable input mechanism, whose
value can be checked by the caller, and which doesn't require
(possibly) long strings etc.

I.e. you have done most of his homework by providing something close to a complete solution. I did NOT say you had done it all nor that I had fully reviewed your code.
--
Flash Gordon
.



Relevant Pages