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


Groups > comp.lang.c > #12449 > unrolled thread

I am learning C: a little problem with a simple source code

Started byzemir <zemir_fp@libero.it>
First post2011-11-01 08:27 -0700
Last post2011-11-02 06:11 -0700
Articles 20 on this page of 146 — 41 participants

Back to article view | Back to comp.lang.c


Contents

  I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 08:27 -0700
    Re: I am learning C: a little problem with a simple source code Mark Bluemel <mark_bluemel@pobox.com> - 2011-11-01 15:49 +0000
    Re: I am learning C: a little problem with a simple source code nroberts <roberts.noah@gmail.com> - 2011-11-01 08:52 -0700
    Re: I am learning C: a little problem with a simple source code jt@toerring.de (Jens Thoms Toerring) - 2011-11-01 16:22 +0000
    Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-01 09:31 -0700
      Re: I am learning C: a little problem with a simple source code Ike Naar <ike@sverige.freeshell.org> - 2011-11-01 16:57 +0000
        Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 11:51 -0700
          Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-01 15:04 -0400
            Re: I am learning C: a little problem with a simple source code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-01 20:21 +0000
              Re: I am learning C: a little problem with a simple source code jacob navia <jacob@spamsink.net> - 2011-11-01 21:41 +0100
          Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-02 08:06 +1300
            Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:17 -0700
              Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-01 16:24 -0400
                Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:27 -0700
                  Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-01 20:24 -0700
                    Re: I am learning C: a little problem with a simple source code gazelle@shell.xmission.com (Kenny McCormack) - 2011-11-02 10:24 +0000
                      Re: I am learning C: a little problem with a simple source code "BartC" <bc@freeuk.com> - 2011-11-02 11:04 +0000
                        Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-02 07:41 -0400
                        Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-02 18:00 +0000
                        Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-02 14:06 -0700
                          Re: I am learning C: a little problem with a simple source code John Bode <jfbode1029@gmail.com> - 2011-11-03 15:05 -0700
                          Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 22:55 -0800
                            Re: I am learning C: a little problem with a simple source code jgk@panix.com (Joe keane) - 2012-01-26 15:59 +0000
                              Re: I am learning C: a little problem with a simple source code Fritz Wuehler <fritz@spamexpire-201201.rodent.frell.theremailer.net> - 2012-01-27 20:28 +0100
                                Re: I am learning C: a little problem with a simple source code Markus Wichmann <nullplan@gmx.net> - 2012-01-28 14:11 +0100
                                  Re: I am learning C: a little problem with a simple source code Shao Miller <sha0.miller@gmail.com> - 2012-01-29 09:34 -0500
                                    Re: I am learning C: a little problem with a simple source code Markus Wichmann <nullplan@gmx.net> - 2012-01-30 13:38 +0100
                                      Re: I am learning C: a little problem with a simple source code Shao Miller <sha0.miller@gmail.com> - 2012-01-30 12:10 -0500
                                Re: I am learning C: a little problem with a simple source code Zoltan Kocsi <zoltan@bendor.com.au> - 2012-01-29 08:55 +1100
                                Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 21:48 -0800
                              Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 21:46 -0800
                                Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-01-31 22:30 -0800
                                  Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 10:11 -0800
                                    Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-08 13:51 -0800
                                      Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-09 12:51 -0800
                                        Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-09 13:36 -0800
                                          Re: I am learning C: a little problem with a simple source code jacob navia <jacob@spamsink.net> - 2012-03-09 23:50 +0100
                                          Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-09 15:01 -0800
                                            Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-09 16:21 -0800
                                              Re: I am learning C: a little problem with a simple source code gazelle@shell.xmission.com (Kenny McCormack) - 2012-03-10 00:57 +0000
                                                Re: I am learning C: a little problem with a simple source code jacob navia <jacob@spamsink.net> - 2012-03-10 13:37 +0100
                                                  Re: I am learning C: a little problem with a simple source code gazelle@shell.xmission.com (Kenny McCormack) - 2012-03-10 13:58 +0000
                                              Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-10 12:24 -0800
                                                Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-10 14:16 -0800
                                                  Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-11 12:50 -0700
                                          Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-03-11 00:57 -0800
                                            Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-11 03:20 -0700
                                              Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-03-12 02:10 -0700
                                                Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2012-03-12 10:49 -0700
                                                  Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-03-14 01:16 -0700
                                            Re: I am learning C: a little problem with a simple source code Robert Miles <milesrf@Usenet-News.net> - 2012-04-15 06:53 -0500
                                              Re: I am learning C: a little problem with a simple source code Nomen Nescio <nobody@dizum.com> - 2012-04-15 19:45 +0200
                                              Re: I am learning C: a little problem with a simple source code Philip Lantz <prl@canterey.us> - 2012-04-15 15:06 -0700
                                Re: I am learning C: a little problem with a simple source code 88888 Dihedral <dihedral88888@googlemail.com> - 2012-04-16 10:56 -0700
                        Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-03 10:19 +1300
                          Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-02 21:10 +0000
                          Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-03 05:13 +0000
                Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:29 -0700
              Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-01 21:44 +0000
                Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 06:33 -0700
                  Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-03 07:05 -0700
                    Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 15:32 -0700
                      Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 01:44 -0700
                        Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 07:30 -0700
                          Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 16:03 +0000
                            Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 09:20 -0700
                              Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 16:45 +0000
                                Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 11:59 -0700
                                  Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 19:32 +0000
                                  Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-05 04:07 -0700
                              Re: I am learning C: a little problem with a simple source code John Gordon <gordon@panix.com> - 2011-11-04 16:54 +0000
                                Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 11:58 -0700
                            Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 18:25 +0100
                          Re: I am learning C: a little problem with a simple source code gordonb.e1fpi@burditt.org (Gordon Burditt) - 2011-11-04 13:50 -0500
                            Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-04 11:57 -0700
                              Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-04 14:44 -0700
                            Re: I am learning C: a little problem with a simple source code "osmium" <r124c4u102@comcast.net> - 2011-11-04 14:03 -0500
                            Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-04 21:37 +0000
                              Re: I am learning C: a little problem with a simple source code Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-06 12:20 +0200
                      Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-04 08:32 -0700
                        Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-04 09:04 -0700
                          Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-04 10:28 -0700
                  Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-03 18:35 +0000
                    Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-03 14:50 -0700
                      Re: I am learning C: a little problem with a simple source code Keith Thompson <kst-u@mib.org> - 2011-11-03 15:59 -0700
                        Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-04 12:00 -0700
                        Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 20:58 +0100
                          Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 21:13 +0100
                      Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 01:56 -0700
                        Re: I am learning C: a little problem with a simple source code Pasquale Frega <zemir_fp@libero.it> - 2011-11-04 12:12 -0700
                          Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-05 10:09 +1300
                    Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 15:32 -0700
                      Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-03 22:53 +0000
                        Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 17:57 -0700
                          Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-03 21:35 -0500
                            Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-04 03:31 +0000
                          Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 02:25 -0700
                    Re: I am learning C: a little problem with a simple source code Kulin Remailer <remailer@reece.net.au> - 2011-11-03 22:47 +0000
                      Re: I am learning C: a little problem with a simple source code Bill Reid <hormelfree@gmail.com> - 2011-11-03 16:14 -0700
                    Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-03 21:55 -0500
                    Re: I am learning C: a little problem with a simple source code Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-04 08:10 -0700
                  Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 14:21 -0700
                    Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-04 16:31 -0500
                    Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-05 10:41 +1300
                    Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-04 21:38 +0000
                      Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-04 22:18 -0500
                        Re: I am learning C: a little problem with a simple source code Lew Pitcher <lpitcher@teksavvy.com> - 2011-11-05 11:11 -0400
                          Re: I am learning C: a little problem with a simple source code Lew Pitcher <lpitcher@teksavvy.com> - 2011-11-05 11:13 -0400
                            Re: I am learning C: a little problem with a simple source code Robert Wessel <robertwessel2@yahoo.com> - 2011-11-05 23:35 -0500
                        Re: I am learning C: a little problem with a simple source code Seebs <usenet-nospam@seebs.net> - 2011-11-06 07:40 +0000
                    Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-04 18:15 -0400
                      Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 20:00 -0700
                        Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-04 23:19 -0400
                          Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 20:33 -0700
                        Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-05 03:52 +0000
                          Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-05 17:01 +1300
                          Re: I am learning C: a little problem with a simple source code ImpalerCore <jadill33@gmail.com> - 2011-11-04 21:48 -0700
            Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:19 -0700
              Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-02 09:26 +1300
            Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:23 -0700
              Re: I am learning C: a little problem with a simple source code Ian Collins <ian-news@hotmail.com> - 2011-11-02 09:27 +1300
                Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 13:31 -0700
          Re: I am learning C: a little problem with a simple source code jt@toerring.de (Jens Thoms Toerring) - 2011-11-01 22:48 +0000
            Re: I am learning C: a little problem with a simple source code Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 23:09 -0800
          Re: I am learning C: a little problem with a simple source code gordonb.prbii@burditt.org (Gordon Burditt) - 2011-11-01 21:24 -0500
          Re: I am learning C: a little problem with a simple source code Dann Corbit <dcorbit@connx.com> - 2011-11-02 21:03 -0700
            Re: I am learning C: a little problem with a simple source code Ike Naar <ike@sverige.freeshell.org> - 2011-11-03 06:39 +0000
            Re: I am learning C: a little problem with a simple source code Barry Schwarz <schwarzb@dqel.com> - 2011-11-03 18:33 -0700
              Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-04 03:03 -0700
                Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 11:40 +0100
                Re: I am learning C: a little problem with a simple source code Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-04 11:36 +0000
                  Re: I am learning C: a little problem with a simple source code Kaz Kylheku <kaz@kylheku.com> - 2011-11-04 15:58 +0000
            Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 07:24 +0100
              Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 10:28 +0100
                Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 16:02 +0100
                  Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 16:20 +0100
                    Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-04 16:39 +0100
              Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-06 18:07 +0100
                Re: I am learning C: a little problem with a simple source code Barry Schwarz <schwarzb@dqel.com> - 2011-11-06 09:59 -0800
                  Re: I am learning C: a little problem with a simple source code "io_x" <a@b.c.invalid> - 2011-11-06 19:23 +0100
                  Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-06 15:23 -0500
        Re: I am learning C: a little problem with a simple source code zemir <zemir_fp@libero.it> - 2011-11-01 11:52 -0700
          Re: I am learning C: a little problem with a simple source code "BartC" <bc@freeuk.com> - 2011-11-01 23:02 +0000
    Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-02 03:53 -0700
      Re: I am learning C: a little problem with a simple source code James Kuyper <jameskuyper@verizon.net> - 2011-11-02 08:03 -0400
        Re: I am learning C: a little problem with a simple source code Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-02 06:11 -0700

