VC++运行结果不对

渤海红鱼 2008-11-05 11:41:06
这是一个c++的一段程序。我把他写到VC++上的时候发现不能正常的运行了。请大家帮我解决一下。
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"

void main(void)
{
int i;
for (i=0;i<10;i++);
cout <<rand()%100<<" ";
cout<<endl<<endl;

srand (2);
for (i=0;i<10;i++);
cout <<rand()%100<<" ";
cout<<endl<<endl;

srand (1);
for (i=0;i<10;i++);
cout <<rand()%100<<" ";
cout<<endl;
}
...全文
122 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-11-05
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;

void main(void)
{
int i;
for (i=0;i <10;i++);
cout <<rand()%100 <<" ";
cout <<endl <<endl;

srand (2);
for (i=0;i <10;i++);
cout <<rand()%100 <<" ";
cout <<endl <<endl;

srand (1);
for (i=0;i <10;i++);
cout <<rand()%100 <<" ";
cout <<endl;
}
lzh9955 2008-11-05
  • 打赏
  • 举报
回复
#include "stdafx.h
thecrypig 2008-11-05
  • 打赏
  • 举报
回复
把#include "stdafx.h" 注释掉
#include "iostream.h" 改成#include "iostream" using namespace std;
运行正确的啊
liubuweiright 2008-11-05
  • 打赏
  • 举报
回复
#include "stdafx.h" //如果是用VC6.0.这句注释掉

srand(种子)


也不太懂,


ericli981 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 thecrypig 的回复:]
把#include "stdafx.h" 注释掉
#include "iostream.h" 改成#include "iostream"  using namespace std;
运行正确的啊

[/Quote]

没错,不用stdafx.h,使用<iostream>,加上using namespace std;
yang1670414 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhaohongbo83 的回复:]
#include <iostream>
using namespace std;
int main( )
{
int i;
for (i=0;i <10;i++); 分号不要
cout < <rand()%100 < <" ";
cout < <endl < <endl;

srand (2);
for (i=0;i <10;i++); 分号不要
cout < <rand()%100 < <" ";
cout < <endl < <endl;

srand (1);
for (i=0;i <10;i++); 分号不要
cout < <rand()%100 < <" ";
cout < <endl;
return 0;
}

这个运行一下
41


45


41

这个函数的作用是什…
[/Quote]
The srand function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.

wolfdog1111 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhaohongbo83 的回复:]
明白了!
[/Quote]

楼上的怎么回事,给讲下!
wolfdog1111 2008-11-05
  • 打赏
  • 举报
回复
是呀,为什么随机数每次都一样?不要srand还是每次的值都一样?
zhaohongbo83 2008-11-05
  • 打赏
  • 举报
回复
明白了!
zhaohongbo83 2008-11-05
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int main( )
{
int i;
for (i=0;i <10;i++);
cout < <rand()%100 < <" ";
cout < <endl < <endl;

srand (2);
for (i=0;i <10;i++);
cout < <rand()%100 < <" ";
cout < <endl < <endl;

srand (1);
for (i=0;i <10;i++);
cout < <rand()%100 < <" ";
cout < <endl;
return 0;
}

这个运行一下
41


45


41

这个函数的作用是什么?这个随机数是怎么回事?
sc_valentine21 2008-11-05
  • 打赏
  • 举报
回复
建一个应用程序

#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
==
==
==
#include <iostream>
using namespace std;

macfan 2008-11-05
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
int main( )
{
int i;
for (i=0;i <10;i++);
cout < <rand()%100 < <" ";
cout < <endl < <endl;

srand (2);
for (i=0;i <10;i++);
cout < <rand()%100 < <" ";
cout < <endl < <endl;

srand (1);
for (i=0;i <10;i++);
cout < <rand()%100 < <" ";
cout < <endl;
return 0;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