注册 登录
编程论坛 C++教室

C++ 中有关 srand 我这个为什么在 Dev-c++ 里面不能运行? VC 就能运行?

yu497272290 发布于 2014-09-03 23:04, 855 次点击
程序代码:
#include<iostream>
#include<time.h>
using namespace std;//使用命名空间 std.

int main()
{

 srand(time(NULL));//将时钟数作为随机数种子


 int price,guess;

 price = rand() % 1000 + 1;//取余,获得1~1000的随机数


 cout << "please input the price:" << endl;

 

 do{

 

 cin >> guess;//记录玩家所输入数值
if(guess > price)
    cout << "The price is lower than your guess,please input the price again:" << endl;

 if(guess < price)
    cout << "The price is higher than your guess,please input the price again:" << endl;//给予玩家比较大小的提示
}

 while (guess != price);//循环结构


 cout << "The price you give is right.It cost " << price << "." <<endl;//获得正确答案
system("pause");

 return 0;

 
}
7 回复
#2
yu4972722902014-09-03 23:09
?有人在吗?问一下
#3
wp2319572014-09-04 08:09
srand 包含time.h 应该就可以了
#4
stop12042014-09-04 17:56
应该有给出编译出错的位置提示吧.
#5
apull2014-09-04 23:25
加上
#include <stdlib.h>
#6
zklhp2014-09-05 12:49
srand()

这是一个C函数 如果你要用 需要引用C++化的头文件 如果不这样 可能会出问题 尤其是不同环境下
#7
yu4972722902014-09-06 15:31
加上
#include <stdlib.h>

我前几天和你一样
#8
午夜小学徒2014-09-11 20:59
你试试这个随机数形式 arc4random()%替换rand()%试试(arc4rand()%是Mac系统的随机数)

[ 本帖最后由 午夜小学徒 于 2014-9-11 21:00 编辑 ]
1