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


Groups > comp.lang.c > #78230 > unrolled thread

Program to compute the amount of bottles of water...

Started byAlla _ <modelling.data@gmail.com>
First post2015-12-09 05:00 -0800
Last post2015-12-11 11:09 +0100
Articles 20 on this page of 93 — 17 participants

Back to article view | Back to comp.lang.c


Contents

  Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-09 05:00 -0800
    Re: Program to compute the amount of bottles of water... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-09 05:13 -0800
    Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-09 05:17 -0800
    Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-09 13:29 +0000
      Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-09 06:54 -0800
        Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-09 15:45 +0000
          Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-09 08:39 -0800
        Re: Program to compute the amount of bottles of water... Barry Schwarz <schwarzb@dqel.com> - 2015-12-09 08:38 -0800
          Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-09 19:09 +0000
      Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-09 07:07 -0800
        Re: Program to compute the amount of bottles of water... "Osmium" <r124c4u102@comcast.net> - 2015-12-09 09:40 -0600
          Re: Program to compute the amount of bottles of water... Udyant Wig <udyantw@gmail.com> - 2015-12-10 18:12 +0530
            Re: Program to compute the amount of bottles of water... "Osmium" <r124c4u102@comcast.net> - 2015-12-10 07:47 -0600
        Re: Program to compute the amount of bottles of water... Barry Schwarz <schwarzb@dqel.com> - 2015-12-09 08:29 -0800
      Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-14 08:23 -0800
        Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-14 16:55 +0000
          Re: Program to compute the amount of bottles of water... Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-12-14 12:17 -0500
            Re: Program to compute the amount of bottles of water... Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-12-14 13:26 -0500
              Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-14 19:20 +0000
                Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-14 12:54 -0800
                  Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-14 21:01 +0000
                    Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-14 21:36 +0000
                  Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-14 21:26 +0000
                    Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-14 21:40 +0000
                  Re: Program to compute the amount of bottles of water... Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-12-14 16:42 -0500
                    Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-14 21:49 +0000
                      Re: Program to compute the amount of bottles of water... Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-12-14 17:25 -0500
                        Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-14 16:18 -0800
                          Re: Program to compute the amount of bottles of water... Jerry Stuckle <jstucklex@attglobal.net> - 2015-12-14 20:24 -0500
                    Re: Program to compute the amount of bottles of water... "Osmium" <r124c4u102@comcast.net> - 2015-12-14 16:04 -0600
        Re: Program to compute the amount of bottles of water... Barry Schwarz <schwarzb@dqel.com> - 2015-12-14 11:07 -0800
        Re: Program to compute the amount of bottles of water... G G <gdotone@gmail.com> - 2015-12-15 09:51 -0800
    Re: Program to compute the amount of bottles of water... "Osmium" <r124c4u102@comcast.net> - 2015-12-09 08:26 -0600
      Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-09 06:57 -0800
    Re: Program to compute the amount of bottles of water... G G <gdotone@gmail.com> - 2015-12-09 08:06 -0800
      Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-09 08:30 -0800
    Re: Program to compute the amount of bottles of water... G G <gdotone@gmail.com> - 2015-12-09 09:45 -0800
    Re: Program to compute the amount of bottles of water... Vécu BOSSEUR <vecu.bosseur@gmail.com> - 2015-12-10 12:44 +0100
      Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-10 08:38 -0800
      Re: Program to compute the amount of bottles of water... Vécu BOSSEUR <vecu.bosseur@gmail.com> - 2015-12-11 22:02 +0100
        Re: Program to compute the amount of bottles of water... Barry Schwarz <schwarzb@dqel.com> - 2015-12-11 15:23 -0800
          Re: Program to compute the amount of bottles of water... Vécu BOSSEUR <vecu.bosseur@gmail.com> - 2015-12-12 12:05 +0100
            Re: Program to compute the amount of bottles of water... Barry Schwarz <schwarzb@dqel.com> - 2015-12-12 07:00 -0800
            Re: Program to compute the amount of bottles of water... James Kuyper <jameskuyper@verizon.net> - 2015-12-12 10:40 -0500
        Re: Program to compute the amount of bottles of water... BartC <bc@freeuk.com> - 2015-12-12 11:34 +0000
    Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-11 00:43 -0800
      Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-11 09:51 +0000
        Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-11 03:14 -0800
          Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-11 11:55 +0000
            Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-11 10:15 -0800
            Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-13 05:20 -0800
              Re: Program to compute the amount of bottles of water... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-13 05:59 -0800
                Re: Program to compute the amount of bottles of water... Jerry Stuckle <jstucklex@attglobal.net> - 2015-12-13 11:24 -0500
                  Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-13 19:29 +0000
                    Re: Program to compute the amount of bottles of water... Jerry Stuckle <jstucklex@attglobal.net> - 2015-12-13 17:47 -0500
                Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-13 20:43 +0000
              Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-13 17:10 +0000
                Re: Program to compute the amount of bottles of water... James Kuyper <jameskuyper@verizon.net> - 2015-12-13 13:00 -0500
                  Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-13 18:25 +0000
                Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-13 10:04 -0800
                  Re: Program to compute the amount of bottles of water... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-13 10:29 -0800
                    Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-13 10:43 -0800
                      Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-13 19:35 +0000
                        Re: Program to compute the amount of bottles of water... Udyant Wig <udyantw@gmail.com> - 2015-12-14 11:49 +0530
                          Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-14 12:34 +0000
                            Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-14 14:38 +0000
                              Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-14 15:09 +0000
                        Re: Program to compute the amount of bottles of water... Steve Thompson <stevet810@gmail.com> - 2015-12-14 05:24 +0000
                          Re: Program to compute the amount of bottles of water... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-14 13:07 -0800
                Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-13 13:42 -0800
                  Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-13 22:14 +0000
          Re: Program to compute the amount of bottles of water... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-11 09:51 -0800
            Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-11 10:15 -0800
              Re: Program to compute the amount of bottles of water... Steve Thompson <stevet810@gmail.com> - 2015-12-12 20:42 +0000
                Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-12 18:10 -0800
                  Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-13 11:16 +0000
                    Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-13 03:41 -0800
                      Re: Program to compute the amount of bottles of water... Steve Thompson <stevet810@gmail.com> - 2015-12-14 01:43 +0000
                  Re: Program to compute the amount of bottles of water... Steve Thompson <stevet810@gmail.com> - 2015-12-14 01:41 +0000
            Re: Program to compute the amount of bottles of water... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-12-11 18:45 +0000
            Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-11 12:34 -0800
              Re: Program to compute the amount of bottles of water... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-11 13:18 -0800
                Re: Program to compute the amount of bottles of water... raltbos@xs4all.nl (Richard Bos) - 2015-12-15 17:48 +0000
                  Re: Program to compute the amount of bottles of water... Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-15 10:58 -0800
                    Re: Program to compute the amount of bottles of water... raltbos@xs4all.nl (Richard Bos) - 2015-12-17 00:14 +0000
          Re: Program to compute the amount of bottles of water... Barry Schwarz <schwarzb@dqel.com> - 2015-12-11 09:53 -0800
            Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-11 10:20 -0800
            Re: Program to compute the amount of bottles of water... Keith Thompson <kst-u@mib.org> - 2015-12-11 12:40 -0800
      Re: Program to compute the amount of bottles of water... Barry Schwarz <schwarzb@dqel.com> - 2015-12-11 02:01 -0800
        Re: Program to compute the amount of bottles of water... Richard Heathfield <rjh@cpax.org.uk> - 2015-12-11 10:18 +0000
        Re: Program to compute the amount of bottles of water... Alla _ <modelling.data@gmail.com> - 2015-12-11 03:02 -0800
      Re: Program to compute the amount of bottles of water... "Osmium" <r124c4u102@comcast.net> - 2015-12-11 08:13 -0600
    Re: Program to compute the amount of bottles of water... "Skybuck Flying" <skybuck2000@hotmail.com> - 2015-12-11 11:09 +0100

