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


Groups > comp.lang.c > #78239

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

From "Osmium" <r124c4u102@comcast.net>
Newsgroups comp.lang.c
Subject Re: Program to compute the amount of bottles of water...
Date 2015-12-09 08:26 -0600
Message-ID <dcqs06Fom4mU1@mid.individual.net> (permalink)
References <82a71d8a-0e36-428d-a6cc-b2af75f09c10@googlegroups.com>

Show all headers | View raw


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

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


Thread

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

csiph-web