Re: how can we check to not enter the any string or char?
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 01 Nov 2007 19:01:34 +0000
CBFalconer wrote, On 01/11/07 16:12:
"emre esirik(hacettepe computer science and engineering)" wrote:friends thank you for all but I am new programmer, I am just
learning C language at hacettepe university comp.science and eng.
so I still dont understand anything, just I want to check if user
enter numbers, it only can be between 1-100 and if user enter
character, give a error,"you cant use character,enter again,
so how can I do this by the easiest way?
<snip>
The cleanest way is to input chars until you have something that
will not fit. For example:
Reading a line and then using strtol is cleaner in my opinion, it has limitations but requires a lot less code. Also, there are several changes I would make to your version...
unsigned int rdvalue(FILE *f) {
unsigned int ch, value, err;
value = err = 0;
unsigned int ch;
unsigned int value = 0;
unsigned int err = 0;
while (EOF != (ch = getc(f))) {
/* ch contains an int, which is the value of the char */
if (!isdigit(ch)) break; /* Only interested in digits */
while ((ch=getc(f)) != EOF && isdigit(ch))
ch = ch - '0'; /* form digit value */
ch -= '0';
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.
/* 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.
Look up the standard include files needed to handle anything not
defined above. The result is acquired without needing any buffers,
accepts any number of leading zeroes, etc. Once this is working
you can use it to input signed ints, etc. Or you can first expand
it to handle longs.
Of course, you have just done most of this chaps homework.
--
Flash Gordon
.
- Follow-Ups:
- Re: how can we check to not enter the any string or char?
- From: CBFalconer
- Re: how can we check to not enter the any string or char?
- References:
- Re: how can we check to not enter the any string or char?
- From: andreyvul
- Re: how can we check to not enter the any string or char?
- From: Jack Klein
- Re: how can we check to not enter the any string or char?
- From: Philip Potter
- Re: how can we check to not enter the any string or char?
- From: santosh
- Re: how can we check to not enter the any string or char?
- From: emre esirik(hacettepe computer science and engineering)
- Re: how can we check to not enter the any string or char?
- From: CBFalconer
- Re: how can we check to not enter the any string or char?
- Prev by Date: Re: C return a++ - is it safe?
- Next by Date: Re: A Trend Towards Lower Software Maintenance Budgets?
- Previous by thread: OT - Re: how can we check to not enter the any string or char?
- Next by thread: Re: how can we check to not enter the any string or char?
- Index(es):
Relevant Pages
|