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



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.

.



Relevant Pages

  • Re: how can we check to not enter the any string or char?
    ... fgets(str, 4, stdin) to be specific ... The line above is a valid goto statement. ... loop, ... you did not initialize n_mines before the loop. ...
    (comp.lang.c)
  • Re: Iteration in lisp
    ... state machine is just a tagbody and goto. ... But if you need to change the state-transition-matrix in the middle ... loop and either table-driven or separate-functions for defining the ... (tagbody state1 (trace-state1 aux) ...
    (comp.lang.lisp)
  • Re: COBOL aint quite dead - yet !
    ... of control there is nothing wrong with goto. ... The loop is entirely abstracted away. ... Then we have well-formed loops with a single invariant and a looping ...
    (comp.lang.cobol)
  • Re: acceptable use of goto?
    ... loop, forward within the loop at various points, then forward out of ... Then you give every statement a label. ... No goto statements. ... Then I build a framework (of if or switch statements) to eliminate the ...
    (comp.lang.c)
  • Re: GSoft BASIC Ramble
    ... Leave and exit statement are not simply "goto statement without a line ... They are structure and proper ways of leaving a loop when a ... small system programming community--and the result was not pretty! ...
    (comp.sys.apple2)