#include
using namespace std; int main() { int i=1; float s=1,t=1.0,e=1.0; while (t>=1e-4) { s*=i; t=1/s; e+=t; i++; } cout<<"e="<<e<<endl; return 0; } 请问哪里有错呢?
调试了下,下面的代码可以在VC++6.0里正常运行。
#include "stdafx.h"
#include <iostream.h>
//using namespace std; //这句我给注释掉了,否则老报错。
int main()
{
int i=1;
float s=1,t=1.0,e=1.0;
while (t>=1e-4)
{
s*=i;
t=1/s;
e+=t;
i++;
}
cout<<"e="<<e<<endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-01-14
最后一项小于0.0001的t,你也把它加进去了。
第2个回答 2014-01-14
float s=1改为float s=1.0
第3个回答 2018-03-22
还能把名字空间的定义给注释掉?你学过C++?你注释掉后后面的语法为std::cout<<