Why isn't srand working?

D

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
 
L

Lew Pitcher

David said:
Hi,
I'm writing a fun little program to play "Petals Around the Roses"

Aha! I wrote a k&r C version of this in the mid 80s. Kewl puzzle, ain't it?

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

OK, you're off topic for comp.lang.c

You are still on topic for comp.lang.c++, though

[snip]

Followups set to comp.lang.c++
 
C

Christopher Benson-Manica

^^^^^^^^^^^

Please don't post C++ code to comp.lang.c in the future.
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.

Any newsreader that can't handle something simple like <iostream>
deserves to be shot anyway - don't bother with it in the future.
 
A

Andrey Tarasevich

David said:
...
#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));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What is this doing here and what did you mean by this?
 
G

Greg Schmidt

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.

You are trying to call srand outside of any function, which gcc is taking
to be an attempt to declare the function prototype. Move the call inside
of your main function, and all should be well.
 
L

Leor Zolman

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...:
--
[snip]

Best thing is just to copy and paste the code as-is right into your
messages. Don't worry about "<" characters, they won't bite ;-)

If you simply move that "srand" call into your main() function from where
it is (out at file scope), your program works fine.
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
B

Ben Pfaff

#include iostream> // I took out the <
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

Don't post C++ to comp.lang.c.
 
M

Malcolm

David Shaw said:
using namespace std;

srand((unsigned)time(NULL));
Either move this to main() or, if you want to play with C++, declare an
object

class Randomiser
{
public:
Randomiser(void) { srand( (unsigned) time(NULL)); };
};

Randomiser myengine;

This will cause the call to srand to be executed some time at program
startup. It's useful if you want to provide a library that relies on seeding
rand(), but doesn't have access to main().
 
K

Keith Thompson

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.

You're not on the web. This is Usenet, which has no problem with '<'
and '>' characters. Usenet is a plain text medium; HTML is largely
irrelevant.

And, as others have mentioned, your code is C++, so it's off-topic in
comp.lang.c.
 
K

Karl Heinz Buchegger

David said:
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.

It would have worked anyway "on the web". Most importantly because
this is *not* the web. This is usenet and we use text only.

More to the point of your post.
Don't mix old style and new style headers. This is a source
of confusion.

#include <iostream>
#include <cstdlib>
#include said:
using namespace std;

srand((unsigned)time(NULL));

The above is a function call. But function calls (unless they
are used to initialize some global variables) need to be in some
function!!!

int main()
{
srand((unsigned)time( NULL ));

display();

...
}
 
C

CBFalconer

Karl said:
It would have worked anyway "on the web". Most importantly because
this is *not* the web. This is usenet and we use text only.

More to the point of your post.
Don't mix old style and new style headers. This is a source
of confusion.

#include <iostream>
#include <cstdlib>
#include <ctime>

Please do not post c++ code to c.l.c. FUPs set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top