Errors when seeding rand() with time(0)

Hi,

I'm an amateur hobbyist working on a project involving random number generation. I'm trying to seed rand() with time(0).

The line in question: srand( static_cast<unsigned int>(time(0)));


When I build the program, it gives me the following message:

error: C++ requires a type specifier for all declarations.

Note: I am using Code::Blocks on a Mac.


I've never seen this before, and haven't been able to find anything online.
Thank you for any help you can offer,

-Lord Mirdalan
Last edited on
Hi,
you probably missed a header file.... could you show us your header files which you have included in code?

PS: welcome to cplusplus.com
Greetings,

Here you go... If this is not what you meant. please let me know. And thank you for the very prompt reply!

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

using namespace std;

with these header files it should work... could you show us your whole code?
Did you put this line of code inside a function?
No, the code is not in a function, although it is being used by a function.

Here's the code. It's for a college programming class - Don't they invent the most original projects?

____________________________________________________________________________

//Date: July 24 2016
//Programmer: ********* *****
//Program name: rollDice
//User enters the desired sum of two dice, and the computer generates random numbers until //they equal the desired sum.

#include <iostream> //allows program to input & output
#include <ctime> //contains functions used to work with time
#include <cstdlib> //defines general purpose functions

using namespace std; //program uses standard libraries

unsigned int rollDice(); //function to roll the dice

unsigned int num; //Number input by user (Desired sum of the dice)
unsigned int sum; //The sum of the two dice
unsigned int die1; //Stores the value of the first die
unsigned int die2; //Stores the value of the first die

int rollCount = 0; //Number of times the dice were rolled to achieve the desired sum

srand( static_cast<unsigned int>(time(0))); //Seeds srand() with the time, measured in seconds since the Linux epoch.


int main() //function main
{//begin main



do{ //begin do... while
cout<<"Enter the desired sum of the dice "; //prompts the user for input
cin>> num; //assign the input data to sum
} //end do... while
while (num < 2 || num > 12); //checks to see if the entered integer is an appropriate value for sum
cout << "Invalid number - try again"; //if no, prompt user for input
cin >> num;

while (sum != num){
rollDice(); //Rolls the dice
}//end do... while


return 0; //returns function main
}//end main


unsigned int rollDice()
{//begin function rollDice
unsigned int die1 = 1 + rand() % 6; //roll the first die
unsigned int die2 = 1 + rand() % 6; //roll the second die

rollCount ++; //Increment rollcount

unsigned int sum = die1 + die2; //the sum of the dice

cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl; //display the individual values of the dice, and their sum
cout << "The number of times the dice were rolled is " << rollCount << endl; //Display rollCount

return sum;
} //end function rollDice




____________________________________________________________________________
Then that's the problem. You need to put it inside a function. A good place to call srand is at the beginning of main().
All right...

That fixed the problem!

Thank you!

Of course, now there are others... That's life, I guess.

-Lord Mirdalan
Topic archived. No new replies allowed.