473,320 Members | 1,828 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,320 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 6690
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.