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
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"?
 
Reply With Quote
 
 
 
 
Stuart Golodetz
Guest
Posts: n/a
 
      08-21-2003
"Bo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
 
 
 
 
Attila Feher
Guest
Posts: n/a
 
      08-21-2003
Buster Copley wrote:
> Bo wrote:
>> 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 );
>> }

>
> That's a syntax error.


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.

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

>
> Yes.


Yes what?

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

>
> Why are you telling us this?


Because he needs help?

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

--
Attila aka WW


 
Reply With Quote
 
Attila Feher
Guest
Posts: n/a
 
      08-21-2003
Bo wrote:
> 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 );

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

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


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

--
Attila aka WW


 
Reply With Quote
 
Attila Feher
Guest
Posts: n/a
 
      08-21-2003
Attila Feher wrote:
> Bo wrote:
>> 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 );

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


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

--
Attila aka WW


 
Reply With Quote
 
ES Kim
Guest
Posts: n/a
 
      08-21-2003
"Bo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...

[snip]

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

[snip]

--
ES Kim


 
Reply With Quote
 
Buster Copley
Guest
Posts: n/a
 
      08-21-2003
Attila Feher wrote:
> Buster Copley wrote:
>>Bo wrote:
>>
>>>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 );
>>>}

>>
>>That's a syntax error.

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


The compiler knows the syntax of a declaration, though.

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

>>
>>Yes.

>
> Yes what?


Yes it works, I meant, although as ES Kim has pointed out, the loop
variable is never incremented and the loop never exits.

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

>>
>>Why are you telling us this?

>
> Because he needs help?


Then he shouldn't post this nonsense code for no reason then ask an
unrelated and off-topic question.

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


What insult, exactly?

> --
> Attila aka WW


 
Reply With Quote
 
White Wolf
Guest
Posts: n/a
 
      08-21-2003
Buster Copley wrote:
[SNIP]
>>> Why are you telling us this?

>>
>> Because he needs help?

>
> Then he shouldn't post this nonsense code for no reason then ask an
> unrelated and off-topic question.


Because he need help?

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

>
> What insult, exactly?


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


 
Reply With Quote
 
Buster Copley
Guest
Posts: n/a
 
      08-21-2003
White Wolf wrote:
> Buster Copley wrote:
> [SNIP]
>
>>>>Why are you telling us this?
>>>
>>>Because he needs help?

>>
>>Then he shouldn't post this nonsense code for no reason then ask an
>>unrelated and off-topic question.

>
>
> Because he need help?


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

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

>>
>>What insult, exactly?

>
> Read your lines again. And read the FAQ on how to respond to

off-topic _if_
> you respond at all.


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.

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


OK.
Buster

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-21-2003
Bo wrote:
> 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 );
> }


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.

 
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