Re: how can we check to not enter the any string or char?
- From: andreyvul <andrey.vul@xxxxxxxxx>
- Date: Wed, 31 Oct 2007 16:33:33 -0700
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 */
/* a char-
encoded digit or a NULL*/
/* (end-of-
string terminator) */
goto enter_again;
n_mines = atoi(str);
if (n_mines < 1 || n_mines > 100)
goto enter_again:
enter_again:
printf("Please only enter between 1 and 100");
}
Note: in this situation, a goto was useful. However, gotos are like
#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.
.
- Follow-Ups:
- 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: pete
- Re: how can we check to not enter the any string or char?
- Prev by Date: Re: printf("%d",float)
- Next by Date: future technologies.........
- Previous by thread: Re: printf("%d",float)
- Next by thread: Re: how can we check to not enter the any string or char?
- Index(es):
Relevant Pages
|