Re: how can we check to not enter the any string or char?
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Thu, 01 Nov 2007 11:12:04 -0500
"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?
First, learn to quote what you are replying to. Then learn to snip
the portions that have no bearing, and to retain the attributions.
This makes your article stand by itself, which is necessary since
there are no transmission guarantees with the Usenet system.
(Google is just a poor interface to Usenet).
The cleanest way is to input chars until you have something that
will not fit. For example:
unsigned int rdvalue(FILE *f) {
unsigned int ch, value, err;
value = 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 */
ch = ch - '0'; /* form digit value */
if (((UINT_MAX - ch) / 10) > 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 */
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.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
.
- Follow-Ups:
- Re: how can we check to not enter the any string or char?
- From: pete
- Re: how can we check to not enter the any string or char?
- From: David Thompson
- Re: how can we check to not enter the any string or char?
- From: Flash Gordon
- OT - 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?
- 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?
- Prev by Date: Bit Reversal using recursion
- Next by Date: Re: Bit Reversal using recursion
- Previous by thread: Re: how can we check to not enter the any string or char?
- Next by thread: OT - Re: how can we check to not enter the any string or char?
- Index(es):
Relevant Pages
|