473,662 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weird FOR LOOP problem

Bo
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"?
Jul 19 '05 #1
14 3578
"Bo" <sn*****@hotmai l.com> wrote in message
news:4f******** *************** ***@posting.goo gle.com...
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.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.
Jul 19 '05 #2
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
Jul 19 '05 #3
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
Jul 19 '05 #4
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
Jul 19 '05 #5
"Bo" <sn*****@hotmai l.com> wrote in message
news:4f******** *************** ***@posting.goo gle.com...

[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
Jul 19 '05 #6
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


Jul 19 '05 #7
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
Jul 19 '05 #8
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 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.
>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

Jul 19 '05 #9
Bo
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" <sg*******@dial .pipex.com> wrote in message news:<3f******* *************** *@news.dial.pip ex.com>...
"Bo" <sn*****@hotmai l.com> wrote in message
news:4f******** *************** ***@posting.goo gle.com...
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.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.

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
14608
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is a total mistery. I have a function inside an ASP page as a result of which I get the following error message: Microsoft VBScript compilation error '800a03ea'
1
2101
by: Kaneda | last post by:
Hello everyone! I have some weird(?) problems, and I am not quite sure if there are due to my errors or maybe a limitation in the .Net framework. I have a ComboBox I need to fill with the content of an untyped DataSet. This is to be done in the "DropDown" Event (since the dataset is empty at program start). If I go with this:
0
1151
by: Kaneda | last post by:
Hello everyone! I have some weird(?) problems, and I am not quite sure if there are due to my errors or maybe a limitation in the .Net framework. I have a ComboBox I need to fill with the content of an untyped DataSet. This is to be done in the "DropDown" Event (since the dataset is empty at program start). If I go with this:
13
1975
by: Dmitry Tkach | last post by:
Hi, everybody! Here is a weird problem, I ran into... I have two huge (80 million rows each) tables (a and b), with id as a PK on both of them and also an FK from b referencing a. When I try to run a query like: select * from a, b where a.id >= 7901288 and a.id=b.id limit 1; The query takes *forever*.
8
2388
by: Jeff | last post by:
Hello everybody, I was doing one of the exercises in the K&R book, and I got something really strange. Here's the source code: /* * Exercise 2-2 from the K&R book, page 42 */ #include <stdio.h>
8
1537
by: Daniel Yelland | last post by:
Hi, I have developed a number of code libraries in Win32 DLLs and have written a number of test suite executables that implicitly link to these libraries in order to test them. In one of my test applications, which runs fine in Debug mode, it is crashing in the destructor of a local object on the stack when it is built in release mode. An example of the C++ that causes the problem is as follows (apologies for the contrived example): -
1
1173
by: John | last post by:
Hi Everyone, I have a strange little problem which I am having difficulty overcoming. I have a loop in ASP: <% For x=0 to 10 Response.Write("Some text...") Next %>
36
1556
by: Scott | last post by:
Hi, Now this one does not make any sense to me at all. I have a while loop and it is not working anything like it is suppose to. I have placed it below. ------------------------- Count = 1; while(count < 4)
0
981
by: berry | last post by:
Hi... I very need yours help. I having a very weird problem. I want to consider the datagrid is empty or not, then if not empty, i want to total up the value. I using datagrid.text <> "" in my code. Overall i have 3datagrids. The program run smoothly at the first 2datagrids. But when reach to the third datagrids, It occurs error "data access error". What is the proint?? Please help me! Thank you.. If datagrid1.Text <> "" Then ...
6
1901
by: Emiurgo | last post by:
Hi there to everyone! I've got a problem which may be interesting to some of you, and I'd be very grateful if there is someone who can give me some advice (or maybe redirect me to some other place). Very brief introduction. Since I'm learning vectorization with SSE (I use gcc 4.1.3 on Ubuntu 7.10) I've built a simple test program in C for comparing running times. I wanted to see how much improvement could give vectorization to simple...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.