Page 2 of 5 — ← Prev page 1 [2] 3 4 5  Next page →


#78684

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-12-14 21:01 +0000
Message-ID<n4najg$nar$1@dont-email.me>
In reply to#78683
On 14/12/15 20:54, Keith Thompson wrote:
>
> Shouldn't it be
>
[...]
>
>      if (endptr != NULL && *endptr != '\n')
>
> ?


Yes. Not my day, is it?

Reading Lew's correction, I saw that he'd added a check to endptr, but 
clearly I didn't read it carefully enough because I was too busy 
whizzing up to where endptr is set up to check that he was right.

Programming is an iterative process. :-)

-- 
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

[toc] | [prev] | [next] | [standalone]


#78692

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-12-14 21:36 +0000
Message-ID<87r3ioww3v.fsf@bsb.me.uk>
In reply to#78684
Richard Heathfield <rjh@cpax.org.uk> writes:

> On 14/12/15 20:54, Keith Thompson wrote:
>>
>> Shouldn't it be
>>
> [...]
>>
>>      if (endptr != NULL && *endptr != '\n')
>>
>> ?
>
>
> Yes. Not my day, is it?
>
> Reading Lew's correction, I saw that he'd added a check to endptr, but
> clearly I didn't read it carefully enough because I was too busy
> whizzing up to where endptr is set up to check that he was right.

Hmm...  I think Lew meant what he wrote.  His explanation and the test
for *endptr != 0 tie up and are valid.  See my reply to Keith for the
endptr == NULL issue.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#78690

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-12-14 21:26 +0000
Message-ID<8737v4yb5v.fsf@bsb.me.uk>
In reply to#78683
Keith Thompson <kst-u@mib.org> writes:

> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 14/12/15 18:26, Lew Pitcher wrote:
>>
>> <snip>
>>
>>> the test should be
>>>
>>>     if(*endptr && *endptr != '\n')
>>
>> GOOD spot! How did I miss that?!?
>
> That's equivalent to
>
>     if(*endptr != '\0' && *endptr != '\n')
>
> Shouldn't it be
>
>     if(endptr && *endptr != '\n')

