473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why isn't srand working?

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
Nov 14 '05 #1
10 6706
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++
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.

--
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.
Nov 14 '05 #2
David Shaw wrote:
...
#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?

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #3
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.


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.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
Nov 14 '05 #4
On 3 Mar 2004 11:33:22 -0800, sp***********@mailinator.com (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.
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
le**@bdsoft.com
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
Nov 14 '05 #5
David Shaw wrote:
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++
--
Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
Nov 14 '05 #6
sp***********@mailinator.com (David Shaw) writes:
#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.
--
Go not to Usenet for counsel, for they will say both no and yes.
Nov 14 '05 #7

"David Shaw" <sp***********@mailinator.com> wrote in message
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().
Nov 14 '05 #8
sp***********@mailinator.com (David Shaw) writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #9
David Shaw wrote:


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 <ctime>

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();

...
}

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 14 '05 #10
Karl Heinz Buchegger wrote:
David Shaw wrote:

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 <ctime>


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dfenestr8 | last post by:
I started off trying to use bsddb with my standard mandrake 9 python 2.2.1 package. The shell reported back no such module. Strange, thinks I. I thought it was standard in the package, it was when...
14
by: David Shaw | last post by:
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...
1
by: Sunshine | last post by:
Pretty new to javascript so please help. Why isn't this working in my validateform()? if (document.This.strOld.value.compareTo(Session("strPassword")) != 0) { alert("Current password...
29
by: interpim | last post by:
Just a quick exercise from my C programming book, that I can't figure out why it isn't working properly. It kinda works but im getting wierd output. It is supposed to remove the vowels from text. ...
6
by: Christina Androne | last post by:
I've read the docs and evrything seems fine. Then what I am missing ? This is the code snippet: try {
0
by: Dave | last post by:
I have a simple master page and it won't seem to work: <%@ Master Language="C#" AutoEventWireup="true" CodeFile="z.master.cs" Inherits="z" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.