Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #78230 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-12-09 05:00 -0800 |
| Last post | 2015-12-11 11:09 +0100 |
| Articles | 20 on this page of 93 — 17 participants |
Back to article view | Back to comp.lang.c
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 1 of 5 [1] 2 3 4 5 Next page →
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-12-09 05:00 -0800 |
| Subject | Program to compute the amount of bottles of water... |
| Message-ID | <82a71d8a-0e36-428d-a6cc-b2af75f09c10@googlegroups.com> |
Hello!
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;
}
Thank you!
[toc] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-12-09 05:13 -0800 |
| Message-ID | <69011d22-88df-44ea-b117-2940be17f5e5@googlegroups.com> |
| In reply to | #78230 |
On Wednesday, December 9, 2015 at 1:00:53 PM UTC, Alla _ wrote: > > 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. > continue only works within the function (in fact within the loop) that you happen to be in. It doesn't work across functions.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-12-09 05:17 -0800 |
| Message-ID | <949f2b93-bf3e-49ac-9dd1-f5c30e2a7ff4@googlegroups.com> |
| In reply to | #78230 |
On Wednesday, December 9, 2015 at 5:00:53 PM UTC+4, Alla _ wrote:
> Hello!
>
> 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;
> }
>
I see one of my mistakes. If c is not a digit, i is still incremented,
and the value of c is till assigned to the array member
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-12-09 13:29 +0000 |
| Message-ID | <n49a7r$vm4$1@dont-email.me> |
| In reply to | #78230 |
On 09/12/15 13:00, Alla _ wrote:
> Hello!
>
> Please, take a look at a small program.
What happened to the histogram 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.
So you're multiplying the time in minutes by 12, and displaying the
result. Okay.
> 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.
Okay, so we're back to input validation.
> 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];
Who taught you to put stuff outside functions like this? And have they
been shot yet? This is bad bad bad bad bad bad bad bad bad bad bad bad
bad design. Don't do it. File scope objects are almost never necessary.
And by "almost never" I mean "once in a blue moon". Most programs don't
need *any*. And by "most" I mean "nearly all, if not all". File scope
objects are a major cause of program bugs. When you use them, it should
be after *reluctantly* coming to the conclusion that for this one
special usage there is a massive advantage, so massive that it outweighs
all the problems and pitfalls - *and*, to discourage you further, you
should give one day's wages to charity for each file scope object you
create.
> int main(void)
> {
> printf("Please, enter the number of minutes\n");
Here's your prompt. Note that it's not in a loop.
> get_minutes(minutes);
>
> printf("%d\n", (char_to_digit(minutes) * 12));
>
> return 0;
> }
>
> void get_minutes(int minutes[])
> {
> int c, i;
>
Here comes your loop, but no prompt.
Your program isn't prompting for re-input after an erroneous input
because you haven't made any effort to instruct it to do that.
> for (i = 0; !isspace(c = getchar()); i++)
> {
> if (!isdigit(c))
> {
> continue;
> }
> else
> {
> minutes[i] = c;
> }
> }
>
> minutes[i] = '\0';
> }
Replace the whole program with the following, which you should study to
see how it works:
#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;
}
--
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]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-12-09 06:54 -0800 |
| Message-ID | <418c8c3c-4160-4909-a461-23b20a21c135@googlegroups.com> |
| In reply to | #78237 |
On Wednesday, December 9, 2015 at 5:30:01 PM UTC+4, Richard Heathfield wrote: > On 09/12/15 13:00, Alla _ wrote: > > Hello! > > > > Please, take a look at a small program. > > What happened to the histogram program? in the pipeline ) > > > 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. > > So you're multiplying the time in minutes by 12, and displaying the > result. Okay. > > > 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. > > Okay, so we're back to input validation. > > > 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]; > > Who taught you to put stuff outside functions like this? And have they > been shot yet? This is bad bad bad bad bad bad bad bad bad bad bad bad > bad design. Don't do it. File scope objects are almost never necessary. > And by "almost never" I mean "once in a blue moon". Most programs don't > need *any*. And by "most" I mean "nearly all, if not all". File scope > objects are a major cause of program bugs. When you use them, it should > be after *reluctantly* coming to the conclusion that for this one > special usage there is a massive advantage, so massive that it outweighs > all the problems and pitfalls - *and*, to discourage you further, you > should give one day's wages to charity for each file scope object you > create. > Oh! This is news to me. All the material I have read thus far presented many many programs which use global variables, and I thought that it is convenient to use those, because a few functions can get to the data presented in global environment. I see your comment on replacing the whole program, but I am sorry I can't take a look at your version now - this is part of the exercise that I have to submit online, therefore I am allowed (fairly so!) only to get hints, or suggestions on exact issues and help on exact problems and bugs, but not to have others solve the whole program for me. But I will save it, and study it as soon as I submit my one. Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-12-09 15:45 +0000 |
| Message-ID | <n49i67$d5$1@dont-email.me> |
| In reply to | #78242 |
On 09/12/15 14:54, Alla _ wrote:
> On Wednesday, December 9, 2015 at 5:30:01 PM UTC+4, Richard Heathfield wrote:
>> What happened to the histogram program?
>
> in the pipeline )
Okay...
<snip>
>>> int minutes[MAX];
>>
>> Who taught you to put stuff outside functions like this? And have they
>> been shot yet? This is bad bad bad bad bad bad bad bad bad bad bad bad
>> bad design. Don't do it. [...]
>>
> Oh! This is news to me. All the material I have read thus far presented
> many many programs which use global variables, and I thought that it is
> convenient to use those, because a few functions can get to the data presented
> in global environment.
The problem is this:
int mystery;
int afunction(void)
{
int i = 42;
mystery = 6;
i *= anotherfunction();
printf("%d\n", mystery);
return i;
}
anotherfunction() may or may not modify mystery's value, and there is no
visual clue whatsoever as to which it is. Consequently, even in this
massively simplified example you might discover that this function
prints, say, 12 rather than 6. Imagine now that afunction() is two or
three hundred lines long (yes, that's too long, but it does happen quite
a lot), and is littered with function calls. At least when you pass
pointer parameters to a function you know that you might have to go and
read that function's code if it takes a pointer to an object in whose
value you are interested. But if the object is defined at file scope,
you have to check /every/ function, just in case.
It is of course true, as you say, that file scope objects are always
there when you want them. The big problem with them is that they are
/also/ always there when you /don't/ want them.
Your programming will improve enormously when you learn to think
globally but act locally.
Data point: a colleague once asked me for help debugging a recursive
function (tree code) that just wouldn't do as it was told. He showed me
the code, and I asked why he'd defined such-and-such an object at file
scope. He was used to my purist attitude and, to his credit, generally
indulged it, but on this occasion he said that he'd just used file scope
"for now" until he'd got it working, and then he was going to tidy up
afterwards.
I moved the file scope object inside the recursive function, and told
him to re-test. The function gave no further trouble.
The problem was that recursive code very often requires a fresh set of
objects for each invocation - the recursive structure thus saves the
current state of the calculation whilst farming out sub-problems to
itself. Because he had defined the object at file scope, each invocation
of the recursive function /destroyed/ the previous invocation's record
of progress, and that meant that when that invocation returned, the
calling instance of the function had no way to continue correctly.
It was because he was in the habit of using file scope objects that he
encountered this problem, and I was able to fix it so easily because I
was /not/ in the habit of using file scope objects.
Adopt good habits *early*. They will save you huge amounts of time.
> I see your comment on replacing the whole program, but I am sorry I can't
> take a look at your version now - this is part of the exercise that I have
> to submit online, therefore I am allowed (fairly so!) only to get hints, or
> suggestions on exact issues and help on exact problems and bugs, but not to
> have others solve the whole program for me.
I wasn't suggesting that you submit it. I was suggesting that you study
it. I have little confidence that the course has any value for you. I am
far more interested in helping you to learn C than I am in helping you
to achieve some arbitrary result on an online course that appears to
have failed to teach you the basics of program structure.
> But I will save it, and study it as soon as I submit my one. Thank you!
That works.
--
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]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-12-09 08:39 -0800 |
| Message-ID | <4abcbab6-320b-4e9c-8667-460c4f612664@googlegroups.com> |
| In reply to | #78249 |
<snip>
> The problem is this:
>
> int mystery;
>
> int afunction(void)
> {
> int i = 42;
> mystery = 6;
> i *= anotherfunction();
> printf("%d\n", mystery);
> return i;
> }
>
> anotherfunction() may or may not modify mystery's value, and there is no
> visual clue whatsoever as to which it is. Consequently, even in this
> massively simplified example you might discover that this function
> prints, say, 12 rather than 6. Imagine now that afunction() is two or
> three hundred lines long (yes, that's too long, but it does happen quite
> a lot), and is littered with function calls. At least when you pass
> pointer parameters to a function you know that you might have to go and
> read that function's code if it takes a pointer to an object in whose
> value you are interested. But if the object is defined at file scope,
> you have to check /every/ function, just in case.
>
> It is of course true, as you say, that file scope objects are always
> there when you want them. The big problem with them is that they are
> /also/ always there when you /don't/ want them.
>
> Your programming will improve enormously when you learn to think
> globally but act locally.
>
> Data point: a colleague once asked me for help debugging a recursive
> function (tree code) that just wouldn't do as it was told. He showed me
> the code, and I asked why he'd defined such-and-such an object at file
> scope. He was used to my purist attitude and, to his credit, generally
> indulged it, but on this occasion he said that he'd just used file scope
> "for now" until he'd got it working, and then he was going to tidy up
> afterwards.
>
> I moved the file scope object inside the recursive function, and told
> him to re-test. The function gave no further trouble.
>
> The problem was that recursive code very often requires a fresh set of
> objects for each invocation - the recursive structure thus saves the
> current state of the calculation whilst farming out sub-problems to
> itself. Because he had defined the object at file scope, each invocation
> of the recursive function /destroyed/ the previous invocation's record
> of progress, and that meant that when that invocation returned, the
> calling instance of the function had no way to continue correctly.
>
> It was because he was in the habit of using file scope objects that he
> encountered this problem, and I was able to fix it so easily because I
> was /not/ in the habit of using file scope objects.
>
> Adopt good habits *early*. They will save you huge amounts of time.
>
I understand it now. Now life becomes harder yet again, but I will have
to follow this path )
> > I see your comment on replacing the whole program, but I am sorry I can't
> > take a look at your version now - this is part of the exercise that I have
> > to submit online, therefore I am allowed (fairly so!) only to get hints, or
> > suggestions on exact issues and help on exact problems and bugs, but not to
> > have others solve the whole program for me.
>
> I wasn't suggesting that you submit it. I was suggesting that you study
> it. I have little confidence that the course has any value for you. I am
> far more interested in helping you to learn C than I am in helping you
> to achieve some arbitrary result on an online course that appears to
> have failed to teach you the basics of program structure.
Please, let's not discuss that course. It does help me a lot, truly does.
And I wouldn't come here if not that course. Only this is enough to be
thankful to it, apart from other numerous positive moments. Also, people
who have organized that course deserve respect: they have put truly a lot of
effort and also their hearts into it. It is brilliant. It gives numerous
possibilities to people around the world, some of which would not even dream
of having this type of education (people from poorest parts of the world).
As you know, I am not doing exercises for the sake of submitting something
that works. If I did, I would have already passed the course )
I have written this program based on chapter 1 material, because I believe
there are enough tools for such exercises. There is something which I don't
yet see.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-12-09 08:38 -0800 |
| Message-ID | <a5lg6blai2jqtcusr9nt60gu5gas638s3t@4ax.com> |
| In reply to | #78242 |
On Wed, 9 Dec 2015 06:54:11 -0800 (PST), Alla _ <modelling.data@gmail.com> wrote: >On Wednesday, December 9, 2015 at 5:30:01 PM UTC+4, Richard Heathfield wrote: >> On 09/12/15 13:00, Alla _ wrote: >> > Hello! >> > >> > Please, take a look at a small program. >> >> What happened to the histogram program? > >in the pipeline ) You have missed the point. Sorry to be blunt but you are not yet ready to handle multiple projects simultaneously. SLOW DOWN. Get one program working correctly before jumping on another. You now have at least three active partially completed programs (this, horizontal histogram, and vertical histogram). If you finish one, maybe the lessons you learn will help you fix (or better prevent) similar problems in the next. -- Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-12-09 19:09 +0000 |
| Message-ID | <n49u4l$k7e$1@dont-email.me> |
| In reply to | #78257 |
On 09/12/15 16:38, Barry Schwarz wrote: > On Wed, 9 Dec 2015 06:54:11 -0800 (PST), Alla _ > <modelling.data@gmail.com> wrote: > >> On Wednesday, December 9, 2015 at 5:30:01 PM UTC+4, Richard Heathfield wrote: >>> On 09/12/15 13:00, Alla _ wrote: >>>> Hello! >>>> >>>> Please, take a look at a small program. >>> >>> What happened to the histogram program? >> >> in the pipeline ) > > You have missed the point. Sorry to be blunt but you are not yet > ready to handle multiple projects simultaneously. SLOW DOWN. On the other hand, she isn't yet ready to write the histogram program. Nor is she ready to write the bottles-of-water program, but it's an easier program - I think the decision to switch to the simpler task was a wise one. > Get one > program working correctly before jumping on another. You now have at > least three active partially completed programs (this, horizontal > histogram, and vertical histogram). If you finish one, maybe the > lessons you learn will help you fix (or better prevent) similar > problems in the next. Although I have high hopes for Alla, I see little sign of her applying old lessons to new situations. I think this is one of the consequences of her decision to go fast rather than slow. Maybe this reversion to simpler exercises is a sign that Alla is ready to slow down enough to learn properly. If so, it should be welcomed. -- 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]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-12-09 07:07 -0800 |
| Message-ID | <877947ef-ee49-4782-9f70-75ac4af19a99@googlegroups.com> |
| In reply to | #78237 |
On Wednesday, December 9, 2015 at 5:30:01 PM UTC+4, Richard Heathfield wrote:
> On 09/12/15 13:00, Alla _ wrote:
> > Hello!
> >
> > Please, take a look at a small program.
>
> What happened to the histogram 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.
>
> So you're multiplying the time in minutes by 12, and displaying the
> result. Okay.
>
> > 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.
>
> Okay, so we're back to input validation.
>
> > 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];
>
> Who taught you to put stuff outside functions like this? And have they
> been shot yet? This is bad bad bad bad bad bad bad bad bad bad bad bad
> bad design. Don't do it. File scope objects are almost never necessary.
> And by "almost never" I mean "once in a blue moon". Most programs don't
> need *any*. And by "most" I mean "nearly all, if not all". File scope
> objects are a major cause of program bugs. When you use them, it should
> be after *reluctantly* coming to the conclusion that for this one
> special usage there is a massive advantage, so massive that it outweighs
> all the problems and pitfalls - *and*, to discourage you further, you
> should give one day's wages to charity for each file scope object you
> create.
>
> > int main(void)
> > {
> > printf("Please, enter the number of minutes\n");
>
> Here's your prompt. Note that it's not in a loop.
>
> > get_minutes(minutes);
> >
> > printf("%d\n", (char_to_digit(minutes) * 12));
> >
> > return 0;
> > }
> >
> > void get_minutes(int minutes[])
> > {
> > int c, i;
> >
>
> Here comes your loop, but no prompt.
>
> Your program isn't prompting for re-input after an erroneous input
> because you haven't made any effort to instruct it to do that.
>
According to instructions, programs like this one should not
use any words as a signal of promp, therefore I thought that
having getchar within a loop is enough.
I have also tried the while one (which obviously produces the same
0 outcome for foo and the like):
void get_minutes(int minutes[])
{
int i = 0;
int c;
while (!isspace(c = getchar()))
{
if (!isdigit(c))
{
continue;
}
else
{
minutes[i] = c;
i++;
}
}
minutes[i] = '\0';
}
foo
0
Here is the result of the check I ran on my program:
:) 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
\ expected prompt for input, not exit code of 0
:( rejects "" minutes
\ expected prompt for input, not exit code of 0
:( rejects "123abc" minutes
\ expected prompt for input, not exit code of 0
[toc] | [prev] | [next] | [standalone]
| From | "Osmium" <r124c4u102@comcast.net> |
|---|---|
| Date | 2015-12-09 09:40 -0600 |
| Message-ID | <dcr0aiFpqolU1@mid.individual.net> |
| In reply to | #78245 |
"Alla _" wrote: > According to instructions, programs like this one should not > use any words as a signal of promp, therefore I thought that > having getchar within a loop is enough. There is no way a program can ensure a user enters a usable set of key strokes. So, if I understand you, the program you write should contain a call to abort() in <stdlib.h>. That will provide a program compliant with the specification you have been given. Simply responding to a bad input with a cursor and no guidance would be an even worse solution.
[toc] | [prev] | [next] | [standalone]
| From | Udyant Wig <udyantw@gmail.com> |
|---|---|
| Date | 2015-12-10 18:12 +0530 |
| Message-ID | <87twnqjwzx.fsf@rudiments.goosenet.in> |
| In reply to | #78247 |
"Osmium" <r124c4u102@comcast.net> writes: > There is no way a program can ensure a user enters a usable set of key > strokes. So, if I understand you, the program you write should contain > a call to abort() in <stdlib.h>. That will provide a program compliant > with the specification you have been given. Simply responding to a > bad input with a cursor and no guidance would be an even worse > solution. ? -- Udyant Wig
[toc] | [prev] | [next] | [standalone]
| From | "Osmium" <r124c4u102@comcast.net> |
|---|---|
| Date | 2015-12-10 07:47 -0600 |
| Message-ID | <dcte3cFdqtnU1@mid.individual.net> |
| In reply to | #78335 |
"Udyant Wig" wrote: > "Osmium" <r124c4u102@comcast.net> writes: >> There is no way a program can ensure a user enters a usable set of key >> strokes. So, if I understand you, the program you write should contain >> a call to abort() in <stdlib.h>. That will provide a program compliant >> with the specification you have been given. Simply responding to a >> bad input with a cursor and no guidance would be an even worse >> solution. > > ? The OP said: > According to instructions, programs like this one should not > use any words as a signal of promp, therefore I thought that > having getchar within a loop is enough. My reading of that is that the spec forbad him from making a second prompt after receiving a bad input. The only way he could reasonably comply with that (ill considered) requirement is what my response was about.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-12-09 08:29 -0800 |
| Message-ID | <oflg6b5bd1u6c1kutn86q76fdk6c77l7ve@4ax.com> |
| In reply to | #78245 |
On Wed, 9 Dec 2015 07:07:22 -0800 (PST), Alla _ <modelling.data@gmail.com> wrote: >According to instructions, programs like this one should not >use any words as a signal of promp, therefore I thought that >having getchar within a loop is enough. What instructions? Please quote them accurately. Any time your program is waiting for the user to type in something, there should be a prompt on the screen telling her what input is needed. Furthermore, if the user's input is not acceptable, it should be followed by an error message informing her of that fact. Even better would be an indication of why the input was not acceptable. And then a prompt to re-enter the input. -- Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-12-14 08:23 -0800 |
| Message-ID | <13d54b91-32fe-4ec6-84f7-45cf6e5ab234@googlegroups.com> |
| In reply to | #78237 |
>
> #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;
> }
>
I have once again studied this program. I understand everything in it,
but not everything about how to use the strtoul() function is still clear to
me.
I have read in the manual, written for those less comfortable with programming,
that (https://reference.cs50.net/stdlib.h/strtoul):
Quote:
The string may begin with an arbitrary amount of white space (as determined by
isspace(3)) followed by a single optional + or - sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base
16; otherwise, a zero base is taken as 10 (decimal) unless the next character
is 0, in which case it is taken as 8 (octal).
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
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
\ expected prompt for input, not exit code of 0
When I run the program with 123abc input, I don't get exit code 0;
instead I get the result of 1476 bottles, but the program should
reject such inputs and prompt the user for a correct one.
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?
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-12-14 16:55 +0000 |
| Message-ID | <n4ms62$of9$1@dont-email.me> |
| In reply to | #78650 |
On 14/12/15 16:23, Alla _ wrote:
<snip>
> 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:
[...]
> :( rejects "123abc" minutes
> \ expected prompt for input, not exit code of 0
>
> When I run the program with 123abc input, I don't get exit code 0;
> instead I get the result of 1476 bottles, but the program should
> reject such inputs and prompt the user for a correct one.
> 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?
In the code you quoted (which I wrote, and which I've snipped for
brevity), strtoul was used in its simplest form (deliberately, because I
didn't think you're ready for the more advanced usage yet).
It is, however, possible to use strtoul in a more sophisticated way, in
which it will provide you with a pointer to the first unused character.
unsigned long get_minutes(void)
{
char input[INPUT_LENGTH] = "";
unsigned long mins = 0;
char *endptr = NULL;
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, &endptr, 10);
}
if(*endptr != '\n')
{
mins = 0;
}
return mins;
}
Here, endptr is a char *.
So &endptr is a char ** (that is, a pointer to a pointer-to-char) - that
is, we pass the address of endptr to strtoul. Thus, strtoul can now
change the location (currently NULL) to which endptr points, and it does
so by pointing it to the first unconverted character.
If the user has played nice, this will be a newline character.
If not, it won't be, so we can just set mins to 0 at that point, and the
rest of the program can remain unchanged.
In recent months (years?) I have written two very long articles on
strtoul which you may be able to locate in the archives.
--
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]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2015-12-14 12:17 -0500 |
| Message-ID | <1RCby.277533$zj3.233084@fx05.iad> |
| In reply to | #78653 |
On Monday December 14 2015 11:55, in comp.lang.c, "Richard Heathfield"
<rjh@cpax.org.uk> wrote:
> On 14/12/15 16:23, Alla _ wrote:
> <snip>
>> 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:
> [...]
>> :( rejects "123abc" minutes
>> \ expected prompt for input, not exit code of 0
>>
>> When I run the program with 123abc input, I don't get exit code 0;
>> instead I get the result of 1476 bottles, but the program should
>> reject such inputs and prompt the user for a correct one.
>> 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?
>
> In the code you quoted (which I wrote, and which I've snipped for
> brevity), strtoul was used in its simplest form (deliberately, because I
> didn't think you're ready for the more advanced usage yet).
>
> It is, however, possible to use strtoul in a more sophisticated way, in
> which it will provide you with a pointer to the first unused character.
>
> unsigned long get_minutes(void)
> {
> char input[INPUT_LENGTH] = "";
> unsigned long mins = 0;
> char *endptr = NULL;
>
> 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, &endptr, 10);
> }
> if(*endptr != '\n')
> {
> mins = 0;
> }
> return mins;
> }
>
> Here, endptr is a char *.
>
> So &endptr is a char ** (that is, a pointer to a pointer-to-char) - that
> is, we pass the address of endptr to strtoul. Thus, strtoul can now
> change the location (currently NULL) to which endptr points, and it does
> so by pointing it to the first unconverted character.
>
> If the user has played nice, this will be a newline character.
>
> If not, it won't be,
"Playing nice" here depends on the programmer's remaining intended
interactions with the user.
If the programmer does not intend any further user input (once get_minutes()
has returned), the user /might/ be justified in "entering" the data and
signalling EOF, rather than entering the data, terminating with a newline.
In this case, (*endptr != '\n') evaluates false (because *endptr == 0), and
whatever the user entered is replaced by 0.
Example interaction in a POSIX-like environment
Output> Please enter the number of minutes you spend in the shower:
Input> 10^D^D
Example interaction in a MS Windows/MSDOS environment
Output> Please enter the number of minutes you spend in the shower:
Input> 10^Z^Z
> so we can just set mins to 0 at that point, and the
> rest of the program can remain unchanged.
>
> In recent months (years?) I have written two very long articles on
> strtoul which you may be able to locate in the archives.
>
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2015-12-14 13:26 -0500 |
| Message-ID | <wRDby.48723$ar.20226@fx33.iad> |
| In reply to | #78659 |
On Monday December 14 2015 12:17, in comp.lang.c, "Lew Pitcher"
<lew.pitcher@digitalfreehold.ca> wrote:
> On Monday December 14 2015 11:55, in comp.lang.c, "Richard Heathfield"
> <rjh@cpax.org.uk> wrote:
[snip]
>> It is, however, possible to use strtoul in a more sophisticated way, in
>> which it will provide you with a pointer to the first unused character.
>>
>> unsigned long get_minutes(void)
>> {
>> char input[INPUT_LENGTH] = "";
>> unsigned long mins = 0;
>> char *endptr = NULL;
>>
>> 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, &endptr, 10);
>> }
>> if(*endptr != '\n')
>> {
>> mins = 0;
>> }
>> return mins;
>> }
>>
>> Here, endptr is a char *.
>>
>> So &endptr is a char ** (that is, a pointer to a pointer-to-char) - that
>> is, we pass the address of endptr to strtoul. Thus, strtoul can now
>> change the location (currently NULL) to which endptr points, and it does
>> so by pointing it to the first unconverted character.
>>
>> If the user has played nice, this will be a newline character.
>>
>> If not, it won't be,
>
> "Playing nice" here depends on the programmer's remaining intended
> interactions with the user.
>
> If the programmer does not intend any further user input (once get_minutes()
> has returned), the user /might/ be justified in "entering" the data and
> signalling EOF, rather than entering the data, terminating with a newline.
> In this case, (*endptr != '\n') evaluates false (because *endptr == 0), and
> whatever the user entered is replaced by 0.
After a bit of lunch, I realized that a simple change can address my concern:
Instead of
if(*endptr != '\n')
{
mins = 0;
}
the test should be
if(*endptr && *endptr != '\n')
{
mins = 0;
}
[snip]
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-12-14 19:20 +0000 |
| Message-ID | <n4n4m5$tdr$1@dont-email.me> |
| In reply to | #78668 |
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?!? -- 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]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-12-14 12:54 -0800 |
| Message-ID | <lnpoy8da4i.fsf@kst-u.example.com> |
| In reply to | #78674 |
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')
?
--
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]
Page 1 of 5 [1] 2 3 4 5 Next page →
Back to top | Article view | comp.lang.c
csiph-web