I was going to start with an emphatic "no" but whilst Lew write the test
he meant to, there is still a null pointer issue.

Undoing the sniping, the code in question is:

|  char input[INPUT_LENGTH] = "";
|  unsigned long mins = 0;
|  char *endptr = NULL;
| 
|  printf("Please enter the number of minutes you spend in the shower:\n");
|  if(fgets(input, sizeof input, stdin) != NULL)
|  {
|    mins = strtoul(input, &endptr, 10);
|  }
|  if(*endptr != '\n')
|  {
|    mins = 0;
|  }
|  return mins;

Lew's point was that it would be fine for the user to enter a number and
then close the stream with no newline.  In that case, *endptr would not
be \n but the code should not discard the mins value.  Instead, the
error condition is that *endptr is neither a newline nor the null at the
end of the input buffer.  I.e.

  if (*endptr != '\0' && *endptr != '\n')

(however you like to write the zero test) is correct.  Personally, I'd
go further and accept as valid an endptr pointing to any sequence of
space characters, but that's been mentioned already, I think.

But there *is* an endptr == NULL bug that you might be referring to.  If
the fgets fails, strtoul is not called and the *endptr test (any *endptr
test) is invalid.  I think the correct code is therefore

  if (fgets(input, sizeof input, stdin) != NULL) {
      char *endptr;
      mins = strtoul(input, &endptr, 10);
      if (*endptr && *endptr != '\n')
          mins = 0;
  }
  return mins;

(I'm using my style, but that's not the point here.)

On a side issue that will probably now get lost, it's often wrong to use
strtoul just because the correct input is always positive.  strtoul's
purpose is simply to return a value of the right *type*, but the strings
it accepts can be signed.  It is almost always better to use strtol and
report to the user that "-3 is not a valid time in the shower" that to
accept (or report as erroneous) a 18446744073709551613 hour shower!

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#78694

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-12-14 21:40 +0000
Message-ID<n4ncsl$14m$1@dont-email.me>
In reply to#78690
On 14/12/15 21:26, Ben Bacarisse wrote:

<snipe>

> I was going to start with an emphatic "no" but whilst Lew write the test
> he meant to, there is still a null pointer issue.
>
> Undoing the sniping,

This threw me. As far as I could tell, the exchange was good-natured 
throughout, and I could see no evidence of sniping. And *then* I worked 
out that you'd simply droped a p. :-)

<snipe>

> But there *is* an endptr == NULL bug that you might be referring to.

I think that this is what Keith meant, yes.

<snipe>

> It is almost always better to use strtol and
> report to the user that "-3 is not a valid time in the shower" that to
> accept (or report as erroneous) a 18446744073709551613 hour shower!

Especially as, after a mere 8,760,000 hours, the water starts running cold.

-- 
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

[toc] | [prev] | [next] | [standalone]


#78695

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2015-12-14 16:42 -0500
Message-ID<uJGby.59772$FF1.33356@fx39.iad>
In reply to#78683
On Monday December 14 2015 15:54, in comp.lang.c, "Keith Thompson"
<kst-u@mib.org> wrote:

> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 14/12/15 18:26, Lew Pitcher wrote:
>>
>> <snip>
>>
>>> the test should be
>>>
>>>     if(*endptr && *endptr != '\n')
>>
>> GOOD spot! How did I miss that?!?
> 
> That's equivalent to
> 
>     if(*endptr != '\0' && *endptr != '\n')
> 
> Shouldn't it be
> 
>     if(endptr && *endptr != '\n')
> 
> or, as I'd prefer to write it:
> 
>     if (endptr != NULL && *endptr != '\n')
> 
> ?

Good catch as well.

I give you...

  if (endptr && *endptr && *endptr != '\n')

1) tests if endptr is not NULL, as can happen
   if the fgets() call returns NULL. This can happen
   when "end-of-file is incountered and no characters have been read into the
   array", or when "a read error occurs during the operation".

2) tests if *endptr is not '\0' (the string termination character),
   as can happen when strtoul interprets a correctly formatted numeric value
   terminated with end-of-string.This can happen if the fgets() that populated
   the buffer read input characters terminated not by a newline, but by
   end-of-file.

3) tests if *endptr is not '\n' (the newline character), as can happen when
   strtoul interprets an incorrectly formatted value

-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#78699

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-12-14 21:49 +0000
Message-ID<87a8pcwvis.fsf@bsb.me.uk>
In reply to#78695
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

