Re: how can we check to not enter the any string or char?
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Wed, 31 Oct 2007 20:53:03 -0500
On Wed, 31 Oct 2007 16:33:33 -0700, andreyvul <andrey.vul@xxxxxxxxx>
wrote in comp.lang.c:
On Oct 31, 6:12 pm, vipvipvipvipvip...@xxxxxxxxx wrote:
fgets() and strtol().
fgets(str, 4, stdin) to be specific
also, there are only up to 4 input bytes, so the loop should be
rewritten to this:
char[4] str;
int i = 0, n_mines = 0;
while (1) {
fgets(str, 4, stdin);
for (i = 0; i < 4; i++)
if ((str[i] < 0x30 && str[i]) || str[i] > 0x39) /* check ascii
value if it's not */
But what if the platform does not use ASCII? C does not require it.
This is extremely foolish, when using '0' and '9' instead of 0x30 and
0x39 guarantees portability to any implementation of C now and
forever.
/* a char-
encoded digit or a NULL*/
/* (end-of-
string terminator) */
goto enter_again;
The line above is a valid goto statement.
n_mines = atoi(str);
At least you did manage to use the generally unsafe atoi() function
safely here, I'll give you that.
if (n_mines < 1 || n_mines > 100)
goto enter_again:
The line above is a syntax error, not a valid goto statement. And
it's completely unnecessary, since the label immediately follows the
botched goto.
enter_again:
printf("Please only enter between 1 and 100");
}
Note: in this situation, a goto was useful. However, gotos are like
There are very few situations where a goto is useful and justified.
This mess is not one of them. Why "mess"? There is no way out of the
loop, no matter what is entered.
#defines and should only be used sparingly.
Also, I hope you meant no tabs, spaces, or other control chars,
because the test checks for numbers only.
Also, you did not initialize n_mines before the loop. This may result
in undefined behavior if you are not careful to later initialize it,
especially in if- and switch-statements. C does not automatically zero
out variables before first use, so initialize your variables at the
top, when they are declared.
That last sentence of yours is only partially correct. C does not
automatically zero out automatic variables before use. It most
certainly initializes all objects with static storage duration to 0 if
they do not have an explicit initializer.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- 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?
- 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?
- Prev by Date: Re: how can we check to not enter the any string or char?
- Next by Date: WWW.SNEAKERS-WHOLESALE.COM cheap wholesale nike sneakers
- Previous by thread: 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
|