Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #102241

Re: while loop condition doesn't work

Newsgroups comp.lang.c
Date 2017-01-20 08:37 -0800
References (1 earlier) <871svxdiu9.fsf@bsb.me.uk> <e9b02777-9542-4e5b-bbf7-885c9bbcb5da@googlegroups.com> <87pojhbw2h.fsf@bsb.me.uk> <d4616907-128c-442d-b29e-82bf47b4e83d@googlegroups.com> <o5tcfb$ap3$1@dont-email.me>
Message-ID <2f5efea6-4d06-42bf-a6f9-976e1faf51e4@googlegroups.com> (permalink)
Subject Re: while loop condition doesn't work
From Alla _ <modelling.data@gmail.com>

Show all headers | View raw


On Friday, January 20, 2017 at 7:06:52 PM UTC+3, Richard Heathfield wrote:
> On 20/01/17 15:45, Alla _ wrote:
> > On Friday, January 20, 2017 at 6:17:01 PM UTC+3, Ben Bacarisse wrote:
> >> Alla _ <modelling.data@gmail.com> writes:
> <snip>
> >>>         while(not_converted > ptr && !isspace(*not_converted))
> >>
> >> Why are you interested in non-spaces? [...]
> > With this function I just need two numbers out of 5 - the first and the last
> > one. At the moment the function is called the input
> > contains numbers, separated by commas (csv format), as in
> > 1062,1252,1587,1934,2519
> 
> In which case you have no need to call isspace at all, and the whole 
> thing can be done in less than a dozen lines of very simple code.
> 
> n = 0; /* this is how many numbers you've successfully parsed */
> do
> {
>    /* try to convert a number */
>    num[n] = strtod(ptr, &not_converted, 10);
> 
>    /* if that worked... */
>    if(not_converted > ptr)
>    {
>      /* record the success */
>      ++n;
> 
>      /* if we're just at a field boundary, we
>         want to move to just past that boundary.
>         If we're at the end of the string,
>         then two things happen: (1) we can't be
>         sure that it's legal to move past the '\0'
>         AND (2) there is in any case nothing more
>         to convert.
> 
>         So if we're not at '\0', we want ptr to
>         point to not_converted + 1. Otherwise, we
>         want ptr to point to not_converted (which will
>         terminate the loop).
> 
>         In C, the result of a condition is either 0
>         (false), or 1 (true), and this happens to map
>         precisely onto what we want. So we can do this:
>       */
> 
>      ptr = not_converted + (*not_converted != '\0');
Yet another amazing trick! ) Thank you. I didn't know
about it. Not surprising though )
>    }
> } while(not_converted > ptr && n < expected_count);
> 
> expected_count would appear to be 5 in this case. If it's always 5, use 
> a constant e.g. #define NUMBERS_PER_LINE 5
Yes, I have this in the program. This was a tiny piece I have created
out of few pieces to ask for help. 

Your approach is great for storing all numbers in the array, which 
was not my intent in this piece; but it is great for the program itself,
there I do store numbers in the array of structs. 
> 
> if(n == expected_count) you have all the numbers you need. If it isn't, 
> you don't, and presumably that's an error.
> 
> Note the complete absence of isspace.
> 
> -- 
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 01:24 -0800
  Re: while loop condition doesn't work mark.bluemel@gmail.com - 2017-01-20 03:29 -0800
  Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 12:19 +0000
    Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 05:12 -0800
      Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 13:17 +0000
        Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 05:20 -0800
        Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 15:07 +0000
    Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 05:31 -0800
      Re: while loop condition doesn't work Manfred <noname@invalid.add> - 2017-01-20 15:23 +0100
      Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 15:16 +0000
        Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 07:45 -0800
          Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 16:06 +0000
            Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 08:37 -0800
            Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 17:54 +0100
              Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 17:04 +0000
                Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 19:08 +0100
                Re: while loop condition doesn't work Tim Rentsch <txr@alumni.caltech.edu> - 2017-01-30 12:51 -0800
                Re: while loop condition doesn't work Keith Thompson <kst-u@mib.org> - 2017-01-30 14:09 -0800
                Re: while loop condition doesn't work supercat@casperkitty.com - 2017-01-30 14:57 -0800
                Re: while loop condition doesn't work Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-01 21:31 -0800
                Re: while loop condition doesn't work Keith Thompson <kst-u@mib.org> - 2017-02-02 10:01 -0800
                Re: while loop condition doesn't work Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-04 00:22 -0800
              Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 19:12 +0000
                Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-23 10:54 +0000
            Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 23:22 -0800
              Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-23 11:02 +0000
            Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-21 01:22 -0800
              Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-21 11:00 +0000
                Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-21 08:35 -0800
                Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-21 17:17 +0000
                Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-21 09:32 -0800
          Re: while loop condition doesn't work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-20 19:16 +0000
            Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-23 11:03 +0000
    Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 14:34 +0100
      Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 07:40 -0800
        Re: while loop condition doesn't work Richard Heathfield <rjh@cpax.org.uk> - 2017-01-20 15:47 +0000
          Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 08:26 -0800
            Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 18:01 +0100
              Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 09:26 -0800
                Re: while loop condition doesn't work David Brown <david.brown@hesbynett.no> - 2017-01-20 19:02 +0100
                Re: while loop condition doesn't work Alla _ <modelling.data@gmail.com> - 2017-01-20 10:13 -0800
                Re: while loop condition doesn't work Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-21 05:36 -0800

csiph-web