> On Monday December 14 2015 15:54, in comp.lang.c, "Keith Thompson"
> <kst-u@mib.org> wrote:
>
>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>> On 14/12/15 18:26, Lew Pitcher wrote:
>>>
>>> <snip>
>>>
>>>> the test should be
>>>>
>>>>     if(*endptr && *endptr != '\n')
>>>
>>> GOOD spot! How did I miss that?!?
>> 
>> That's equivalent to
>> 
>>     if(*endptr != '\0' && *endptr != '\n')
>> 
>> Shouldn't it be
>> 
>>     if(endptr && *endptr != '\n')
>> 
>> or, as I'd prefer to write it:
>> 
>>     if (endptr != NULL && *endptr != '\n')
>> 
>> ?
>
> Good catch as well.
>
> I give you...
>
>   if (endptr && *endptr && *endptr != '\n')

Right, but that basically treats endptr as a flag variable, signalling
if strtoul has been called.  I'd still put the simpler (two-part) test
into the first if (and I'd put the declaration of endptr there too).

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#78706

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2015-12-14 17:25 -0500
Message-ID<JlHby.54386$051.27175@fx28.iad>
In reply to#78699
On Monday December 14 2015 16:49, in comp.lang.c, "Ben Bacarisse"
<ben.usenet@bsb.me.uk> wrote:

> Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
> 
>> On Monday December 14 2015 15:54, in comp.lang.c, "Keith Thompson"
>> <kst-u@mib.org> wrote:
>>
>>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>>> On 14/12/15 18:26, Lew Pitcher wrote:
>>>>
>>>> <snip>
>>>>
>>>>> the test should be
>>>>>
>>>>>     if(*endptr && *endptr != '\n')
>>>>
>>>> GOOD spot! How did I miss that?!?
>>> 
>>> That's equivalent to
>>> 
>>>     if(*endptr != '\0' && *endptr != '\n')
>>> 
>>> Shouldn't it be
>>> 
>>>     if(endptr && *endptr != '\n')
>>> 
>>> or, as I'd prefer to write it:
>>> 
>>>     if (endptr != NULL && *endptr != '\n')
>>> 
>>> ?
>>
>> Good catch as well.
>>
>> I give you...
>>
>>   if (endptr && *endptr && *endptr != '\n')
> 
> Right, but that basically treats endptr as a flag variable, signalling
> if strtoul has been called.  I'd still put the simpler (two-part) test
> into the first if (and I'd put the declaration of endptr there too).

Aren't code reviews wonderful? Makes me miss the old days, when defending my
code (and acknowledging it's deficiencies) made me a better programmer.


-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#78716

FromKeith Thompson <kst-u@mib.org>
Date2015-12-14 16:18 -0800
Message-ID<lnegeod0oc.fsf@kst-u.example.com>
In reply to#78706
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
[...]
> Aren't code reviews wonderful? Makes me miss the old days, when defending my
> code (and acknowledging it's deficiencies) made me a better programmer.

its

(Sorry, couldn't resist.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#78725

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-12-14 20:24 -0500
Message-ID<n4npvq$j3r$1@dont-email.me>
In reply to#78716
On 12/14/2015 7:18 PM, Keith Thompson wrote:
> Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
> [...]
>> Aren't code reviews wonderful? Makes me miss the old days, when defending my
>> code (and acknowledging it's deficiencies) made me a better programmer.
> 
> its
> 
> (Sorry, couldn't resist.)
> 

It's something my wife (an ex-English teacher) is on me about all the
time :)

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#78702

From"Osmium" <r124c4u102@comcast.net>
Date2015-12-14 16:04 -0600
Message-ID<dd8so3Fa5ggU1@mid.individual.net>
In reply to#78695
"Lew Pitcher" wrote:

> On Monday December 14 2015 15:54, in comp.lang.c, "Keith Thompson"
> <kst-u@mib.org> wrote:
>
>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>> On 14/12/15 18:26, Lew Pitcher wrote:
>>>
>>> <snip>
>>>
>>>> the test should be
>>>>
>>>>     if(*endptr && *endptr != '\n')
>>>
>>> GOOD spot! How did I miss that?!?
>>
>> That's equivalent to
>>
>>     if(*endptr != '\0' && *endptr != '\n')
>>
>> Shouldn't it be
>>
>>     if(endptr && *endptr != '\n')
>>
>> or, as I'd prefer to write it:
>>
>>     if (endptr != NULL && *endptr != '\n')
>>
>> ?
>
> Good catch as well.
>
> I give you...
>
>  if (endptr && *endptr && *endptr != '\n')
>
> 1) tests if endptr is not NULL, as can happen
>   if the fgets() call returns NULL. This can happen
>   when "end-of-file is incountered and no characters have been read into 
> the
>   array", or when "a read error occurs during the operation".
>
> 2) tests if *endptr is not '\0' (the string termination character),
>   as can happen when strtoul interprets a correctly formatted numeric 
> value
>   terminated with end-of-string.This can happen if the fgets() that 
> populated
>   the buffer read input characters terminated not by a newline, but by
>   end-of-file.
>
> 3) tests if *endptr is not '\n' (the newline character), as can happen 
> when
>   strtoul interprets an incorrectly formatted value

I suggest this thread be re-titled as "How many angels can dance on the head 
of a pin"

https://en.wikipedia.org/wiki/How_many_angels_can_dance_on_the_head_of_a_pin%3F 

[toc] | [prev] | [next] | [standalone]


#78671

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-12-14 11:07 -0800
Message-ID<2n2u6blgue64qtlecr3fcovigahr5bpdv6@4ax.com>
In reply to#78650
On Mon, 14 Dec 2015 08:23:00 -0800 (PST), Alla _
<modelling.data@gmail.com> wrote:


<snip>

>The remainder of the string is converted to an unsigned long int value in the 
>obvious manner, stopping at the first character which is not a valid digit in
> the given base. (In bases above 10, the letter A in either upper or lower case
> represents 10, B represents 11, and so forth, with Z representing 35.) End of 
>quote

Why did you stop quoting there?  The next paragraph in the reference
shows exactly what you need to address your concern about rejecting
123abc.

>The function should "stop at the first character which is not a valid digit in
>the given base". In this program the base is 10, therefore if there is an input 
>123abc the function stops at the fist non-digit character, which is 'a', and 
>computes the amount of bottles 123 * 12.
>
>Below is the result of the test. All good except for the last one:
>:) water.c exists
>:) water.c compiles
>:) 1 minute equals 12 bottles
>:) 2 minutes equals 24 bottles
>:) 5 minutes equals 60 bottles
>:) 10 minutes equals 120 bottles
>:) rejects "foo" minutes
>:) rejects "" minutes
>:( rejects "123abc" minutes

In what way does this code reject 123abc?  As you note below, it
happily gives you 1476 which is 123*12.

>   \ expected prompt for input, not exit code of 0 
>
>When I run the program with 123abc input, I don't get exit code 0;

Yes you do.  The phrase exit code refers to the return code from main.
What you did not get was a return code of 0 from get_minutes (like you
did for "foo") but rather the value of 123 which exactly matches what
the description of strtoul says you should get.

>instead I get the result of 1476 bottles, but the program should
>reject such inputs and prompt the user for a correct one. 

While that may be the desired behavior of the program, it is not the
documented behavior of strtoul or get_minutes.

>Shall I construct a condition that could test if there is any character
>in the input that is not a valid digit, and if true, prompt for another
>input? Given there is fgets and strtoul functions, this additional prompt
>might be redundant. What do you think?

I think you are mixing apples and oranges.  fgets has nothing to do
with whether the input is a valid number.  strtoul will tell you if
there is invalid input but will never ask you to re-enter the input.
Checking whether the input is satisfactory for a particular purpose is
always the job of the calling function.  (For example, in this case
would you want to reject input that had a trailing blank after the
number?)


-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#78762

FromG G <gdotone@gmail.com>
Date2015-12-15 09:51 -0800
Message-ID<253e3d77-27e9-4110-b955-f9fd0c25896b@googlegroups.com>
In reply to#78650
On Monday, December 14, 2015 at 11:23:21 AM UTC-5, Alla _ wrote:
> > 
> > #include <stdio.h>
> > #include <stdlib.h>
> > 
> > #define BOTTLES_PER_MINUTE 12
> > #define INPUT_LENGTH 32
> > 
> > unsigned long get_minutes(void)
> > {
> >    char input[INPUT_LENGTH] = "";
> >    unsigned long mins = 0;
> > 
> >    printf("Please enter the numb"
> >           "er of minutes you spe"
> >           "nd in the shower:\n");
> >    if(fgets(input, sizeof input, stdin) != NULL)
> >    {
> >      mins = strtoul(input, NULL, 10);
> >    }
> >    return mins;
> > }
> > 
> > int main(void)
> > {
> >    unsigned long bottles = 0;
> >    unsigned long minutes = 0;
> >    while((minutes = get_minutes()) == 0)
> >    {
> >      printf("Sorry, I didn't un"
> >             "derstand that.\nPl"
> >             "ease try again.\n");
> >    }
> > 
> >    bottles = minutes * BOTTLES_PER_MINUTE;
> > 
> >    printf("Equivalent to %lu"
> >           " bottles of water.\n",
> >           bottles);
> >    return 0;
> > }
> > 
-----------------------------------------------------
Alla_

Forgive me, today I'm going to be a little lazy, what
happens if the user for some unknown reason enter 
negative number.
-10
I see you define, unsigned longs to handle the integer
so does the program just crash.
But, you looking great, you are working through it.

After writing the p-code a few times I may have done 
something like

prompt the user
read the input into an array
check the array for characters other than digits
if digits
   build number ( just as an exercise )
if not ...

calculate bottles

print message with number of bottles.
----------------------------------------------------------
in coding this would have used a while loop
I think to control.
just rough cut thoughts here.

I glad to see you coded it without break or continue.

[toc] | [prev] | [next] | [standalone]


#78239

From"Osmium" <r124c4u102@comcast.net>
Date2015-12-09 08:26 -0600
Message-ID<dcqs06Fom4mU1@mid.individual.net>
In reply to#78230
"Alla _" wrote:

> Please, take a look at a small program. The goal of the program is
> to get the number of minutes that a user supposedly spends in the shower,
> and compute the amount of bottles of water equivalent to the amount of 
> water
> used by the user within that period of time. This is based on a premise
> that taking a 1-minute shower is akin to using 12 bottles of water.
>
> My program correctly computes the amount of bottles if I enter up to 10 
> minutes
> (that's enough for this exercise; I even do some extra checks which are 
> not
> required), but the program doesn't do the following:
>
> - if the user enters something like "foo", or "123abc", or space, the 
> program
> should re-prompt for another input; but at the moment the program returns
> 0 if there is an incorrect input.
>
> To re-prompt the user I use continue statement in the inner if statement
> in the get_minutes function. Why this doesn't work? Please, don't give the
> full solution, but a useful hint.
>
> #include <stdio.h>
> #include <ctype.h>
>
> #define MAX 5000
> int minutes[MAX];
>
> void get_minutes(int minutes[]);
> int char_to_digit(int minutes[]);
>
> int main(void)
> {
>    printf("Please, enter the number of minutes\n");
>    get_minutes(minutes);
>
>    printf("%d\n", (char_to_digit(minutes) * 12));
>
>    return 0;
> }
>
> void get_minutes(int minutes[])
> {
>    int c, i;
>
>    for (i = 0; !isspace(c = getchar()); i++)
>    {
>        if (!isdigit(c))
>        {
>            continue;
>        }
>        else
>        {
>            minutes[i] = c;
>        }
>    }
>
>    minutes[i] = '\0';
> }
>
> int char_to_digit(int minutes[])
> {
>    int i = 0;
>    int n, sign;
>
>    /* test for sign is not required; but I use it */
>
>    sign = (minutes[i] == '-')? -1: 1;
>    if (minutes[i] == '-' || minutes[i] == '+')
>    {
>        i++;
>    }
>
>    for (n = 0; minutes[i] != '\0'; i++)
>    {
>        n = 10 * n + (minutes[i] - '0');
>    }
>
>    return sign * n;
> }

You decide to get a number, but instead, you get a character.  Does it not 
seem reasonable to you that a programming language that has been around 
since the 1980s might already have a function available that reads a number? 
After all, numbers and computers have a natural alliance.You could start by 
looking at the often criticized scanf() and its cousins.  Note that the 
designers did not go to the effort of providing  a return value so you could 
simply throw it away. 

[toc] | [prev] | [next] | [standalone]


#78243

FromAlla _ <modelling.data@gmail.com>
Date2015-12-09 06:57 -0800
Message-ID<e39cd3bd-bdde-416c-a5ad-3ea4fae5d2e2@googlegroups.com>
In reply to#78239
On Wednesday, December 9, 2015 at 6:26:31 PM UTC+4, Osmium wrote:
> "Alla _" wrote:
> 
> > Please, take a look at a small program. The goal of the program is
> > to get the number of minutes that a user supposedly spends in the shower,
> > and compute the amount of bottles of water equivalent to the amount of 
> > water
> > used by the user within that period of time. This is based on a premise
> > that taking a 1-minute shower is akin to using 12 bottles of water.
> >
> > My program correctly computes the amount of bottles if I enter up to 10 
> > minutes
> > (that's enough for this exercise; I even do some extra checks which are 
> > not
> > required), but the program doesn't do the following:
> >
> > - if the user enters something like "foo", or "123abc", or space, the 
> > program
> > should re-prompt for another input; but at the moment the program returns
> > 0 if there is an incorrect input.
> >
> > To re-prompt the user I use continue statement in the inner if statement
> > in the get_minutes function. Why this doesn't work? Please, don't give the
> > full solution, but a useful hint.
> >
> > #include <stdio.h>
> > #include <ctype.h>
> >
> > #define MAX 5000
> > int minutes[MAX];
> >
> > void get_minutes(int minutes[]);
> > int char_to_digit(int minutes[]);
> >
> > int main(void)
> > {
> >    printf("Please, enter the number of minutes\n");
> >    get_minutes(minutes);
> >
> >    printf("%d\n", (char_to_digit(minutes) * 12));
> >
> >    return 0;
> > }
> >
> > void get_minutes(int minutes[])
> > {
> >    int c, i;
> >
> >    for (i = 0; !isspace(c = getchar()); i++)
> >    {
> >        if (!isdigit(c))
> >        {
> >            continue;
> >        }
> >        else
> >        {
> >            minutes[i] = c;
> >        }
> >    }
> >
> >    minutes[i] = '\0';
> > }
> >
> > int char_to_digit(int minutes[])
> > {
> >    int i = 0;
> >    int n, sign;
> >
> >    /* test for sign is not required; but I use it */
> >
> >    sign = (minutes[i] == '-')? -1: 1;
> >    if (minutes[i] == '-' || minutes[i] == '+')
> >    {
> >        i++;
> >    }
> >
> >    for (n = 0; minutes[i] != '\0'; i++)
> >    {
> >        n = 10 * n + (minutes[i] - '0');
> >    }
> >
> >    return sign * n;
> > }
> 
> You decide to get a number, but instead, you get a character.  Does it not 
> seem reasonable to you that a programming language that has been around 
> since the 1980s might already have a function available that reads a number? 
> After all, numbers and computers have a natural alliance.You could start by 
> looking at the often criticized scanf() and its cousins.  Note that the 
> designers did not go to the effort of providing  a return value so you could 
> simply throw it away.

True, and I know scanf, and have been using it a lot. But now I am studying 
K&R approach and style, and I use simple methods presented within first chapter.
Those are suffice for solving exercises like this one. I believe it is also
useful at this stage to create "my own" (none of them really are my own, of
course) functions.

even if C has ready installed ones. 

[toc] | [prev] | [next] | [standalone]


#78251

FromG G <gdotone@gmail.com>
Date2015-12-09 08:06 -0800
Message-ID<7ab76d7e-9cf2-4680-9e39-9ee09906e271@googlegroups.com>
In reply to#78230
On Wednesday, December 9, 2015 at 8:00:53 AM UTC-5, Alla _ wrote:

> Hello!
...
> > To re-prompt the user I use continue statement in the inner if statement
> in the get_minutes function. Why this doesn't work? Please, don't give the 
> full solution, but a useful hint.
> 

Try it without the continue statement.
I mean write your code so you don't use a continue statement.

Alla you are well on your way to becoming good. 
Keep up the hard work. And keep using pencil and 
paper first to walk through the possible data entries
and figure out what should happen.

[toc] | [prev] | [next] | [standalone]


#78256

FromAlla _ <modelling.data@gmail.com>
Date2015-12-09 08:30 -0800
Message-ID<c862eb25-0c1a-4f93-8ed6-e8d897f64157@googlegroups.com>
In reply to#78251
On Wednesday, December 9, 2015 at 8:06:16 PM UTC+4, G G wrote:
> On Wednesday, December 9, 2015 at 8:00:53 AM UTC-5, Alla _ wrote:
> 
> > Hello!
> ...
> > > To re-prompt the user I use continue statement in the inner if statement
> > in the get_minutes function. Why this doesn't work? Please, don't give the 
> > full solution, but a useful hint.
> > 
> 
> Try it without the continue statement.
> I mean write your code so you don't use a continue statement.
> 
> Alla you are well on your way to becoming good. 
> Keep up the hard work. And keep using pencil and 
> paper first to walk through the possible data entries
> and figure out what should happen.

Thank you very much for these nice words. My hope is still with me )