Page 7 of 8 — ← Prev page 1 2 3 4 5 6 [7] 8  Next page →


#12481

FromIan Collins <ian-news@hotmail.com>
Date2011-11-02 09:27 +1300
Message-ID<9hb320FelvU4@mid.individual.net>
In reply to#12476
On 11/ 2/11 09:23 AM, zemir wrote:
> I have tried to resolve with other ways as well as i...

You don't have to post 3 times!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#12482

Fromzemir <zemir_fp@libero.it>
Date2011-11-01 13:31 -0700
Message-ID<ccd039ca-c2a0-447b-aa09-61590ac2b5ef@eh5g2000vbb.googlegroups.com>
In reply to#12481
On Nov 1, 9:27 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 11/ 2/11 09:23 AM, zemir wrote:
>
> > I have tried to resolve with other ways as well as i...
>
> You don't have to post 3 times!
>
> --
> Ian Collins

ok, i have a little problem with my browser.........now i have solved,
i hope.....

[toc] | [prev] | [next] | [standalone]


#12495

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-11-01 22:48 +0000
Message-ID<9hbb9iF354U1@mid.uni-berlin.de>
In reply to#12467
zemir <zemir_fp@libero.it> wrote:
> Hi all and thanks for your suggestions, but i have solved. I think my
> solution it is not good solution and more of you will be not very
> happy but it seems to work fine, very fine. Please read:

Actually, there's notthing "solved".

> #include <stdio.h>;
> #include <math.h>;
> const float Pi = 3.14159265358979323846;
> float area(float r)
> {
>     return(r*r*Pi);
> }
> float perimeter(float r) {
>     return(2*Pi*r);
> }
> void main() {

int main( void )

>     system("cls");
>     float r=0; int c;
>     printf("This programm calculate Area and Perimenter of a Circle");
>     printf("\n\nPlease insert radius value (in cm): ");

      fflush( stdout );

>     scanf("%f", &r);
>     if( r > 0 ) {

If scanf() didn't find anything that looked like a number 'r' now has
some random value.

>         printf("Area is cm2: ");
>         printf("%.2f", area(r));
>         printf("\nPerimeter is cm: ");
>         printf("%.2f", perimeter(r));

You could do that all in a single line and you should have a '\n'
at the end.

>     } else {
>          printf("Value must to be a valid number\n");
>          printf("Press anykey to continue");

End that with a '\n'.

>          c=getch();
>          system("circle");

WTF is that supposed to do?

>          return 0;

So if the user entered a negative radius (or something not a number
and the value in 'r' was a negative number) you stop the program?
And the message to the user shoud be "Press any key to end the
program"...

>     }
>     printf("\nDo you want to continue? (y or elsewhere to exit)");

      fflush( stdout );

Well, whatever the user enters, the program will stop. That's
nothing what your original program was supposed to do.

>     c=getch();
>     if(c==121) {

      if ( c == 'y' )

>         system("circle");
>         return 0;
>     }

    return 0;
> }

All you did is write a program that behaves complete different.
That's not a solution in my books. A solution would be to cor-
rect the mistakes you had in the first version. I.e. check the
return value of scanf() and if it tells you that not as many
items could be read (in your case 1) as was expected clear the
input buffer by e.g. calling getc() until you find a '\n'. Then
you can call scanf() again without it trying to read the stuff
again it already rejected the last time round.

                              Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

[toc] | [prev] | [next] | [standalone]


#16178

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-01-24 23:09 -0800
Message-ID<kfny5sw9s2o.fsf@x-alumni2.alumni.caltech.edu>
In reply to#12495
jt@toerring.de (Jens Thoms Toerring) writes:

