Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > weird FOR LOOP problem

Reply
Thread Tools

weird FOR LOOP problem

 
 
Bo
Guest
Posts: n/a
 
      08-21-2003
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" <> wrote in message news:<3f44b21b$0$15040$ m>...
> "Bo" <> wrote in message
> news: om...
> > 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 );
> > }

>
> 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.
>
> > 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 );
> > }

>
> It should do.
>
> > 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.

>
> 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).
>
> > Is there a way to print text to a console window even if your project
> > is "win32 application"?

>
> Yes. It's entirely off-topic in a C++ language newsgroup, however. You want
> to ask this in microsoft.public.vc.language. FWIW:
>
> <OT>
> Look up "CreateConsoleScreenBuffer" in MSDN and check out the related
> "Console Functions" (link at the bottom of the page).
> </OT>
>
> HTH,
>
> Stuart.

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      08-21-2003

Ron Natalie <> wrote in message
news:3f453d89$0$17694$ m...
>
> "Bo" <> wrote in message

news: om...
> > 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)

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


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

-Mike



 
Reply With Quote
 
 
 
 
Attila Feher
Guest
Posts: n/a
 
      08-22-2003
Buster Copley wrote:
[SNIP]
> As far as I can see the guy's a troll.


Then you have just fed one. Very clever.

> His post is ridiculous, so I
> guess I did ridicule it a little.


You have way too much time on your hands.

> To the OP, if you were serious,
> sorry for my misinterpretation, and please read the FAQ before
> posting.


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?

> Mr Feher, if you're still annoyed that I disagreed with your reply to
> another post today then say so. Otherwise calm down.


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]
> I read my lines, all four of them. I still don't see an insult.


They are unhelpful, uncessary and hostile for absolutely no reason.

> This newsgroup is a public forum. I'll write what I please.


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++?

> I thought
> "Who cares?" was appropriate for an off-topic question,


It was not. Read the FAQ.

> even if it is supremely unhelpful.


Your whole post is extremely unhelpful, waste of bandwidhh and every readers
time.

> It's not much worse than "Do it yourself.


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.

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


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.

> > So far it is the charter and the FAQ which tells what
> > can be posted here.

>
> OK.


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


 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      08-22-2003
"Bo" <> wrote in message
news: om...
> 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)


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)

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


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

> Thanks.
>
>
> > ?
> >
> > 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.
> >
> > > 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 );
> > > }

> >
> > It should do.
> >
> > > 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.

> >
> > 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).
> >
> > > Is there a way to print text to a console window even if your project
> > > is "win32 application"?

> >
> > Yes. It's entirely off-topic in a C++ language newsgroup, however. You

want
> > to ask this in microsoft.public.vc.language. FWIW:
> >
> > <OT>
> > Look up "CreateConsoleScreenBuffer" in MSDN and check out the related
> > "Console Functions" (link at the bottom of the page).
> > </OT>
> >
> > HTH,
> >
> > Stuart.



 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      08-22-2003
"Dave O'Hearn" <> wrote in message
news: om...
> (Bo) wrote:

<snip>
> In C++, it is usually easier to use iostreams, so you don't have to
> remember the %d and %f things,
>
> #include <iostreams>


ITYM: #include <iostream>

HTH,

Stuart.

> // ... etc ...
> std::cout << i << " " << a << " " << b << std::endl;
>

<snip>
> --
> Dave O'Hearn



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: A Weird Appearance for a Weird Site dorayme HTML 1 01-21-2011 06:51 AM
Re: A Weird Appearance for a Weird Site richard HTML 0 01-21-2011 06:46 AM
newbie with a weird technical problem (@ least I think it's weird) will Ruby 6 12-27-2006 04:46 PM
weird FOR LOOP problem (solved) Bo C++ 2 08-22-2003 10:44 AM



Advertisments