Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #102764
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Recursion: getting input from the user |
| Date | 2017-01-30 17:16 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <874m0gpixb.fsf@bsb.me.uk> (permalink) |
| References | <a1baac5a-a7a9-41e6-baaf-5190d075c3ab@googlegroups.com> <87efzkpx2f.fsf@bsb.me.uk> <7d912eeb-0bd5-41ea-84fb-eb9364a9c847@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
<snip>
> Solved by rewriting like this:
> double industry_rate()
> {
> printf("Please, enter the projected industry growth rate beyond 5 year horizon in decimal notation; max 3 numbers after decimal point\n");
> char buf[7] = {0};
> char c;
int c; (Do you remember why?)
> int i = 0;
>
> while( (c = getchar()) != EOF && (c != '\n' && c != '\t'))
> buf[i++] = c;
You can overrun the buffer. Why do you check for a tab?
> if (c == '\n')
> buf[i] = '\0';
This statement serves no purpose.
> if(buf[0])
> {
> char *not_converted = NULL;
> double rate = strtod(buf, ¬_converted);
>
> if(*not_converted == '\n' || *not_converted == '\t'
> || *not_converted == '\0')
There can't be a \n or a \t in the buffer, and if the number entered was
followed by a space *not_converted won't be == 0. Do you really want to
reject that input?
> return rate;
> }
> return industry_rate();
> }
> What indentation will be a correct one? Is the one above
> better?
I was referring to not indenting the body of the inner 'if' (i.e. the
return rate; statement).
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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