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


Groups > comp.lang.c > #102792

Re: Recursion: getting input from the user

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Recursion: getting input from the user
Date 2017-01-30 20:16 +0000
Organization A noiseless patient Spider
Message-ID <87mve8nw1r.fsf@bsb.me.uk> (permalink)
References <a1baac5a-a7a9-41e6-baaf-5190d075c3ab@googlegroups.com> <1tvu8chco0lpmavsea22e23le3p9s63087@4ax.com>

Show all headers | View raw


Barry Schwarz <schwarzb@dqel.com> writes:

> On Mon, 30 Jan 2017 02:06:42 -0800 (PST), Alla _
> <modelling.data@gmail.com> wrote:
>
>>Hello!
>>I am trying to write a function that gets an input from the user.
>>If the user has not provided a correct input, then the function
>>should start all over again, prompting the user for another input.
>>I wonder why it doesn't perform the recursion. If I enter only new
>>line and then press ctrl D to indicate EOF, I get back the printf
>>message for the second time and the program stops.
>>When I enter a number, it correctly stores the number. 
>>
>>Here is how the function is called:
>>av.industry_rate = industry_rate();
>>
>>The function:
>>double industry_rate()
>>{
>
> <snip>
>
>>    return industry_rate();
>>}
>
> Is no one going to mention that using recursion for this purpose
> should be on the list of 10 worst ideas ever.

I'm not because I think it shows imagination.  It may not be one of the
10 *best* ideas ever, but it's very far from being in the worst 10!

> A while or do...while
> loop that iterates until the input has been validated will be so much
> easier to debug

Yes.  A loop is almost always better in this situation.

> not to mention more efficient and less likely to
> overflow the stack.

Given that it's for interactive input, the efficiency is of a call vs a
loop is going to be of no consequence, and overflowing the stack is
unlikely to be a problem.

-- 
Ben.

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


Thread

Recursion: getting input from the user Alla _ <modelling.data@gmail.com> - 2017-01-30 02:06 -0800
  Re: Recursion: getting input from the user Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-30 12:11 +0000
    Re: Recursion: getting input from the user Alla _ <modelling.data@gmail.com> - 2017-01-30 04:46 -0800
      Re: Recursion: getting input from the user David Brown <david.brown@hesbynett.no> - 2017-01-30 14:35 +0100
        Re: Recursion: getting input from the user Alla _ <modelling.data@gmail.com> - 2017-01-30 05:55 -0800
      Re: Recursion: getting input from the user Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-30 17:16 +0000
  Re: Recursion: getting input from the user Barry Schwarz <schwarzb@dqel.com> - 2017-01-30 10:07 -0800
    Re: Recursion: getting input from the user Alla _ <modelling.data@gmail.com> - 2017-01-30 11:13 -0800
    Re: Recursion: getting input from the user "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-30 15:07 -0500
    Re: Recursion: getting input from the user Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-30 20:16 +0000
    Re: Recursion: getting input from the user Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-02 01:47 -0800
    Re: Recursion: getting input from the user supercat@casperkitty.com - 2017-02-02 12:38 -0800

csiph-web