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



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
.



Relevant Pages

  • Re: how can we check to not enter the any string or char?
    ... fgets(str, 4, stdin) to be specific ... goto enter_again; ... 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)