[toc] | [prev] | [next] | [standalone]


#78263

FromG G <gdotone@gmail.com>
Date2015-12-09 09:45 -0800
Message-ID<ddb9fc9b-3ad4-49ba-aa72-6005e98c58e8@googlegroups.com>
In reply to#78230
Well how about not building the number, let's say 
the user enters data and the data should be an 
integer, is there a library function that may accept or 
reject the input if it is not an integer.

123abc ... so you would say that's unacceptable right?
123       ... would be ok
abc123 ... so you would say that's unacceptable right?

1  2  3 .... what would that be 123 or would you say the
                user should not have entered spaces in be-
                tween the numbers. or would it just take the 
                first integer and do the computation.

Pseudo code .... first try....

Prompt user to enter a number
Read in number
Calculate bottles

Pseudo code .... second try ....

Prompt use to enter an integer

Read in number (data)
   if data is not an integer
      Prompt user that data is not an integer
      Prompt user to reenter data an integer
      Read in number (data)

Calculate bottles (you could do this as a 
function too)

Pseudo code .... try four ....
   .... 
   ....


Alla_ I posted p-code cause someone 
ask me how would I look at the problem
and create p-code on my way to solving it. 
I posted here cause some many others 
know much more than me and can add to
or take away from, and correct where
it needs to be corrected.




   