> zemir <zemir_fp@libero.it> wrote:
>[snip]
>>     system("cls");
>>     float r=0; int c;
>>     printf("This programm calculate Area and Perimenter of a Circle");
>>     printf("\n\nPlease insert radius value (in cm): ");
>
>       fflush( stdout );
>
>>     scanf("%f", &r);
>>     if( r > 0 ) {
>
> If scanf() didn't find anything that looked like a number 'r' now has
> some random value.  [snip unrelated]

I don't think so.  Either there is a well-formed number (or INF,
etc) on input, and the scanf() call yields the value 1, or there
wasn't a well-formed number on input, and the scanf() call yields
the value 0, which means 'r' was not assigned to.  So if scanf()
didn't find something that looks like a number, 'r' still has the
value 0.

[toc] | [prev] | [next] | [standalone]


#12505

Fromgordonb.prbii@burditt.org (Gordon Burditt)
Date2011-11-01 21:24 -0500
Message-ID<hIudnfFd6cZqNC3TnZ2dnUVZ_r6dnZ2d@posted.internetamerica>
In reply to#12467
>                printf("Value must to be a valid number\n");
>                printf("Press anykey to continue");
>                c=getch();
>                system("circle");
>                return 0;

Exercise for the reader:  which keys on YOUR keyboard can you press
which will *NOT* cause getch() to continue?  (Even if you are on a
system which *has* getch()?  Of those systems that DO have getch(),
they don't all work alike.  Some do, and some do not, wait for a
character if one is not available yet.  Some do, and some do not,
echo the character.)

A few that come to mind are:  Left Shift, Right Shift, Left Ctrl,
Right Ctrl, Left Alt, Right Alt, Caps Lock, my login keycard, my
car key, my house key, and the power switch on the back of the
computer.


>        if(c==121) {

Is there a reason why you didn't write this as:
	if (c == (int)floor(sqrt(14642.5))) {
to make it even less obvious what the user response is supposed to
be?  (Especially when YES is taken as no, there is no documentation
nor tech support phone number, and the user starts reading the
source code out of desparation.)

>                system("circle");
>                return 0;

Every time you do this, you add one more execution of the program
"circle", without terminating the existing one.  When you finally
quit, all of them terminate quickly.  How many simultaneous executions
of this program do you think the system can hold before you run out
of something (memory, page space, file handles, limit on processes
for this user, or whatever) and system() will fail?


>        }
> }
> 

> Then i have another question about variable c: the y value of int c, i
> think, it is ascii code so it is the same in all the system?

C is not guaranteed to use ascii code nor anything remotely resembling
ascii code.  Ever heard of EBCDIC?  Besides that, the code is more
readable if you write 'y' where you mean 'y' without trying to
confuse people reading the code.

[toc] | [prev] | [next] | [standalone]


#12573

FromDann Corbit <dcorbit@connx.com>
Date2011-11-02 21:03 -0700
Message-ID<MPG.291bb255e6082c1a989687@aioe.org>
In reply to#12467
In article <3cd13578-0463-46aa-9dae-9a548afb5117
@l19g2000yqc.googlegroups.com>, zemir_fp@libero.it says...
> 
> Hi all and thanks for your suggestions, but i have solved. I think my
> solution it is not good solution and more of you will be not very
> happy but it seems to work fine, very fine. Please read:
> 
> #include <stdio.h>;
> #include <math.h>;
> const float Pi = 3.14159265358979323846;
> float area(float r)
> {                                                  /*define area
> function*/
> 	return(r*r*Pi);
> }
> float perimeter(float r) {                                         /
> *define perimeter function*/
> 		return(2*Pi*r);
> }
> void main() {
> 	system("cls");
> 	float r=0; int c;/*define radius and c variable used to control the
> behaviour of program*/
> 	printf("This programm calculate Area and Perimenter of a Circle");
> 	printf("\n\nPlease insert radius value (in cm): "); /*input of the
> radius value*/
> 	scanf("%f", &r);
> if(r>0) {
> 	printf("Area is cm2: ");
> 	printf("%.2f", area(r));                                /*call area
> function*/
> 	printf("\nPerimeter is cm: ");
> 	printf("%.2f", perimeter(r));                       	/*call perimeter
> function*/
> 	} else {
> 		printf("Value must to be a valid number\n");
> 		printf("Press anykey to continue");
> 		c=getch();
> 		system("circle");
> 		return 0;
> 	}
> 	printf("\nDo you want to continue? (y or elsewhere to exit)");
> 	c=getch();
> 	if(c==121) {
> 		system("circle");
> 		return 0;
> 	}
> }
> 
> Then i have another question about variable c: the y value of int c, i
> think, it is ascii code so it is the same in all the system?

On EBCDIC systems y is 169.  Consider also that they might type in a 
capital 'Y' and get surprised if it gets interpreted as quitting time.
You've got some non-portable stuff in your code but then again, I used a 
goto so both of us are evil and doomed forever more.

I came up with this variant when I saw your original post.

#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

static const double pi_approximation = 
3.1415926535897932384626433832795028841971693993751;
static char string[32767];

/* Area of a circle with radius r */
double area (double r)
{
    return (r * r * pi_approximation);
}

/* Perimeter of a circle with radius r */
double perimeter (double r)
{
    return (2.0 * pi_approximation * r);
}

/* Safely collect a string (due to Jack Klein) */
char * getsafe (char *buffer, int count)
{
    char *result = buffer, *np;
    if ((buffer == NULL) || (count < 1))
        result = NULL;
    else if (count == 1)
        *result = '\0';
    else if ((result = fgets (buffer, count, stdin)) != NULL)
        if (np = strchr (buffer, '\n'))
            *np = '\0';
    return result;
}


