为什么不工作? [英] Why isn't srand working?
问题描述
我正在写一个有趣的小程序来玩玫瑰花瓣
...我需要播种我的随机数字所以每次运行程序时它们都不会是
,但是我的编译器不会让我。
这里是输出我的编译器生成...:
-
编译器:默认编译器
执行g ++。exe ...
g ++。exe" C:\Documents and Settings \rmarkoff \Desktop\roses.cpp" -o
" C:\Documents and Settings \rmarkoff \Desktop\roses.exe"
-I" C:\Dev-Cpp \\ \\include\c ++" -I" C:\Dev-Cpp \include\c ++ \mingw32"
-I" C:\Dev-Cpp \include\c ++ \ backward" -IC:\ Dev-Cpp \ include
-L" C:\Dev-Cpp \lib"
C:/ Documents and Settings /rmarkoff/Desktop/roses.cpp:7:ISO C ++
禁止
声明`srand''没有类型
C:/ Documents and Settings / rmarkoff / Desktop / roses.cpp:7:`int srand''
重新申报
作为不同类型的符号
C:/Dev-Cpp/include/stdlib.h:362:先前声明的'void
srand(unsigned
int)''
执行终止
-
这里是我的代码:
#include iostream> //我拿出了<
#include stdlib.h> //在这些#includes
#include time.h> //这样他们就可以在网上工作了。
使用命名空间std;
srand((unsigned)time(NULL));
int matrix [4];
void display(void){// Start Display()
int x;
for(x = 0; x< 4; x ++){//随机化矩阵
矩阵[x] =( rand()%6)+ 1;
} //结束于()
std :: cout<< " \\\
----------------------------------" << std :: endl;
std :: cout<< " |" << matrix [0]<< " |" << " |" <<矩阵[1]<< " |" <<
" |"
<<矩阵[2]<< " |" << matrix [3]<< " |" <<
矩阵[4]<< std :: endl;
std :: cout<< " ----------------------------------" << std :: endl;
} //结束显示()
int main(){//启动main()
display();
std :: system(" pause");
} //结束主
....感谢您给我的任何帮助。 :)
- David Shaw
Hi,
I''m writing a fun little program to play "Petals Around the Roses"
with... I need to seed my random numbers so that they won''t be the
same every time I run the program, but my compiler won''t let me.
Here''s the output my compiler generated...:
--
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\rmarkoff\Desktop\roses.cpp" -o
"C:\Documents and Settings\rmarkoff\Desktop\roses.exe"
-I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32"
-I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include"
-L"C:\Dev-Cpp\lib"
C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: ISO C++
forbids
declaration of `srand'' with no type
C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: `int srand''
redeclared
as different kind of symbol
C:/Dev-Cpp/include/stdlib.h:362: previous declaration of `void
srand(unsigned
int)''
Execution terminated
--
And here''s my code:
#include iostream> // I took out the <
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.
using namespace std;
srand((unsigned)time(NULL));
int matrix[4];
void display(void) { // Start Display()
int x;
for(x=0;x<4;x++) { // Randomize matrix
matrix[x] = (rand() % 6) + 1;
} // End for()
std::cout << "\n----------------------------------" << std::endl;
std::cout << "|" << matrix[0] << "|" << "|" << matrix[1] << "|" <<
"|"
<< matrix[2] << "|" << matrix[3] << "|" <<
matrix[4] << std::endl;
std::cout << "----------------------------------" << std::endl;
} // end display()
int main() { // Start main()
display();
std::system("pause");
} // End main
.... thanks for any help you can give me. :)
- David Shaw
推荐答案
在comp.lang.c中David Shaw< sp ***** ******@mailinator.com>写道:
^^^^^^^^^^^
请将来不要将c ++代码发布到comp.lang.c 。
In comp.lang.c David Shaw <sp***********@mailinator.com> wrote:
^^^^^^^^^^^
Please don''t post C++ code to comp.lang.c in the future.
C:/ Documents and Settings / rmarkoff / Desktop / roses.cpp:7:ISO C ++
禁止声明`srand''没有类型
C:/ Documents and Settings / rmarkoff / Desktop / roses.cpp:7:`int srand''
使用命名空间std作为不同类型的符号
重新声明
;
srand((无符号)时间(NULL));
你的编译器试图告诉你一些事情,即你不能像你想要的那样在函数之外调用函数。它b / b
认为你宣布了一个原型,因此变得混乱了。
#include iostream> //我拿出了<
#include stdlib.h> //在这些#includes
#include time.h> //这样他们就可以在网上工作。
C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: ISO C++
forbids
declaration of `srand'' with no type C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: `int srand''
redeclared
as different kind of symbol using namespace std; srand((unsigned)time(NULL));
Your compiler is trying to tell you something, namely that you can''t
call functions outside of functions, as you are trying to do. It
thinks you''re declaring a prototype, and is consequently becoming
confused.
#include iostream> // I took out the <
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.
任何新闻阅读器都无法处理简单的事情,例如< iostream>
不管怎么说都值得拍摄 - 将来也不用担心。
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
Any newsreader that can''t handle something simple like <iostream>
deserves to be shot anyway - don''t bother with it in the future.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
David Shaw写道:
David Shaw wrote:
...
#include iostream> //我拿出了<
#include stdlib.h> //在这些#includes
#include time.h> //这样他们就可以在网上工作了。
使用命名空间std;
srand((unsigned)time(NULL));
...
#include iostream> // I took out the <
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.
using namespace std;
srand((unsigned)time(NULL));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这是做什么的,你的意思是什么这个?
-
祝你好运,
Andrey Tarasevich
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What is this doing here and what did you mean by this?
--
Best regards,
Andrey Tarasevich
2004年3月3日11:33:22 -0800,David Shaw写道:
On 3 Mar 2004 11:33:22 -0800, David Shaw wrote:
我正在写一个有趣的小程序玩玫瑰花瓣
...我需要播种我的随机数字,以便每次我运行程序时它们都不会相同,但我的编译器赢了不要让我。
Hi,
I''m writing a fun little program to play "Petals Around the Roses"
with... I need to seed my random numbers so that they won''t be the
same every time I run the program, but my compiler won''t let me.
你试图在任何函数之外调用srand,gcc正在采取
来尝试声明功能原型。在主要功能的内部移动电话
,一切都应该很好。
-
Greg Schmidt gr *** @ trawna.com
Trawna Publications http://www.trawna.com/
这篇关于为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!