奇怪的FOR LOOP问题 [英] weird FOR LOOP problem

查看:65
本文介绍了奇怪的FOR LOOP问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有这个代码时,a和b'的值永远不会增加。


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

}


然而这是有效的:


double a = 0.0,b = 0.0;


for(int i = 0; i< 100; a + = 0.1,b + = 0.2)

{

printf("%s \ n" ,我+ a + b);

}


我使用visual c ++ 6.0


该死的花了我四个小时才赶上这个。当然,我的原始代码

更加复杂。


有没有办法将文本打印到控制台窗口,即使你的项目

是win32应用程序?

解决方案

" Bo" < SN ***** @ hotmail.com>在消息中写道

新闻:4f ************************** @ posting.google.c om ...

当我有这个代码时,a和b'的值永远不会增加。

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


它甚至不应该编译。在VC ++ 6上,它给出了一个警告(坦率地说,它应该给出一个错误,但它总比没有好)。为什么你忽略了以下




警告C4518:''double'':存储类或类型说明符意外
这里
;忽略





出于好奇......


FWIW,之所以如此值永远不会增加的是a和b都是类型

int。如果你把int加0.1,你会得到什么?同样的int又回来了,

因此你遇到的问题。

然而这是有效的:

double a = 0.0,b对于(int i = 0; i <100; a + = 0.1,b + = 0.2)

printf("%s \ n,= 0.0;

i + a + b);
}


它应该这样做。

我使用visual c ++ 6.0

该死的我花了四个小时才赶上这个。当然,我的原始代码
要复杂得多。


这个故事的寓意是:阅读编译器警告,了解它们,并且

对它们采取行动。如果你找不到编译器告诉你的错误,那么你将永远找不到真正令人讨厌的,模糊不清的错误。这些警告是因为

a的原因(好吧,除了关于在

调试器中截断长标识符的问题,这是一个非常毫无意义的警告)。

有没有办法将文本打印到控制台窗口,即使你的项目是win32应用程序?




是的。然而,它在C ++语言新闻组中完全偏离主题。你想在microsoft.public.vc.language中询问

。 FWIW:


< OT>

查找CreateConsoleScreenBuffer"在MSDN中查看相关的

控制台功能 (链接在页面底部)。

< / OT>


HTH,


Stuart 。


Buster Copley写道:

Bo写道:

当我有这个代码时,a和b'' s值永远不会增加。

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



这是语法错误。




这不是语法错误。这是一个语义错误。 i + a + b不是字符串。

但是C ++语言语法并不关心格式字符串。

但这有效:

对于(int i = 0; i <100; a + = 0.1,b + = 0.2),a = 0.0,b = 0.0;


{
printf("%s \ n",i + a + b);
}



是的。




是什么?

我用visual c ++ 6.0

该死的花了我四个小时来抓住这个。当然,我的原始代码
要复杂得多。



为什么要告诉我们这个?




因为他需要帮助吗?

有没有办法将文本打印到控制台窗口,即使你的项目是win32应用程序 ;?



谁在乎?




显然你不是。那你为什么不回到你所做的任何事情?

来到这里侮辱人们?


-

Attila aka WW


Bo写道:

当我有这个代码时,a和b'的值永远不会增加。 />
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]


表达式i + a + b的类型是double。您尝试打印字符串(键入

chat *)。你很幸运它没有崩溃。使用printf格式

适用于浮点数,而不是%s。

有没有办法将文本打印到控制台窗口,即使你的项目也是如此
是win32应用程序?




请将此问题发布到Windows编程新闻组。这个是仅用于标准C ++语言的



-

Attila aka WW


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

解决方案

"Bo" <sn*****@hotmail.com> wrote in message
news:4f**************************@posting.google.c 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.


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


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


这篇关于奇怪的FOR LOOP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