int main (void)
{
    double r = 0;
    int c = 0;
    int converted;
    printf ("This programm calculate Area and Perimenter of a Circle");
    do
    {
doofus:
        printf ("\n\nPlease give radius value (in cm):");
        fflush (stdout);
        getsafe (string, sizeof string);
        r = atof (string);
        if (r <= 0)
        {
            if (r == 0 && string[0] == '0')
            {
                puts ("Both perimeter and radius are zero, but you knew 
that.");
            }
            else
            {
                puts ("No such thing as a negative radius in Euclidean 
geometry.");
                goto doofus;
            }
        }
        printf ("Area = %.2f cm^2\n", area (r));
        printf ("Perimeter = %.2f cm\n", perimeter (r));
        puts ("Do you want to continue? (y or elsewhere to exit):");
        fflush (stdout);
        c = getchar ();
    }
    while (toupper (c) == 'Y');
    return 0;
}

[toc] | [prev] | [next] | [standalone]


#12575

FromIke Naar <ike@sverige.freeshell.org>
Date2011-11-03 06:39 +0000
Message-ID<slrn3vfsjb4dom.l3l.ike@sverige.freeshell.org>
In reply to#12573
On 2011-11-03, Dann Corbit <dcorbit@connx.com> wrote:
> int main (void)
> {
>     double r = 0;

pointless initialization.

>     int c = 0;
>     int converted;

unused variable.

>     printf ("This programm calculate Area and Perimenter of a Circle");
>     do
>     {
> doofus:
>         printf ("\n\nPlease give radius value (in cm):");
>         fflush (stdout);
>         getsafe (string, sizeof string);
>         r = atof (string);

#include <stdlib.h> is needed for atof.

>         if (r <= 0)
>         {
>             if (r == 0 && string[0] == '0')

This test will fail for e.g. the input string ".0".

>             {
>                 puts ("Both perimeter and radius are zero, but you knew 
> that.");
>             }
>             else
>             {
>                 puts ("No such thing as a negative radius in Euclidean 
> geometry.");
>                 goto doofus;
>             }
>         }
>         printf ("Area = %.2f cm^2\n", area (r));
>         printf ("Perimeter = %.2f cm\n", perimeter (r));
>         puts ("Do you want to continue? (y or elsewhere to exit):");
>         fflush (stdout);
>         c = getchar ();
>     }
>     while (toupper (c) == 'Y');
>     return 0;
> }

[toc] | [prev] | [next] | [standalone]


#12650

FromBarry Schwarz <schwarzb@dqel.com>
Date2011-11-03 18:33 -0700
Message-ID<e6f6b7poeq8uv5lm0evao4qlv9otsls49e@4ax.com>
In reply to#12573
On Wed, 2 Nov 2011 21:03:34 -0700, Dann Corbit <dcorbit@connx.com>
wrote:


snip

>On EBCDIC systems y is 169.  Consider also that they might type in a 
>capital 'Y' and get surprised if it gets interpreted as quitting time.

On EBCDIC systems, 169 == 0xa9 == 'z'

>You've got some non-portable stuff in your code but then again, I used a 
>goto so both of us are evil and doomed forever more.
>
>I came up with this variant when I saw your original post.
>
>#include <stdio.h>
>#include <ctype.h>
>#include <math.h>
>#include <string.h>
>
>static const double pi_approximation = 
>3.1415926535897932384626433832795028841971693993751;
>static char string[32767];
>
>/* Area of a circle with radius r */
>double area (double r)
>{
>    return (r * r * pi_approximation);
>}
>
>/* Perimeter of a circle with radius r */
>double perimeter (double r)
>{
>    return (2.0 * pi_approximation * r);
>}
>
>/* Safely collect a string (due to Jack Klein) */
>char * getsafe (char *buffer, int count)
>{
>    char *result = buffer, *np;
>    if ((buffer == NULL) || (count < 1))
>        result = NULL;
>    else if (count == 1)
>        *result = '\0';
>    else if ((result = fgets (buffer, count, stdin)) != NULL)
>        if (np = strchr (buffer, '\n'))
>            *np = '\0';
>    return result;
>}
>
>
>int main (void)
>{
>    double r = 0;
>    int c = 0;
>    int converted;
>    printf ("This programm calculate Area and Perimenter of a Circle");
>    do
>    {
>doofus:
>        printf ("\n\nPlease give radius value (in cm):");
>        fflush (stdout);
>        getsafe (string, sizeof string);
>        r = atof (string);
>        if (r <= 0)
>        {
>            if (r == 0 && string[0] == '0')
>            {
>                puts ("Both perimeter and radius are zero, but you knew 
>that.");
>            }
>            else
>            {
>                puts ("No such thing as a negative radius in Euclidean 
>geometry.");
>                goto doofus;

Why not use the continue statement and eliminate both the goto and the
label?

>            }
>        }
>        printf ("Area = %.2f cm^2\n", area (r));
>        printf ("Perimeter = %.2f cm\n", perimeter (r));

It is a tad unusual to leave a space between the function name and the
argument list.

>        puts ("Do you want to continue? (y or elsewhere to exit):");
>        fflush (stdout);
>        c = getchar ();
>    }
>    while (toupper (c) == 'Y');
>    return 0;
>}

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#12670

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2011-11-04 03:03 -0700
Message-ID<f66d2fa9-a957-460a-9bf0-79846e304694@x8g2000yql.googlegroups.com>
In reply to#12650
On Nov 4, 1:33 am, Barry Schwarz <schwa...@dqel.com> wrote:
> On Wed, 2 Nov 2011 21:03:34 -0700, Dann Corbit <dcor...@connx.com>

<snip>

> >I came up with this variant when I saw your original post.
>
> >#include <stdio.h>
> >#include <ctype.h>
> >#include <math.h>
> >#include <string.h>
>
> >static const double pi_approximation =
> >3.1415926535897932384626433832795028841971693993751;
> >static char string[32767];
>
> >/* Area of a circle with radius r */
> >double area (double r)
> >{
> >    return (r * r * pi_approximation);
> >}
>
> >/* Perimeter of a circle with radius r */
> >double perimeter (double r)
> >{
> >    return (2.0 * pi_approximation * r);
> >}
>
> >/* Safely collect a string (due to Jack Klein) */
> >char * getsafe (char *buffer, int count)
> >{
> >    char *result = buffer, *np;
> >    if ((buffer == NULL) || (count < 1))
> >        result = NULL;
> >    else if (count == 1)
> >        *result = '\0';
> >    else if ((result = fgets (buffer, count, stdin)) != NULL)
> >        if (np = strchr (buffer, '\n'))
> >            *np = '\0';
> >    return result;
> >}
>
> >int main (void)
> >{
> >    double r = 0;
> >    int c = 0;
> >    int converted;
> >    printf ("This programm calculate Area and Perimenter of a Circle");
> >    do
> >    {
> >doofus:
> >        printf ("\n\nPlease give radius value (in cm):");
> >        fflush (stdout);
> >        getsafe (string, sizeof string);
> >        r = atof (string);
> >        if (r <= 0)
> >        {
> >            if (r == 0 && string[0] == '0')
> >            {
> >                puts ("Both perimeter and radius are zero, but you knew
> >that.");
> >            }
> >            else
> >            {
> >                puts ("No such thing as a negative radius in Euclidean
> >geometry.");
> >                goto doofus;
>
> Why not use the continue statement and eliminate both the goto and the
> label?

continue is nearly as nasty as goto. They can both be eliminated in
this case

> >            }
> >        }
> >        printf ("Area = %.2f cm^2\n", area (r));
> >        printf ("Perimeter = %.2f cm\n", perimeter (r));
>
> It is a tad unusual to leave a space between the function name and the
> argument list.

I do it all the time. Why shouldn't it follow the normal text
convention?

goto-less version

int main (void)
{
    double r = 0;
    int c = '\N';
    int converted;

    printf ("This program calculate the Area and Perimenter of a
Circle");

    do
    {
        printf ("\n\nPlease give radius value (in cm):");
        fflush (stdout);
        getsafe (string, sizeof string);
        r = atof (string);

        if (r <= 0)
        {
            if (r == 0 && string[0] == '0')
            {
                puts ("Both perimeter and radius are zero, but you
knew that.");
            }
            else
            {
                puts ("No such thing as a negative radius in Euclidean
geometry.");
            }
        }
        else
        {
            printf ("Area = %.2f cm^2\n", area (r));
            printf ("Perimeter = %.2f cm\n", perimeter (r));
            puts ("Do you want to continue? (y or elsewhere to
exit):");
            fflush (stdout);
            c = getchar ();
        }
    }
    while (toupper (c) == 'Y');

    return 0;
}



[toc] | [prev] | [next] | [standalone]


#12671

From"io_x" <a@b.c.invalid>
Date2011-11-04 11:40 +0100
Message-ID<4eb3c062$0$1385$4fafbaef@reader2.news.tin.it>
In reply to#12670
"Nick Keighley" <nick_keighley_nospam@hotmail.com> ha scritto nel messaggio
news:f66d2fa9-a957-460a-9bf0-79846e304694@x8g2000yql.googlegroups.com...
On Nov 4, 1:33 am, Barry Schwarz <schwa...@dqel.com> wrote:
> On Wed, 2 Nov 2011 21:03:34 -0700, Dann Corbit <dcor...@connx.com>

> > printf ("Area = %.2f cm^2\n", area (r));
> > printf ("Perimeter = %.2f cm\n", perimeter (r));
>
> It is a tad unusual to leave a space between the function name and the
> argument list.

I do it all the time. Why shouldn't it follow the normal text
convention?

#this is io_x
#as convention
#i find better not use the space in if() while() etc

goto-less version

int main (void)
{
    double r = 0;
    int c = '\N';

#it is c='\n'?

    int converted;

    printf ("This program calculate the Area and Perimenter of a
Circle");

    do
    {
        printf ("\n\nPlease give radius value (in cm):");
        fflush (stdout);
        getsafe (string, sizeof string);
        r = atof (string);

        if (r <= 0)
        {
            if (r == 0 && string[0] == '0')

#i not have full understood why string[0] has to be '0' here
#can not be "spaces<fnumber>" for atof() ?

            {
                puts ("Both perimeter and radius are zero, but you
knew that.");

#why r should be not 0? why it is an error?
#it follow the path of error and give no area etc

            }
            else
            {
                puts ("No such thing as a negative radius in Euclidean
geometry.");
            }
        }
        else
        {
            printf ("Area = %.2f cm^2\n", area (r));
            printf ("Perimeter = %.2f cm\n", perimeter (r));
            puts ("Do you want to continue? (y or elsewhere to
exit):");
            fflush (stdout);
            c = getchar ();
        }

#if i understand well the deep code indentation:
#here you can reach this position using execution path begin with if(r<=0);
#in the case r<=0, if r<=0, what it is in variable "c"? it will be "Y" or "N"?

    }
    while (toupper (c) == 'Y');

    return 0;
}






[toc] | [prev] | [next] | [standalone]


#12672

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-11-04 11:36 +0000
Message-ID<0.272061a526d1234f607f.20111104113607GMT.8739e4glw8.fsf@bsb.me.uk>
In reply to#12670
Nick Keighley <nick_keighley_nospam@hotmail.com> writes:

> On Nov 4, 1:33 am, Barry Schwarz <schwa...@dqel.com> wrote:
>> On Wed, 2 Nov 2011 21:03:34 -0700, Dann Corbit <dcor...@connx.com>
<snip>
>> >        printf ("Area = %.2f cm^2\n", area (r));
>> >        printf ("Perimeter = %.2f cm\n", perimeter (r));
>>
>> It is a tad unusual to leave a space between the function name and the
>> argument list.
>
> I do it all the time. Why shouldn't it follow the normal text
> convention?

The main argument (no pun intended) is that the programming usage
follows from mathematics: f(x) and so on.  Even having seen the inserted
space for some time (I thinks it's relatively new) I still can't shake
off the sense it gives of a parenthetical remark:

  f(x) + y    // f applied to x plus y
  f (x) + y   // f plus y (and by the way, x)

Silly, I know, but it's only fair that there be a comment from someone
who hates this style!

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#12688

FromKaz Kylheku <kaz@kylheku.com>
Date2011-11-04 15:58 +0000
Message-ID<20111104115711.756@kylheku.com>
In reply to#12672
On 2011-11-04, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Nick Keighley <nick_keighley_nospam@hotmail.com> writes:
>
>> On Nov 4, 1:33 am, Barry Schwarz <schwa...@dqel.com> wrote:
>>> On Wed, 2 Nov 2011 21:03:34 -0700, Dann Corbit <dcor...@connx.com>
><snip>
>>> >        printf ("Area = %.2f cm^2\n", area (r));
>>> >        printf ("Perimeter = %.2f cm\n", perimeter (r));
>>>
>>> It is a tad unusual to leave a space between the function name and the
>>> argument list.
>>
>> I do it all the time. Why shouldn't it follow the normal text
>> convention?
>
> The main argument (no pun intended) is that the programming usage
> follows from mathematics: f(x) and so on.  Even having seen the inserted
> space for some time (I thinks it's relatively new) I still can't shake
> off the sense it gives of a parenthetical remark:
>
>   f(x) + y    // f applied to x plus y
>   f (x) + y   // f plus y (and by the way, x)
>
> Silly, I know, but it's only fair that there be a comment from someone
> who hates this style!

F (X) is only appropriate when F is a macro or operator, and (X) is not
evaluated as a normal function argument list.

  assert (cond)
  sizeof (type)
  if (cond) ...
  fun(x)

Writing fun (x) is a good way to invite the reader of your code to
regard you as a twit.

[toc] | [prev] | [next] | [standalone]


#12662

From"io_x" <a@b.c.invalid>
Date2011-11-04 07:24 +0100
Message-ID<4eb38450$0$1380$4fafbaef@reader2.news.tin.it>
In reply to#12573
"Dann Corbit" <dcorbit@connx.com> ha scritto nel messaggio
news:MPG.291bb255e6082c1a989687@aioe.org...
> In article <3cd13578-0463-46aa-9dae-9a548afb5117
> @l19g2000yqc.googlegroups.com>, zemir_fp@libero.it says...
> I came up with this variant when I saw your original post.
>
> #include <stdio.h>
> #include <ctype.h>
> #include <math.h>
> #include <string.h>
>
> static const double pi_approximation =
> 3.1415926535897932384626433832795028841971693993751;
> static char string[32767];
>
> /* Area of a circle with radius r */
> double area (double r)
> {
>    return (r * r * pi_approximation);
> }
>
> /* Perimeter of a circle with radius r */
> double perimeter (double r)
> {
>    return (2.0 * pi_approximation * r);
> }
>
> /* Safely collect a string (due to Jack Klein) */
> char * getsafe (char *buffer, int count)
> {
>    char *result = buffer, *np;
>    if ((buffer == NULL) || (count < 1))
>        result = NULL;
>    else if (count == 1)
>        *result = '\0';
>    else if ((result = fgets (buffer, count, stdin)) != NULL)
>        if (np = strchr (buffer, '\n'))
>            *np = '\0';
>    return result;
> }
>
>
> int main (void)
> {
>    double r = 0;
>    int c = 0;
>    int converted;
>    printf ("This programm calculate Area and Perimenter of a Circle");
>    do
>    {
> doofus:
>        printf ("\n\nPlease give radius value (in cm):");
>        fflush (stdout);
>        getsafe (string, sizeof string);
>        r = atof (string);
>        if (r <= 0)
>        {
>            if (r == 0 && string[0] == '0')
>            {
>                puts ("Both perimeter and radius are zero, but you knew
> that.");
>            }
>            else
>            {
>                puts ("No such thing as a negative radius in Euclidean
> geometry.");
>                goto doofus;
>            }
>        }
>        printf ("Area = %.2f cm^2\n", area (r));
>        printf ("Perimeter = %.2f cm\n", perimeter (r));
>        puts ("Do you want to continue? (y or elsewhere to exit):");
>        fflush (stdout);
>        c = getchar ();
>    }
>    while (toupper (c) == 'Y');
>    return 0;
> }

I came up with this variant of your NG code using my
little i/o library i wrote some time ago for exercise
of find easy i/o functions not too much difficult
to implement and use.

I think these routines are better and easy for use
of all your[NG] and all standard C and C++ library
function for std input.

for the OP: i use goto, pointers and a home made .dll library
for this exercise so it could be not ok for you...

Are there errors in them? it is easy to make errors...

#include  <stdio.h>
#include  <ctype.h>
#include  <math.h>
#include  <string.h>
#include  <stdlib.h>
#include  <float.h>
#include  <limits.h>

static char string[32767];

double pi=3.1415926535897932384626433832795028841971693993751;

/* Area of a circle with radius r */
double       area(double r){return r*r*pi;}

/* Perimeter of a circle with radius r */
double  perimeter(double r){return 2.0*pi*r;}

/* Safely collect a string (due to Jack Klein) */
char * getsafe (char *buffer, int count)
{
    char *result = buffer, *np;
    if ((buffer == NULL) || (count < 1))
        result = NULL;
    else if (count == 1)
        *result = '\0';
    else if ((result = fgets (buffer, count, stdin)) != NULL)
        if (np = strchr (buffer, '\n'))
            *np = '\0';
    return result;
}

// library definitions
double  GetDouble(char*   testo);
char*   GetLine(char*     testo);


int main(void)
{double              r;
 int      c, nerrors=0;
 int         converted;
 char          *pc, *p;

 printf ("This programm calculate Area and Perimenter of a Circle\n");
doofus:
   if(nerrors==10) {puts("Troppi errori Esco\n"); return 0;}
   r=GetDouble("nPlease give radius value (in cm): ");
   if(r==DBL_MIN)
         {puts("Error in input"); ++nerrors;  goto doofus;}
   if(r<=0.0)
      {if(r==0.0)
         {puts("Both perimeter and radius are zero, but you knew that.");}
       else
         {puts ("No such thing as a negative radius in Euclidean geometry.");
          ++nerrors;
          goto doofus;
         }
      }
   nerrors=0;
   printf("Area      = %.2f cm^2\n",      area(r));
   printf("Perimeter = %.2f cm  \n", perimeter(r));

   pc=GetLine("Do you want to continue? (y or elsewhere to exit): ");
   if(pc==0) {puts("There is one error i can not solve\n"); return 0;}
   for(p=pc; *p && isspace((unsigned char)*p) ;  ++p)  ;
   c=*p;
   free(pc);
   if(toupper(c)=='Y') goto doofus;
 printf("\n");
 return 0;
}

//--------------------------------------
// std i/o Library
#include  <stdio.h>
#include   <stdlib.h>
#include  <float.h>
#include  <limits.h>
#include  <math.h>

// ritorna un puntatore da liberarsi tramite free()
// ritorna 0 per errore
char* __export  GetLine(char*  testo)
{unsigned  s, i;
 int          c;
 char     *p,*t;
 s=1024; p=malloc(s);
 if(p==0)  return  0;
 printf("%s", testo); fflush(stdout);
 for(i=0; (c=getchar())!=EOF ; )
   {if(i>=s)
       {s=s+1024;
        t=realloc(p, s+4);
        if(t==0||(int)s<0)
           {free(p); return  0;}
        p=t;
       }
    p[i]=c; ++i;
    if(c=='\n')
           break;
   }
 p[i]=0;
 return   p;
}

// ritorna INT_MIN per errore
int __export  GetInt(char*   testo)
{int               i,c;
 long                v;
 char  arr[1028], *p=0;

 printf("%s", testo); fflush(stdout);
 for(i=0; i<1024; ++i)
    {c=getchar();
     if(c==EOF||c=='\n')
          break;
     arr[i]=(char)c;
    }
 arr[i]=0;
 if(i==1024)   // elimina la linea fino a '\n'
    {while((c=getchar())!=EOF)
         if(c=='\n')  break;
     return  INT_MIN;
    }
 v=strtol(arr, &p, 10);
 if(v<INT_MIN||v>INT_MAX||p==arr)
         return  INT_MIN;
 return  (int) v;
}

// ritorna UINT_MAX per errore
unsigned __export  GetUns(char*   testo)
{int               i,c;
 unsigned long       v;
 char  arr[1028], *p=0;

 printf("%s", testo); fflush(stdout);
 for(i=0; i<1024; ++i)
    {c=getchar();
     if(c==EOF||c=='\n')
          break;
     arr[i]=(char)c;
    }
 arr[i]=0;
 if(i==1024)   // elimina la linea fino a '\n'
    {while((c=getchar())!=EOF)
         if(c=='\n')  break;
     return  UINT_MAX;
    }
 v=strtoul(arr, &p, 10);
 if(v>UINT_MAX||p==arr)
         return  UINT_MAX;
 return  (unsigned) v;
}


// ritorna DBL_MIN per errore
double __export  GetDouble(char*   testo)
{int               i,c;
 double              v;
 char  arr[1028], *p=0;

 printf("%s", testo); fflush(stdout);
 for(i=0; i<1024; ++i)
    {c=getchar();
     if(c==EOF||c=='\n')
          break;
     arr[i]=(char)c;
    }
 arr[i]=0;
 if(i==1024)   // elimina la linea fino a '\n'
    {while((c=getchar())!=EOF)
         if(c=='\n')  break;
     return  DBL_MIN;
    }
 v=strtod(arr, &p);
 if(v==HUGE_VAL||p==arr)
     return  DBL_MIN;
 return   v;
}

/*
int WINAPI
   DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{(void) hinst;  (void) lpReserved;
 if(reason == DLL_PROCESS_ATTACH){}
 else if(reason == DLL_PROCESS_DETACH){}
 return   1;
}
*/





[toc] | [prev] | [next] | [standalone]


#12668

From"io_x" <a@b.c.invalid>
Date2011-11-04 10:28 +0100
Message-ID<4eb3af70$0$1381$4fafbaef@reader1.news.tin.it>
In reply to#12662
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4eb38450$0$1380$4fafbaef@reader2.news.tin.it...
> // ritorna un puntatore da liberarsi tramite free()
> // ritorna 0 per errore
> char* __export  GetLine(char*  testo)
> {unsigned  s, i;
> int          c;
> char     *p,*t;
> s=1024; p=malloc(s);
> if(p==0)  return  0;
> printf("%s", testo); fflush(stdout);
> for(i=0; (c=getchar())!=EOF ; )
>   {if(i>=s)
>       {s=s+1024;

pheraps better:

+         if(s+4 >= (unsigned)INT_MAX)
+                goto  li;

>        t=realloc(p, s+4);

it is easy to make one error:
for example here there is at last one error
in the lines "if(t==0||(int)s<0)
                  {free(p); return  0;}"
[double free of p in the case (int)s<0] with
the below line [and the lines using +] would be ok
        if(t==0){li: free(p); return  0;}

but why nobody find the problem?
my write is incomprensible? there is some other problem?

these routine are only for joke; the right one
is to rewrite *scanf* in the way handle all errors
and handle u32, u8, u16 too

>        if(t==0||(int)s<0)
>           {free(p); return  0;}
>        p=t;
>       }
>    p[i]=c; ++i;
>    if(c=='\n')
>           break;
>   }
> p[i]=0;
> return   p;
> }


[toc] | [prev] | [next] | [standalone]


#12678

From"io_x" <a@b.c.invalid>
Date2011-11-04 16:02 +0100
Message-ID<4eb3fdc5$0$1379$4fafbaef@reader2.news.tin.it>
In reply to#12668
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4eb3af70$0$1381$4fafbaef@reader1.news.tin.it...
>
> "io_x" <a@b.c.invalid> ha scritto nel messaggio
> news:4eb38450$0$1380$4fafbaef@reader2.news.tin.it...
>> // ritorna un puntatore da liberarsi tramite free()
>> // ritorna 0 per errore
>> char* __export  GetLine(char*  testo)

what about below function?

// ritorna un puntatore a una *stringa* da liberare con free().
// ritorna 0 per errore.
// se incontra EOF prima di \n termina la stringa con
// i bytes '\0' '\-1' altrimenti termina la stringa con
// i bytes '\0' '\0'
char* __export  GetLine(char*  testo)
{unsigned  s, i;
 int          c;
 char     *p,*t;

 s=1024; p=malloc(s);
 if(p==0)  return  0;
 printf("%s", testo); fflush(stdout);

 for(i=0; (c=getchar())!=EOF ; )
   {if(i>=s)
       {s=s+1024;
        if(s+4>(unsigned) INT_MAX )
             goto li;
        t=realloc(p, s+4);
        if(t==0)
           {li: free(p); return  0;}
        p=t;
       }
    p[i]=c; ++i;
    if(c=='\n')
           break;
   }
 p[i]   =  0;
 p[i+1] = (c==EOF?-1: 0);
 return   p;
}

what about below function?

// par: res: pointer to string of output
//      testo: string to write to the screen
// return
// (unsigned)-1 for error
// or if ok return the len of string in res
// if there is no '\n' the string end with bytes '\0\-1'
// if there is the '\n' the string end with bytes '\0\0'
unsigned  __export  GetLine(char** res, char*  testo)
{unsigned  s, i;
 int       c, r;
 char     *p,*t;

 if(res==0) return -1;
 s=1024; p=malloc(s);   if(p==0) return -1;
 r=printf("%s", testo); if(r <  0) goto li;
 r=fflush(stdout);      if(r==EOF) goto li;

 for(i=0; (c=getchar())!=EOF ; )
   {if(i>=s)
       {s=s+1024;
        if(s+4>(unsigned) INT_MAX )
             goto li;
        t=realloc(p, s+4);
        if(t==0)
           {li: free(p); return  -1;}
        p=t;
       }
    p[i]=c; ++i;
    if(c=='\n')
           break;
   }
 p[i]   =  0;
 p[i+1] = (c==EOF?-1: 0);
 *res=p;
 if(ferror(stdin))
        return -1;
 return   i;
}




[toc] | [prev] | [next] | [standalone]


#12682

From"io_x" <a@b.c.invalid>
Date2011-11-04 16:20 +0100
Message-ID<4eb40211$0$1374$4fafbaef@reader1.news.tin.it>
In reply to#12678
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4eb3fdc5$0$1379$4fafbaef@reader2.news.tin.it...
>
> "io_x" <a@b.c.invalid> ha scritto nel messaggio
> news:4eb3af70$0$1381$4fafbaef@reader1.news.tin.it...
>>
>> "io_x" <a@b.c.invalid> ha scritto nel messaggio
>> news:4eb38450$0$1380$4fafbaef@reader2.news.tin.it...
>>> // ritorna un puntatore da liberarsi tramite free()
>>> // ritorna 0 per errore
>>> char* __export  GetLine(char*  testo)
>
> what about below function?
>
> // ritorna un puntatore a una *stringa* da liberare con free().
> // ritorna 0 per errore.
> // se incontra EOF prima di \n termina la stringa con
> // i bytes '\0' '\-1' altrimenti termina la stringa con
> // i bytes '\0' '\0'
> char* __export  GetLine(char*  testo)
> {unsigned  s, i;
> int          c;
> char     *p,*t;
>
> s=1024; p=malloc(s);
> if(p==0)  return  0;
> printf("%s", testo); fflush(stdout);
>
> for(i=0; (c=getchar())!=EOF ; )
>   {if(i>=s)
>       {s=s+1024;
>        if(s+4>(unsigned) INT_MAX )
>             goto li;
>        t=realloc(p, s+4);
>        if(t==0)
>           {li: free(p); return  0;}
>        p=t;
>       }
>    p[i]=c; ++i;
>    if(c=='\n')
>           break;
>   }
> p[i]   =  0;
> p[i+1] = (c==EOF?-1: 0);

i realize above is not necessary because
if the string end with "\n" than no EOF is met
else if EOF is meet string not end with "\n"


what about below function?

// fa apparire il testo nello schermo e prende una stringa
// da stdin
// ritorna un puntatore a una *stringa* da liberare con free().
// ritorna 0 per errore.
char* __export  GetLine(char*  testo)
{unsigned  s, i;
 int       c, r;
 char     *p,*t;

 s=1024; p=malloc(s);   if(p==0)   return  0;
 r=printf("%s", testo); if(r< 0)   goto   li;
 r=fflush(stdout);      if(r==EOF) goto   li;

 for(i=0; (c=getchar())!=EOF ; )
   {if(i>=s)
       {s=s+1024;
        if(s+4>(unsigned) INT_MAX )
             goto li;
        t=realloc(p, s+4);
        if(t==0)
           {li: free(p); return  0;}
        p=t;
       }
    p[i]=c; ++i;
    if(c=='\n')  break;
   }
 p[i]=0;
 if(ferror(stdin))
        goto  li;
 return   p;
}

what about below function?
both not tested...
have a good day

// par: res: pointer to string of input
//      testo: string to write to the screen
// return
// (unsigned)-1 for error
// or if ok return the len of string in res
unsigned  __export  GetLine(char** res, char*  testo)
{unsigned  s, i;
 int       c, r;
 char     *p,*t;

 if(res==0) return -1;
 s=1024; p=malloc(s);   if(p==0) return -1;
 r=printf("%s", testo); if(r <  0) goto li;
 r=fflush(stdout);      if(r==EOF) goto li;

 for(i=0; (c=getchar())!=EOF ; )
   {if(i>=s)
       {s=s+1024;
        if(s+4>(unsigned) INT_MAX )
             goto li;
        t=realloc(p, s+4);
        if(t==0)
           {li: free(p); return  -1;}
        p=t;
       }
    p[i]=c; ++i;
    if(c=='\n') break;
   }
 p[i]= 0;
 *res= p;
 if(ferror(stdin))
        return -1;
 return   i;
}



[toc] | [prev] | [next] | [standalone]


#12686

From"io_x" <a@b.c.invalid>
Date2011-11-04 16:39 +0100
Message-ID<4eb40657$0$1387$4fafbaef@reader1.news.tin.it>
In reply to#12682
"io_x" <a@b.c.invalid> ha scritto nel messaggio 
news:4eb40211$0$1374$4fafbaef@reader1.news.tin.it...

> // fa apparire il testo nello schermo e prende una stringa
> // da stdin
> // ritorna un puntatore a una *stringa* da liberare con free().
> // ritorna 0 per errore.
> char* __export  GetLine(char*  testo)
> {unsigned  s, i;
> int       c, r;
> char     *p,*t;
>
> s=1024; p=malloc(s);   if(p==0)   return  0;
^^^^^^^^^^^^^^^^^^^^^^^^^
i have one dubit about the write +1 of the array...
it has to be:
 s=1024; p=malloc(s+4);  if(p==0)   return  0;
...

> what about below function?
> both not tested...
> have a good day
>
> // par: res: pointer to string of input
> //      testo: string to write to the screen
> // return
> // (unsigned)-1 for error
> // or if ok return the len of string in res
> unsigned  __export  GetLine(char** res, char*  testo)
> {unsigned  s, i;
> int       c, r;
> char     *p,*t;
>
> if(res==0) return -1;
> s=1024; p=malloc(s);   if(p==0) return -1;
^^^^^^^^^^^^^^^^^^^^^^^^
s=1024; p=malloc(s+4);  if(p==0)   return  0;

> r=printf("%s", testo); if(r <  0) goto li;
> r=fflush(stdout);      if(r==EOF) goto li;



[toc] | [prev] | [next] | [standalone]


#12816

From"io_x" <a@b.c.invalid>
Date2011-11-06 18:07 +0100
Message-ID<4eb6bdf6$0$1392$4fafbaef@reader1.news.tin.it>
In reply to#12662
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4eb38450$0$1380$4fafbaef@reader2.news.tin.it...

when i see my routines i note this

>> /* Safely collect a string (due to Jack Klein) */
>> char * getsafe (char *buffer, int count)
>> {
>>    char *result = buffer, *np;
>>    if ((buffer == NULL) || (count < 1))
>>        result = NULL;
>>    else if (count == 1)
>>        *result = '\0';
>>    else if ((result = fgets (buffer, count, stdin)) != NULL)
>>        if (np = strchr (buffer, '\n'))
>>            *np = '\0';
>>    return result;
>> }

it seem to me that if someone insert  stringInput<EOF>
than stringInput is a valid line even if not has "\n"
the above function not get that string
because return NULL on that input

there is someone out there? It seem the only response to my posts
is by Ian Collins that i plonk some time ago [i see that trhu
google groups] so i can not answer

possibly *i'm* in the KF of many in the group for my strange
behaviour too
if i can say the true i like not to be 100% grammar correct...
it is strange to think that?


[toc] | [prev] | [next] | [standalone]


#12822

FromBarry Schwarz <schwarzb@dqel.com>
Date2011-11-06 09:59 -0800
Message-ID<hcidb7psajio3qffu169hcj3imn478ianr@4ax.com>
In reply to#12816
On Sun, 6 Nov 2011 18:07:05 +0100, "io_x" <a@b.c.invalid> wrote:

>
>"io_x" <a@b.c.invalid> ha scritto nel messaggio
>news:4eb38450$0$1380$4fafbaef@reader2.news.tin.it...
>
>when i see my routines i note this
>
>>> /* Safely collect a string (due to Jack Klein) */
>>> char * getsafe (char *buffer, int count)
>>> {
>>>    char *result = buffer, *np;
>>>    if ((buffer == NULL) || (count < 1))
>>>        result = NULL;
>>>    else if (count == 1)
>>>        *result = '\0';
>>>    else if ((result = fgets (buffer, count, stdin)) != NULL)
>>>        if (np = strchr (buffer, '\n'))
>>>            *np = '\0';
>>>    return result;
>>> }
>
>it seem to me that if someone insert  stringInput<EOF>
>than stringInput is a valid line even if not has "\n"
>the above function not get that string
>because return NULL on that input

Check 7.19.7.2-3 again.  Look at the second sentence carefully.

>
>there is someone out there? It seem the only response to my posts
>is by Ian Collins that i plonk some time ago [i see that trhu
>google groups] so i can not answer
>
>possibly *i'm* in the KF of many in the group for my strange
>behaviour too
>if i can say the true i like not to be 100% grammar correct...
>it is strange to think that?

It's not your grammar.

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#12824

From"io_x" <a@b.c.invalid>
Date2011-11-06 19:23 +0100
Message-ID<4eb6cfde$0$1388$4fafbaef@reader1.news.tin.it>
In reply to#12822
"Barry Schwarz" <schwarzb@dqel.com> ha scritto nel messaggio
news:hcidb7psajio3qffu169hcj3imn478ianr@4ax.com...
> On Sun, 6 Nov 2011 18:07:05 +0100, "io_x" <a@b.c.invalid> wrote:
>
>>
>>"io_x" <a@b.c.invalid> ha scritto nel messaggio
>>news:4eb38450$0$1380$4fafbaef@reader2.news.tin.it...
>>
>>when i see my routines i note this
>>
>>>> /* Safely collect a string (due to Jack Klein) */
>>>> char * getsafe (char *buffer, int count)
>>>> {
>>>>    char *result = buffer, *np;
>>>>    if ((buffer == NULL) || (count < 1))
>>>>        result = NULL;
>>>>    else if (count == 1)
>>>>        *result = '\0';
>>>>    else if ((result = fgets (buffer, count, stdin)) != NULL)
>>>>        if (np = strchr (buffer, '\n'))
>>>>            *np = '\0';
>>>>    return result;
>>>> }
>>
>>it seem to me that if someone insert  stringInput<EOF>
>>than stringInput is a valid line even if not has "\n"
>>the above function not get that string
>>because return NULL on that input
>
> Check 7.19.7.2-3 again.  Look at the second sentence carefully.

whitout check "7.19.7.2-3" i think i know what the problem is:
below i rewrite something i wrote in one post
about the above function:
Message-ID: <4eb6c450$0$1393$4fafbaef@reader1.news.tin.it>
------------------------------------
i realize it is as fgets(); the return value is NULL but the
string is took [or get]

what i had to say it was that the function above
could not get a line in the case strlen(line)>count
so for me it could be not safe
if for eaxample count is  500 and i insert a 1000 chars number
the line is broken in two part
the first 500 chars and the second 500 chars
----------------------------------------------

>>there is someone out there? It seem the only response to my posts
>>is by Ian Collins that i plonk some time ago [i see that trhu
>>google groups] so i can not answer
>>
>>possibly *i'm* in the KF of many in the group for my strange
>>behaviour too
>>if i can say the true i like not to be 100% grammar correct...
>>it is strange to think that?
>
> It's not your grammar.

it is not a problem of grammar, [yes it is not my grammar first language]
but errors i do are so too much i not worry to correct them :)

> -- 
> Remove del for email


[toc] | [prev] | [next] | [standalone]


Page 7 of 8 — ← Prev page 1 2 3 4 5 6 [7] 8  Next page →

Back to top | Article view | comp.lang.c


csiph-web