[toc] | [prev] | [next] | [standalone]


#78333

FromVécu BOSSEUR <vecu.bosseur@gmail.com>
Date2015-12-10 12:44 +0100
Message-ID<d044f$566965b0$51fd309e$16017@no-peering.z51.biz>
In reply to#78230
Le Wed, 09 Dec 2015 05:00:24 -0800, Alla _ a écrit :

> Hello!
...
> - if the user enters something like "foo", or "123abc", or space, the
> program should re-prompt for another input; but at the moment the
> program returns 0 if there is an incorrect input.
> 
> To re-prompt the user I use continue statement in the inner if statement
> in the get_minutes function. Why this doesn't work? Please, don't give
> the full solution, but a useful hint.
...
> Thank you!

Hi !

Here follows my implementation of converting an integer representation 
("0", "-3", "4", "+7", ...) to an integer variable , in hope this helps :

Program "atoi_enhanced.c" :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define N_CHARACTERS 20 // [ -9223372036854775808 .. +9223372036854775807 
] , where + sign is optional

int atoi2(int* err /* not NULL */, const char* repr /* not NULL */)
{
    char buffer[N_CHARACTERS + 2];
    buffer[N_CHARACTERS + 1] = '\0';
    if (strlen(repr) > 0)
        if (*repr == '+')
            repr++;
    int value = atoi(repr);
    size_t sz = snprintf(buffer, N_CHARACTERS + 1, "%d", value);
    if ((sz >= N_CHARACTERS + 1) || (sz < 0))
    {    
        *err = 1;
        return -1;
    }
    if (0 != strcmp(buffer, repr))
    {    
        *err = 1;
        return -1;
    }
    *err = 0;
    return value;
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "ERROR: Usage Error.\nUSAGE: %s <Integer>\n", argv
[0]);
        return 1;
    }

    int value, err;
    value = atoi2(&err, argv[1]);

    if (err)
    {
        fprintf(stderr, "ERROR: Input \"%s\" was not an Integer\n", argv
[1]);
        return 1;
    }

    fprintf(stderr, "INFO: Parsed input as Integer %d\n", value);

    if (value < 0)
    {
        fprintf(stderr, "ERROR: Integer %d not fit for this application 
because %d < 0\n", value, value);
        return 1;
    }

    return 0;
}

