code question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Collins

    #16
    Re: code question

    Barry Schwarz wrote:
    On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
    <nospam@nspam.i nvalidwrote:
    >
    > I have this code I would like to clean up.
    >>
    >#include <stdio.h>
    >#include <stdlib.h>
    >>
    >int main(int argc, char *argv[])
    >{
    > double x, y, a, b;
    > FILE *fp;
    > x = strtod(argv[1], NULL);
    > y = strtod(argv[2], NULL);
    > a = strtod(argv[3], NULL);
    > b = strtod(argv[4], NULL);
    >
    So the fourth command line argument should be a number.
    >
    > if ((fp = fopen(argv[4], "a")) == NULL) {
    >
    At the same time, it should be a file name.
    >
    That doesn't strike you as slightly odd?
    >
    Most things "Bill" posts are at least slightly odd...

    --
    Ian Collins.

    Comment

    • Bill Cunningham

      #17
      Re: code question


      "Ian Collins" <ian-news@hotmail.co mwrote in message
      news:6jimldF389 73U6@mid.indivi dual.net...
      The how about
      >
      #include <stdio.h>
      #include <stdlib.h>
      >
      int main( int argc, char **argv )
      {
      double data[4] = {0.0};
      >
      if ( argc 5 ) {
      puts("Too many arguments");
      exit(EXIT_FAILU RE);
      }
      >
      unsigned n = 1;
      >
      while ( n < argc ) {
      data[n-1] = strtod(argv[n], NULL);
      ++n;
      [snip]

      What's above is a bit of a different method than I would think of. Is that n
      minus 1 in the brackets?
      Why set n to 1?

      Bill


      Comment

      • Richard

        #18
        Re: code question

        "Bill Cunningham" <nospam@nspam.i nvalidwrites:
        "Richard" <rgrdev@gmail.c omwrote in message
        news:gb1clg$bg$ 1@registered.mo tzarella.org...
        >
        >Did you try it with a debugger? No? Then give up. Not that you need one
        >to spot the error. 10/10 for persistence. O/10 for originality. These
        >posts are getting a tad tiresome.
        >
        So are yours Richard Good God so are yours.
        >
        Have a nice life..
        So, did you use the debugger to show you were it was segment faulting?
        No? Then give up. You are totally useless as a programmer and will never
        make it. Why? Because you refuse to use the tools there to help you do a
        job I offer this advise to you to save you a lot of hurt and pain in the
        future. If it's any consolation I could never be a high wire trapeze
        artist no matter how much I practised.

        And while I am at it, please stop spamming the application development
        groups telling them you are trying to write a printer driver when you
        could not even tell them the type of printer you are using.

        Comment

        • Richard

          #19
          Re: code question

          Barry Schwarz <schwarzb@dqel. comwrites:
          On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
          <nospam@nspam.i nvalidwrote:
          >
          > I have this code I would like to clean up.
          >>
          >>#include <stdio.h>
          >>#include <stdlib.h>
          >>
          >>int main(int argc, char *argv[])
          >>{
          > double x, y, a, b;
          > FILE *fp;
          > x = strtod(argv[1], NULL);
          > y = strtod(argv[2], NULL);
          > a = strtod(argv[3], NULL);
          > b = strtod(argv[4], NULL);
          >
          So the fourth command line argument should be a number.
          >
          > if ((fp = fopen(argv[4], "a")) == NULL) {
          >
          At the same time, it should be a file name.
          >
          That doesn't strike you as slightly odd?
          Not it doesn't because he never reads his own code. He refused to use a
          debugger or debugging methods to see his data in action. In short he is
          either a troll or the worst programmer of all time. And I mean *ALL*
          time.

          Comment

          • Ian Collins

            #20
            Re: code question

            Bill Cunningham wrote:
            "Ian Collins" <ian-news@hotmail.co mwrote in message
            news:6jimldF389 73U6@mid.indivi dual.net...
            >The how about
            >>
            >#include <stdio.h>
            >#include <stdlib.h>
            >>
            >int main( int argc, char **argv )
            >{
            > double data[4] = {0.0};
            >>
            > if ( argc 5 ) {
            > puts("Too many arguments");
            > exit(EXIT_FAILU RE);
            > }
            >>
            > unsigned n = 1;
            >>
            > while ( n < argc ) {
            > data[n-1] = strtod(argv[n], NULL);
            > ++n;
            >
            [snip]
            >
            What's above is a bit of a different method than I would think of. Is that n
            minus 1 in the brackets?
            Why set n to 1?
            >
            What's argv[1]?

            --
            Ian Collins.

            Comment

            • Bill Cunningham

              #21
              Re: code question

              kandr2 is about the toughest reading there is on C. I'll try again in a
              couple of days when my concentration can be focused.

              Bill


              Comment

              • Richard

                #22
                Re: code question

                "Bill Cunningham" <nospam@nspam.i nvalidwrites:
                "Ian Collins" <ian-news@hotmail.co mwrote in message
                news:6jimldF389 73U6@mid.indivi dual.net...
                >The how about
                >>
                >#include <stdio.h>
                >#include <stdlib.h>
                >>
                >int main( int argc, char **argv )
                >{
                > double data[4] = {0.0};
                >>
                > if ( argc 5 ) {
                > puts("Too many arguments");
                > exit(EXIT_FAILU RE);
                > }
                >>
                > unsigned n = 1;
                >>
                > while ( n < argc ) {
                > data[n-1] = strtod(argv[n], NULL);
                > ++n;
                >
                [snip]
                >
                What's above is a bit of a different method than I would think of. Is that n
                minus 1 in the brackets?
                Why set n to 1?
                >
                Bill
                To confuse you I assume. Its not a very C way of doing it IMO. Not
                tested for ISO C correctness or whether it even works but this is much
                clearer if you (and you must as a c programmer) understand for
                loops. You should get the idea.

                for(int n=0;n<argc;n++)
                data[n] = strtod(argv[n+1], NULL);

                Comment

                • Richard

                  #23
                  Re: code question

                  Richard<rgrdev@ gmail.comwrites :
                  "Bill Cunningham" <nospam@nspam.i nvalidwrites:
                  >
                  >"Ian Collins" <ian-news@hotmail.co mwrote in message
                  >news:6jimldF38 973U6@mid.indiv idual.net...
                  >>The how about
                  >>>
                  >>#include <stdio.h>
                  >>#include <stdlib.h>
                  >>>
                  >>int main( int argc, char **argv )
                  >>{
                  >> double data[4] = {0.0};
                  >>>
                  >> if ( argc 5 ) {
                  >> puts("Too many arguments");
                  >> exit(EXIT_FAILU RE);
                  >> }
                  >>>
                  >> unsigned n = 1;
                  >>>
                  >> while ( n < argc ) {
                  >> data[n-1] = strtod(argv[n], NULL);
                  >> ++n;
                  >>
                  >[snip]
                  >>
                  >What's above is a bit of a different method than I would think of. Is that n
                  >minus 1 in the brackets?
                  >Why set n to 1?
                  >>
                  >Bill
                  >
                  To confuse you I assume. Its not a very C way of doing it IMO. Not
                  tested for ISO C correctness or whether it even works but this is much
                  clearer if you (and you must as a c programmer) understand for
                  loops. You should get the idea.
                  >
                  for(int n=0;n<argc;n++)
                  data[n] = strtod(argv[n+1], NULL);
                  ps use a debugger. And tell me my error from posting too quickly
                  there. Hint : look at what is in argv and what you are interested in.

                  Comment

                  • Ian Collins

                    #24
                    Re: code question

                    Richard wrote:
                    "Bill Cunningham" <nospam@nspam.i nvalidwrites:
                    >
                    >"Ian Collins" <ian-news@hotmail.co mwrote in message
                    >news:6jimldF38 973U6@mid.indiv idual.net...
                    >>The how about
                    >>>
                    >>#include <stdio.h>
                    >>#include <stdlib.h>
                    >>>
                    >>int main( int argc, char **argv )
                    >>{
                    >> double data[4] = {0.0};
                    >>>
                    >> if ( argc 5 ) {
                    >> puts("Too many arguments");
                    >> exit(EXIT_FAILU RE);
                    >> }
                    >>>
                    >> unsigned n = 1;
                    >>>
                    >> while ( n < argc ) {
                    >> data[n-1] = strtod(argv[n], NULL);
                    >> ++n;
                    >[snip]
                    >>
                    >What's above is a bit of a different method than I would think of. Is that n
                    >minus 1 in the brackets?
                    >Why set n to 1?
                    >>
                    >
                    To confuse you I assume.
                    Nope, I just posted before refactoring!

                    --
                    Ian Collins.

                    Comment

                    • Richard

                      #25
                      Re: code question

                      Ian Collins <ian-news@hotmail.co mwrites:
                      Richard wrote:
                      >"Bill Cunningham" <nospam@nspam.i nvalidwrites:
                      >>
                      >>"Ian Collins" <ian-news@hotmail.co mwrote in message
                      >>news:6jimldF3 8973U6@mid.indi vidual.net...
                      >>>The how about
                      >>>>
                      >>>#include <stdio.h>
                      >>>#include <stdlib.h>
                      >>>>
                      >>>int main( int argc, char **argv )
                      >>>{
                      >>> double data[4] = {0.0};
                      >>>>
                      >>> if ( argc 5 ) {
                      >>> puts("Too many arguments");
                      >>> exit(EXIT_FAILU RE);
                      >>> }
                      >>>>
                      >>> unsigned n = 1;
                      >>>>
                      >>> while ( n < argc ) {
                      >>> data[n-1] = strtod(argv[n], NULL);
                      >>> ++n;
                      >>[snip]
                      >>>
                      >>What's above is a bit of a different method than I would think of. Is that n
                      >>minus 1 in the brackets?
                      >>Why set n to 1?
                      >>>
                      >>
                      >To confuse you I assume.
                      >
                      Nope, I just posted before refactoring!
                      I apologise that I didn't get the tone right there. I wasn't being
                      insulting to you - I was more hinting that anything could confuse Bill.

                      Comment

                      • Ian Collins

                        #26
                        Re: code question

                        Richard wrote:
                        >
                        for(int n=0;n<argc;n++)
                        data[n] = strtod(argv[n+1], NULL);
                        At lest my "non C" way of doing it didn't have the bug you have...

                        I won't spoil the reader's fun by saying what it is.

                        --
                        Ian Collins.

                        Comment

                        • Ian Collins

                          #27
                          Re: code question

                          Richard wrote:
                          Ian Collins <ian-news@hotmail.co mwrites:
                          >
                          >Richard wrote:
                          >>>>
                          >>To confuse you I assume.
                          >Nope, I just posted before refactoring!
                          >
                          I apologise that I didn't get the tone right there. I wasn't being
                          insulting to you - I was more hinting that anything could confuse Bill.
                          I can't argue with that!

                          --
                          Ian Collins.

                          Comment

                          • Ben Bacarisse

                            #28
                            Re: code question

                            Richard<rgrdev@ gmail.comwrites :
                            "Bill Cunningham" <nospam@nspam.i nvalidwrites:
                            >
                            >"Ian Collins" <ian-news@hotmail.co mwrote in message
                            >news:6jimldF38 973U6@mid.indiv idual.net...
                            <snip>
                            >> unsigned n = 1;
                            >> while ( n < argc ) {
                            >> data[n-1] = strtod(argv[n], NULL);
                            >> ++n;
                            >>
                            >[snip]
                            >>
                            >What's above is a bit of a different method than I would think of. Is that n
                            >minus 1 in the brackets?
                            >Why set n to 1?
                            <snip>
                            To confuse you I assume. Its not a very C way of doing it IMO. Not
                            tested for ISO C correctness or whether it even works but this is much
                            clearer if you (and you must as a c programmer) understand for
                            loops. You should get the idea.
                            >
                            for(int n=0;n<argc;n++)
                            data[n] = strtod(argv[n+1], NULL);
                            Great disclaimer! "This is better (but it may be wrong)". Yes it is
                            (wrong). Here is the flaw with using a debugger. It is easier to
                            think about this code than to paste this into a main function and
                            debug it.

                            --
                            Ben.

                            Comment

                            • Bill Cunningham

                              #29
                              Re: code question

                              Richard wrote:
                              And while I am at it, please stop spamming the application development
                              groups telling them you are trying to write a printer driver when you
                              could not even tell them the type of printer you are using.
                              [Testing out THunderbird]

                              Go back a read the thread for what it says. We were conversing about my
                              printer make and model. I don't need to defend what's posted.
                              >

                              Comment

                              • Bill Cunningham

                                #30
                                Re: code question


                                "Barry Schwarz" <schwarzb@dqel. comwrote in message
                                news:7fe8d4def7 j0qh98uumds7bt8 ae0635kvc@4ax.c om...
                                On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
                                <nospam@nspam.i nvalidwrote:
                                > if ((fp = fopen(argv[4], "a")) == NULL) {
                                >
                                At the same time, it should be a file name.
                                >
                                That doesn't strike you as slightly odd?
                                >
                                A filename input is an argument. I thought you treated it as such. There are
                                many things that kandr2 doesn't teach you. Things like this for example.
                                That why clc is needed.

                                Bill


                                Comment

                                Working...