weird FOR LOOP problem

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

    weird FOR LOOP problem

    When I had this code, a and b's value never increases.

    for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
    {
    printf( "%s\n", i+a+b );
    }

    This works, however:

    double a=0.0, b=0.0;

    for( int i=0 ; i<100; a+=0.1, b+=0.2 )
    {
    printf( "%s\n", i+a+b );
    }

    I used visual c++ 6.0

    Damn it took me four hours to catch this. Of course, my original code
    is whole lot more complicated.

    Is there a way to print text to a console window even if your project
    is "win32 application"?
  • Stuart Golodetz

    #2
    Re: weird FOR LOOP problem

    "Bo" <snarlly@hotmai l.com> wrote in message
    news:4f190004.0 308210219.28882 a26@posting.goo gle.com...[color=blue]
    > When I had this code, a and b's value never increases.
    >
    > for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
    > {
    > printf( "%s\n", i+a+b );
    > }[/color]

    It shouldn't even compile. On VC++ 6, it gives a warning (frankly, it should
    give an error, but it's better than nothing). Why did you ignore the
    following:

    warning C4518: 'double ' : storage-class or type specifier(s) unexpected
    here; ignored

    ?

    Just out of curiosity...

    FWIW, the reason the values never increase is that a and b are both of type
    int. If you add 0.1 to an int, what do you get? The same int back again,
    hence the problem you're experiencing.
    [color=blue]
    > This works, however:
    >
    > double a=0.0, b=0.0;
    >
    > for( int i=0 ; i<100; a+=0.1, b+=0.2 )
    > {
    > printf( "%s\n", i+a+b );
    > }[/color]

    It should do.
    [color=blue]
    > I used visual c++ 6.0
    >
    > Damn it took me four hours to catch this. Of course, my original code
    > is whole lot more complicated.[/color]

    And the moral of this story is: read compiler warnings, understand them, and
    act on them. If you can't find the bugs your compiler is telling you about,
    you'll never find the really nasty, obscure ones. The warnings are there for
    a reason (well, except the one about truncation of long identifiers in the
    debugger, which is a really pointless warning).
    [color=blue]
    > Is there a way to print text to a console window even if your project
    > is "win32 application"?[/color]

    Yes. It's entirely off-topic in a C++ language newsgroup, however. You want
    to ask this in microsoft.publi c.vc.language. FWIW:

    <OT>
    Look up "CreateConsoleS creenBuffer" in MSDN and check out the related
    "Console Functions" (link at the bottom of the page).
    </OT>

    HTH,

    Stuart.


    Comment

    • Attila Feher

      #3
      Re: weird FOR LOOP problem

      Buster Copley wrote:[color=blue]
      > Bo wrote:[color=green]
      >> When I had this code, a and b's value never increases.
      >>
      >> for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
      >> {
      >> printf( "%s\n", i+a+b );
      >> }[/color]
      >
      > That's a syntax error.[/color]

      That is not a syntax error. It is a semantic error. i+a+b is not a string.
      But the C++ language syntax does not care about the format string.
      [color=blue][color=green]
      >> This works, however:
      >>
      >> double a=0.0, b=0.0;
      >>
      >> for( int i=0 ; i<100; a+=0.1, b+=0.2 )
      >> {
      >> printf( "%s\n", i+a+b );
      >> }[/color]
      >
      > Yes.[/color]

      Yes what?
      [color=blue][color=green]
      >> I used visual c++ 6.0
      >>
      >> Damn it took me four hours to catch this. Of course, my original code
      >> is whole lot more complicated.[/color]
      >
      > Why are you telling us this?[/color]

      Because he needs help?
      [color=blue][color=green]
      >> Is there a way to print text to a console window even if your project
      >> is "win32 application"?[/color]
      >
      > Who cares?[/color]

      Apparently you don't. So why don't you just go back to whatever you did
      before you came in here to insult people?

      --
      Attila aka WW


      Comment

      • Attila Feher

        #4
        Re: weird FOR LOOP problem

        Bo wrote:[color=blue]
        > When I had this code, a and b's value never increases.
        >
        > for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
        > {
        > printf( "%s\n", i+a+b );[/color]
        [SNIP]

        The type of the expression i+a+b is double. You try to print a string (type
        chat *). You are lucky it does not crash. Use the printf format
        appropriate for floating point numbers, not the %s.
        [color=blue]
        > Is there a way to print text to a console window even if your project
        > is "win32 application"?[/color]

        Please post this question to a Windows programming newsgroup. This one is
        only for the standard C++ language.

        --
        Attila aka WW


        Comment

        • Attila Feher

          #5
          Re: weird FOR LOOP problem

          Attila Feher wrote:[color=blue]
          > Bo wrote:[color=green]
          >> When I had this code, a and b's value never increases.
          >>
          >> for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
          >> {
          >> printf( "%s\n", i+a+b );[/color]
          > [SNIP]
          >
          > The type of the expression i+a+b is double. You try to print a
          > string (type chat *). You are lucky it does not crash. Use the
          > printf format appropriate for floating point numbers, not the %s.[/color]

          Correction: Read Stuarts answer. I have apparently skipped the head of the
          for loop.

          --
          Attila aka WW


          Comment

          • ES Kim

            #6
            Re: weird FOR LOOP problem

            "Bo" <snarlly@hotmai l.com> wrote in message
            news:4f190004.0 308210219.28882 a26@posting.goo gle.com...

            [snip]
            [color=blue]
            > This works, however:
            >
            > double a=0.0, b=0.0;
            >
            > for( int i=0 ; i<100; a+=0.1, b+=0.2 )
            > {
            > printf( "%s\n", i+a+b );
            > }[/color]

            It works forever.

            [snip]

            --
            ES Kim


            Comment

            • Buster Copley

              #7
              Re: weird FOR LOOP problem

              Attila Feher wrote:[color=blue]
              > Buster Copley wrote:[color=green]
              >>Bo wrote:
              >>[color=darkred]
              >>>When I had this code, a and b's value never increases.
              >>>
              >>>for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
              >>>{
              >>>printf( "%s\n", i+a+b );
              >>>}[/color]
              >>
              >>That's a syntax error.[/color]
              >
              > That is not a syntax error. It is a semantic error. i+a+b is not a string.
              > But the C++ language syntax does not care about the format string.[/color]

              The compiler knows the syntax of a declaration, though.
              [color=blue][color=green][color=darkred]
              >>>This works, however:
              >>>
              >>>double a=0.0, b=0.0;
              >>>
              >>>for( int i=0 ; i<100; a+=0.1, b+=0.2 )
              >>>{
              >>>printf( "%s\n", i+a+b );
              >>>}[/color]
              >>
              >>Yes.[/color]
              >
              > Yes what?[/color]

              Yes it works, I meant, although as ES Kim has pointed out, the loop
              variable is never incremented and the loop never exits.
              [color=blue][color=green][color=darkred]
              >>>I used visual c++ 6.0
              >>>
              >>>Damn it took me four hours to catch this. Of course, my original code
              >>>is whole lot more complicated.[/color]
              >>
              >>Why are you telling us this?[/color]
              >
              > Because he needs help?[/color]

              Then he shouldn't post this nonsense code for no reason then ask an
              unrelated and off-topic question.
              [color=blue][color=green][color=darkred]
              >>>Is there a way to print text to a console window even if your project
              >>>is "win32 application"?[/color]
              >>
              >>Who cares?[/color]
              >
              > Apparently you don't. So why don't you just go back to whatever you did
              > before you came in here to insult people?[/color]

              What insult, exactly?
              [color=blue]
              > --
              > Attila aka WW[/color]

              Comment

              • White Wolf

                #8
                Re: weird FOR LOOP problem

                Buster Copley wrote:
                [SNIP][color=blue][color=green][color=darkred]
                >>> Why are you telling us this?[/color]
                >>
                >> Because he needs help?[/color]
                >
                > Then he shouldn't post this nonsense code for no reason then ask an
                > unrelated and off-topic question.[/color]

                Because he need help?
                [color=blue][color=green][color=darkred]
                >>>> Is there a way to print text to a console window even if your
                >>>> project
                >>>> is "win32 application"?
                >>>
                >>> Who cares?[/color]
                >>
                >> Apparently you don't. So why don't you just go back to whatever you
                >> did before you came in here to insult people?[/color]
                >
                > What insult, exactly?[/color]

                Read your lines again. And read the FAQ on how to respond to off-topic _if_
                you respond at all. So far it is the charter and the FAQ which tells what
                can be posted here.

                --
                WW aka Attila


                Comment

                • Buster Copley

                  #9
                  [OT] weird FOR LOOP problem

                  White Wolf wrote:[color=blue]
                  > Buster Copley wrote:
                  > [SNIP]
                  >[color=green][color=darkred]
                  >>>>Why are you telling us this?
                  >>>
                  >>>Because he needs help?[/color]
                  >>
                  >>Then he shouldn't post this nonsense code for no reason then ask an
                  >>unrelated and off-topic question.[/color]
                  >
                  >
                  > Because he need help?[/color]

                  As far as I can see the guy's a troll. His post is ridiculous, so I
                  guess I did ridicule it a little. To the OP, if you were serious,
                  sorry for my misinterpretati on, and please read the FAQ before posting.

                  Mr Feher, if you're still annoyed that I disagreed with your reply to
                  another post today then say so. Otherwise calm down.
                  [color=blue][color=green][color=darkred]
                  >>>>>Is there a way to print text to a console window even if your
                  >>>>>project
                  >>>>>is "win32 application"?
                  >>>>
                  >>>>Who cares?
                  >>>
                  >>>Apparently you don't. So why don't you just go back to whatever you
                  >>>did before you came in here to insult people?[/color]
                  >>
                  >>What insult, exactly?[/color]
                  >
                  > Read your lines again. And read the FAQ on how to respond to[/color]
                  off-topic _if_[color=blue]
                  > you respond at all.[/color]

                  I read my lines, all four of them. I still don't see an insult.

                  This newsgroup is a public forum. I'll write what I please. I thought
                  "Who cares?" was appropriate for an off-topic question, even if it is
                  supremely unhelpful. It's not much worse than "Do it yourself." [FAQ
                  5.3]. "Go back to whatever you did before you came in here" I find
                  offensive, but I support your right to say it if you must.
                  [color=blue]
                  > So far it is the charter and the FAQ which tells what
                  > can be posted here.[/color]

                  OK.
                  Buster

                  Comment

                  • Bo

                    #10
                    Re: weird FOR LOOP problem

                    yeah, I forgot the i++.

                    Sorry for introducing these erraneously bad examples. Regardless of
                    its erraneous syntax, I still don't see the for loop prob.

                    Thanks.

                    "Stuart Golodetz" <sgolodetz@dial .pipex.com> wrote in message news:<3f44b21b$ 0$15040$cc9e4d1 f@news.dial.pip ex.com>...[color=blue]
                    > "Bo" <snarlly@hotmai l.com> wrote in message
                    > news:4f190004.0 308210219.28882 a26@posting.goo gle.com...[color=green]
                    > > When I had this code, a and b's value never increases.
                    > >
                    > > for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
                    > > {
                    > > printf( "%s\n", i+a+b );
                    > > }[/color]
                    >
                    > It shouldn't even compile. On VC++ 6, it gives a warning (frankly, it should
                    > give an error, but it's better than nothing). Why did you ignore the
                    > following:
                    >
                    > warning C4518: 'double ' : storage-class or type specifier(s) unexpected
                    > here; ignored
                    >
                    > ?
                    >
                    > Just out of curiosity...
                    >
                    > FWIW, the reason the values never increase is that a and b are both of type
                    > int. If you add 0.1 to an int, what do you get? The same int back again,
                    > hence the problem you're experiencing.
                    >[color=green]
                    > > This works, however:
                    > >
                    > > double a=0.0, b=0.0;
                    > >
                    > > for( int i=0 ; i<100; a+=0.1, b+=0.2 )
                    > > {
                    > > printf( "%s\n", i+a+b );
                    > > }[/color]
                    >
                    > It should do.
                    >[color=green]
                    > > I used visual c++ 6.0
                    > >
                    > > Damn it took me four hours to catch this. Of course, my original code
                    > > is whole lot more complicated.[/color]
                    >
                    > And the moral of this story is: read compiler warnings, understand them, and
                    > act on them. If you can't find the bugs your compiler is telling you about,
                    > you'll never find the really nasty, obscure ones. The warnings are there for
                    > a reason (well, except the one about truncation of long identifiers in the
                    > debugger, which is a really pointless warning).
                    >[color=green]
                    > > Is there a way to print text to a console window even if your project
                    > > is "win32 application"?[/color]
                    >
                    > Yes. It's entirely off-topic in a C++ language newsgroup, however. You want
                    > to ask this in microsoft.publi c.vc.language. FWIW:
                    >
                    > <OT>
                    > Look up "CreateConsoleS creenBuffer" in MSDN and check out the related
                    > "Console Functions" (link at the bottom of the page).
                    > </OT>
                    >
                    > HTH,
                    >
                    > Stuart.[/color]

                    Comment

                    • Kevin Goodsell

                      #11
                      Re: weird FOR LOOP problem

                      Bo wrote:[color=blue]
                      > When I had this code, a and b's value never increases.
                      >
                      > for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
                      > {
                      > printf( "%s\n", i+a+b );
                      > }[/color]

                      As others have pointed out, there are numerous problems here.

                      My advice is to not use printf at all. You clearly don't understand its
                      use well enough to use it correctly. That's OK, most programmers don't.
                      That's why most (all) programmers should use std::cout instead (unless
                      they have a good reason not to).

                      -Kevin
                      --
                      My email address is valid, but changes periodically.
                      To contact me please use the address from a recent posting.

                      Comment

                      • Mike Wahler

                        #12
                        Re: weird FOR LOOP problem


                        Ron Natalie <ron@sensor.com > wrote in message
                        news:3f453d89$0 $17694$9a6e19ea @news.newshosti ng.com...[color=blue]
                        >
                        > "Bo" <snarlly@hotmai l.com> wrote in message[/color]
                        news:4f190004.0 308211340.54c37 39f@posting.goo gle.com...[color=blue][color=green]
                        > > Actually, supposedly, a, b are double:
                        > >
                        > > for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
                        > >
                        > > Do you mean a, b are automatically converted to int because they were
                        > > declared after "int i=0,"? (notice the comma)[/color]
                        >
                        > You can't do that in C++. You can only have one type in a declaration
                        > statement. VC++ seems to just pitch the "double" that's syntactically
                        > incorrect.[/color]

                        My VC++ (6.0) bitches about it, and aborts translation.

                        -Mike



                        Comment

                        • Attila Feher

                          #13
                          Re: [OT] weird FOR LOOP problem

                          Buster Copley wrote:
                          [SNIP][color=blue]
                          > As far as I can see the guy's a troll.[/color]

                          Then you have just fed one. Very clever.
                          [color=blue]
                          > His post is ridiculous, so I
                          > guess I did ridicule it a little.[/color]

                          You have way too much time on your hands.
                          [color=blue]
                          > To the OP, if you were serious,
                          > sorry for my misinterpretati on, and please read the FAQ before
                          > posting.[/color]

                          Instead of being mean (hostile), unhelpful and unpolite, why didn't you just
                          tell the same (as the FAQ, as the Netiquette etc. dictates). Testosteron?
                          Too much cofee?
                          [color=blue]
                          > Mr Feher, if you're still annoyed that I disagreed with your reply to
                          > another post today then say so. Otherwise calm down.[/color]

                          As I have already explained in another post: thanks, but no thanks. I do
                          not need therapy from you. BTW I am not annoyed. Surprised - maybe.

                          [SNIP][color=blue]
                          > I read my lines, all four of them. I still don't see an insult.[/color]

                          They are unhelpful, uncessary and hostile for absolutely no reason.
                          [color=blue]
                          > This newsgroup is a public forum. I'll write what I please.[/color]

                          Not quite. There is a charter, a FAQ etc. for this newsgroup. Anyway there
                          is a question in my mind so I spit it out. I have not been able to use the
                          newsgroup for a while. When did you elect yourself to be the most
                          influential person in comp.lang.c++?
                          [color=blue]
                          > I thought
                          > "Who cares?" was appropriate for an off-topic question,[/color]

                          It was not. Read the FAQ.
                          [color=blue]
                          > even if it is supremely unhelpful.[/color]

                          Your whole post is extremely unhelpful, waste of bandwidhh and every readers
                          time.
                          [color=blue]
                          > It's not much worse than "Do it yourself.[/color]

                          Much worse. The "Do it yourself" is about people apparently copy-pasting
                          their assignment here and wait for an answer. Your humiliating words were
                          (possibly) hitting a beginner who has done (possibly) nothing wrong, but
                          being a beginner. If it was not one but a troll: you have just wasted your
                          time to keep that troll alive by feeding it.
                          [color=blue]
                          >" [FAQ
                          > 5.3]. "Go back to whatever you did before you came in here" I find
                          > offensive, but I support your right to say it if you must.[/color]

                          After an unhelpful and hostile post you came up with I thought that you must
                          have just got here from abusing cats or something like that. So I have felt
                          that my reminding you of your previous activities might save the community
                          from such mean postings in the future by driving you back to the act where
                          you can "first-hand" see the humiliation you cause by your mean words/act
                          and not only imagine it.
                          [color=blue][color=green]
                          > > So far it is the charter and the FAQ which tells what
                          > > can be posted here.[/color]
                          >
                          > OK.[/color]

                          So read it and use it. And one other suggestion: try to keep that "petting
                          on the back" or "Bo gives you therapy" part from your posts. First of all
                          you fire way off target with your assumptions, second: it does not really
                          belong to the topic of the newsgroup.

                          --
                          Attila aka WW


                          Comment

                          • Stuart Golodetz

                            #14
                            Re: weird FOR LOOP problem

                            "Bo" <snarlly@hotmai l.com> wrote in message
                            news:4f190004.0 308211340.54c37 39f@posting.goo gle.com...[color=blue]
                            > Actually, supposedly, a, b are double:
                            >
                            > for( int i=0, double a=0.0, double b=0.0 ; i<100; a+=0.1, b+=0.2 )
                            >
                            > Do you mean a, b are automatically converted to int because they were
                            > declared after "int i=0,"? (notice the comma)[/color]

                            VC++ seems to be (strangely) ignoring "double", which is illegal in that
                            context. You can't define two different types of variable in the first bit
                            of a for loop. In other words, these are ok (if rather pointless, since they
                            don't do anything in these examples :-)):

                            for(int i=0; i<10; ++i) {}
                            for(double d=0; d<10; d+=0.1) {}

                            but what you had above isn't.

                            What VC++ is doing is treating what you wrote as if you'd written this:

                            for(int i=0, a=0.0, b=0.0; i<100; a+=0.1, b+=0.2)
                            [color=blue]
                            > Sorry for introducing the line printf("%s\n", i+a+b). As I said my
                            > original code is too complicated so I just used this line without much
                            > thought. Regardless of its erraneous syntax, I still don't see the for
                            > loop prob.[/color]

                            Hopefully the above clarified things a bit? Incidentally, the syntax of the
                            printf line isn't the problem, it's its semantics. In other words, it will
                            compile, but it still won't work.

                            HTH,

                            Stuart.

                            P.S. Apologies for taking a bit of a swipe at you in my original posting
                            about not reading the compiler warning (looking back at my post, it didn't
                            come across as entirely friendly :-)) The advice about reading warnings is
                            worth noting, nonetheless, you can catch a number of bugs that way (and
                            avoid spending lots of time debugging).
                            [color=blue]
                            > Thanks.
                            >
                            >[color=green]
                            > > ?
                            > >
                            > > Just out of curiosity...
                            > >
                            > > FWIW, the reason the values never increase is that a and b are both of[/color][/color]
                            type[color=blue][color=green]
                            > > int. If you add 0.1 to an int, what do you get? The same int back again,
                            > > hence the problem you're experiencing.
                            > >[color=darkred]
                            > > > This works, however:
                            > > >
                            > > > double a=0.0, b=0.0;
                            > > >
                            > > > for( int i=0 ; i<100; a+=0.1, b+=0.2 )
                            > > > {
                            > > > printf( "%s\n", i+a+b );
                            > > > }[/color]
                            > >
                            > > It should do.
                            > >[color=darkred]
                            > > > I used visual c++ 6.0
                            > > >
                            > > > Damn it took me four hours to catch this. Of course, my original code
                            > > > is whole lot more complicated.[/color]
                            > >
                            > > And the moral of this story is: read compiler warnings, understand them,[/color][/color]
                            and[color=blue][color=green]
                            > > act on them. If you can't find the bugs your compiler is telling you[/color][/color]
                            about,[color=blue][color=green]
                            > > you'll never find the really nasty, obscure ones. The warnings are there[/color][/color]
                            for[color=blue][color=green]
                            > > a reason (well, except the one about truncation of long identifiers in[/color][/color]
                            the[color=blue][color=green]
                            > > debugger, which is a really pointless warning).
                            > >[color=darkred]
                            > > > Is there a way to print text to a console window even if your project
                            > > > is "win32 application"?[/color]
                            > >
                            > > Yes. It's entirely off-topic in a C++ language newsgroup, however. You[/color][/color]
                            want[color=blue][color=green]
                            > > to ask this in microsoft.publi c.vc.language. FWIW:
                            > >
                            > > <OT>
                            > > Look up "CreateConsoleS creenBuffer" in MSDN and check out the related
                            > > "Console Functions" (link at the bottom of the page).
                            > > </OT>
                            > >
                            > > HTH,
                            > >
                            > > Stuart.[/color][/color]


                            Comment

                            • Stuart Golodetz

                              #15
                              Re: weird FOR LOOP problem

                              "Dave O'Hearn" <daveoh77@pobox .com> wrote in message
                              news:3e05f9e4.0 308210653.10ec4 ed2@posting.goo gle.com...[color=blue]
                              > snarlly@hotmail .com (Bo) wrote:[/color]
                              <snip>[color=blue]
                              > In C++, it is usually easier to use iostreams, so you don't have to
                              > remember the %d and %f things,
                              >
                              > #include <iostreams>[/color]

                              ITYM: #include <iostream>

                              HTH,

                              Stuart.
                              [color=blue]
                              > // ... etc ...
                              > std::cout << i << " " << a << " " << b << std::endl;
                              >[/color]
                              <snip>[color=blue]
                              > --
                              > Dave O'Hearn[/color]


                              Comment

                              Working...