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



Jack Klein wrote:
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.

It is unlikely but possible that the character set does not have the digits 0-9 nicely in order. Better to use isdigit().

--
Philip Potter pgp <at> doc.ic.ac.uk
.



Relevant Pages

  • Re: Gracefully stopping an infinite loop after a particular keyboard input
    ... Talking about case 1, where user inputs Q, stdin will keep ... waiting for some input from keyboard and hence will not start the loop ... I should press Qto stop the script, ... proc do_stuff { ...
    (comp.lang.tcl)
  • Re: Which kind of loop do I use, do or while?
    ... FILE *fp = stdin; ... The alternative method would be not to enter the loop if there are no ... process("standard input", stdin, flags) ... the first iteration might be ...
    (comp.programming)
  • Re: flush writing to multiple children IO::Pipe
    ... > 1) the parent's loop that writes to each child seems to have to finish ... > 2) if I throw the parent's "write" sequence in a loop, ... STDIN is not shared amongst the children. ...
    (comp.lang.perl.modules)
  • Re: howto grab key strokes directly?
    ... set nbew state with new call to ioctl(0, TCSETA, &term); ... your loop follows here, reading one char from stdin each time: ... > I figured out to use selectto get noticed if stdin has data to read, ...
    (comp.unix.programmer)
  • Re: Unix script question (basic)
    ... stdin) to stdout. ... A while loop can read it line by line. ... awk '' FILE ...
    (comp.unix.questions)