[toc] | [prev] | [next] | [standalone]


#78345

FromKeith Thompson <kst-u@mib.org>
Date2015-12-10 08:38 -0800
Message-ID<lnh9jqjm1p.fsf@kst-u.example.com>
In reply to#78333
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
[...]
>>isdigit(( unsigned char )ch )
>
>   todo: it might be more safe just to replace the standard
>   is-functions by custom static inline functions.

Why would that be safer?  Are you concerned that an implementation of
isdigit() might be buggy?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#78462

FromVécu BOSSEUR <vecu.bosseur@gmail.com>
Date2015-12-11 22:02 +0100
Message-ID<a0f5d$566b39d2$51fd1d6b$4226@no-peering.z51.biz>
In reply to#78333
Le Thu, 10 Dec 2015 12:44:48 +0100, Vécu BOSSEUR a écrit :

> Le Wed, 09 Dec 2015 05:00:24 -0800, Alla _ a écrit :
> 
>> Hello!
> ...
>> - if the user enters something like "foo", or "123abc", or space, the
>> program should re-prompt for another input; but at the moment the
>> program returns 0 if there is an incorrect input.
>> 
>> To re-prompt the user I use continue statement in the inner if
>> statement in the get_minutes function. Why this doesn't work? Please,
>> don't give the full solution, but a useful hint.
> ...
>> Thank you!
> 
> Hi !
> 
> Here follows my implementation of converting an integer representation
> ("0", "-3", "4", "+7", ...) to an integer variable , in hope this helps
> :
> 
> Program "atoi_enhanced.c" :
> 
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> 
> #define N_CHARACTERS 20 // [ -9223372036854775808 ..
> +9223372036854775807 ] , where + sign is optional
> 
> int atoi2(int* err /* not NULL */, const char* repr /* not NULL */)
> {
>     char buffer[N_CHARACTERS + 2];
>     buffer[N_CHARACTERS + 1] = '\0';
>     if (strlen(repr) > 0)
>         if (*repr == '+')
>             repr++;
>     int value = atoi(repr);
>     size_t sz = snprintf(buffer, N_CHARACTERS + 1, "%d", value); if ((sz
>     >= N_CHARACTERS + 1) || (sz < 0))
>     {
>         *err = 1;
>         return -1;
>     }
>     if (0 != strcmp(buffer, repr))
>     {
>         *err = 1;
>         return -1;
>     }
>     *err = 0;
>     return value;
> }
> 
> int main(int argc, char* argv[])
> {
>     if (argc != 2)
>     {
>         fprintf(stderr, "ERROR: Usage Error.\nUSAGE: %s <Integer>\n",
>         argv
> [0]);
>         return 1;
>     }
> 
>     int value, err;
>     value = atoi2(&err, argv[1]);
> 
>     if (err)
>     {
>         fprintf(stderr, "ERROR: Input \"%s\" was not an Integer\n", argv
> [1]);
>         return 1;
>     }
> 
>     fprintf(stderr, "INFO: Parsed input as Integer %d\n", value);
> 
>     if (value < 0)
>     {
>         fprintf(stderr, "ERROR: Integer %d not fit for this application
> because %d < 0\n", value, value);
>         return 1;
>     }
> 
>     return 0;
> }

Hello everybody,

Besides Stefan_Ram's post to my own reply, would someone comment on my 
own implementation ?

I'd like to thank Stefan, however.

Regards,
Valentin.

[toc] | [prev] | [next] | [standalone]


Page 2 of 5 — ← Prev page 1 [2] 3 4 5  Next page →

Back to top | Article view | comp.lang.c


csiph-web