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


Groups > comp.lang.c > #68381

Re: Initializing sum to zero wouldn't compile

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Initializing sum to zero wouldn't compile
Date 2015-08-26 11:24 +0100
Organization A noiseless patient Spider
Message-ID <87d1ya9wkd.fsf@bsb.me.uk> (permalink)
References (1 earlier) <f31a40a6-3db3-4cf5-a251-73779f5b9b9b@googlegroups.com> <87r3mv7bys.fsf@bsb.me.uk> <c0acb3a0-aa9d-43aa-b4ae-c00e7622e43a@googlegroups.com> <87y4h359i3.fsf@bsb.me.uk> <5e85b7d7-e360-4224-acbf-bf9d05b12ca2@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:

>> >     for ( i = 0; i < 3; i++)
>> >     {
>> >         scanf ("%f", &element);
<snip>
>> >     }
<snip>

> Thank you for your questions, they are very helpful.
> Actually, I hoped that I am testing scanf by using 
> if (n - element == 0 || element == 0 ). Now I see 
> that I don't understand what "to test scanf" means. 
> I would be grateful if you explain this.

I mean you should check that some valid input was read.  If the person
using your program enters "twelve" instead of "12" scanf will fail.  The
variable "element" will have *some* value (it will either be what's
called "indeterminate" or it will be the value from the last time round
the loop) but in nether case should you report and average using it.

So, what to do?  scanf tells you, by the value returned, the number of
items read, so you can write

  if (scanf("%lf", &element) == 1) {
      /* do something with the data */
  }

The other side is what to do when there is an input problem.
Unfortunately, your example is rather artificial so it's hard to know
what the right thing to do it.  It's ironic, but a more complex program
will have a simpler way to handle this error.  For example

  int n = 0;
  double sum = 0;
  while (scanf("%lf", &element) == 1) {
      n += 1;
      sum += element;
  }
  if (n > 0)
      printf("The average of your %n numbers is %g\n", n, sum/n);

> As to numbers like 1.0, these are indeed float values,
> but I made the program test for these ones and discard 
> them as invalid, because if the user enters, for example, 1 or 2,
> the program will consider them as float values, i.e. 1.0, if 
> I understand that correctly.

I'm still baffled by why you want to exclude input like 1, 1.0, 1e1, 0,
0.0, 0e100 and so on.  They are all perfectly reasonable numbers.  Why
can't I enter 1 2 3 and get the answer 2?  It's perplexing.

> "As to your question, what is the value of i after 
> the loop if it ran to completion?  What value does it have 
> if the loop was terminated with a break?"  The value of i 
> upon completion is 2 (after user enters 3 values from i=0 to i=2).

No.  Check again.

> When the loop is terminated with break statement, the value of i can
> be anything from 0 to 2, depending when the user enters the "invalid"
> value.

Yes.  That distinction is important because once you get rid of the
funny "is_float" variable, and start checking the result of scanf, it
would offer a simple way to tell if you got all three numbers.

-- 
Ben.

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


Thread

Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 04:39 -0700
  Re: Initializing sum to zero wouldn't compile James Kuyper <jameskuyper@verizon.net> - 2015-08-21 07:54 -0400
    Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 05:27 -0700
      Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-21 10:28 -0700
        Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:15 -0700
    Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 05:30 -0700
  Re: Initializing sum to zero wouldn't compile Ike Naar <ike@iceland.freeshell.org> - 2015-08-21 12:40 +0000
    Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:16 -0700
  Re: Initializing sum to zero wouldn't compile Siri Cruz <chine.bleu@yahoo.com> - 2015-08-21 07:41 -0700
    Re: Initializing sum to zero wouldn't compile Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-08-21 10:56 -0400
    Re: Initializing sum to zero wouldn't compile Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-21 09:28 -0600
  Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-21 10:02 -0700
    Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-21 11:21 -0700
      Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-21 22:27 -0700
        Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 02:25 -0700
  Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 03:50 -0700
    Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-22 13:18 +0100
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 11:21 -0700
        Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-22 21:55 +0100
          Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 01:36 -0700
            Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 11:24 +0100
        Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-22 17:17 -0700
          Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 01:26 -0700
            Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-26 02:27 -0700
              Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-26 04:21 -0700
                Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 12:37 +0100
                Re: Initializing sum to zero wouldn't compile Richard Heathfield <rjh@cpax.org.uk> - 2015-08-26 13:06 +0100
                Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-26 13:43 +0100
                Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-26 14:03 +0100
                Re: Initializing sum to zero wouldn't compile Richard Heathfield <rjh@cpax.org.uk> - 2015-08-26 14:06 +0100
                Re: Initializing sum to zero wouldn't compile Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 15:14 -0700
                Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-26 11:38 -0700
                Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-26 11:44 -0700
    Re: Initializing sum to zero wouldn't compile Barry Schwarz <schwarzb@dqel.com> - 2015-08-22 10:49 -0700
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-22 11:16 -0700
  Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-29 07:19 -0700
    Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-29 15:35 +0100
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-30 02:11 -0700
        Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 12:28 +0100
          Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 15:02 -0700
            Re: Initializing sum to zero wouldn't compile Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 22:36 +0100
              Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 16:10 -0700
                Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-31 00:14 +0100
                Re: Initializing sum to zero wouldn't compile Keith Thompson <kst-u@mib.org> - 2015-08-30 19:56 -0700
    Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-29 16:57 +0100
      Re: Initializing sum to zero wouldn't compile Bartc <bc@freeuk.com> - 2015-08-29 17:37 +0100
      Re: Initializing sum to zero wouldn't compile Alla _ <modelling.data@gmail.com> - 2015-08-30 02:14 -0700

csiph-web