Sep 3, 2011 at 8:52pm UTC
Im working on a tutorial from a book that simulates a die roll by generating a random number. The only error i get is 'generator' was not declared in this scope. Am I not including the right header files? Keep in mind I am working in xcode on a mac.
// Die roller
// Demonstrates generating random numbers
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); // seed random number
generator;
int randomNumber = rand(); // generate random number
int die = (randomNumber % 6) + 1; // Get a number between 1 and 6
cout << " You rolled a " << die << endl;
return 0;
}
Last edited on Sep 3, 2011 at 8:53pm UTC
Sep 3, 2011 at 8:57pm UTC
"generator" is supposed to be part of the comment in the previous line.
Sep 3, 2011 at 9:00pm UTC
Add a new // for generator to make it a comment. Also, a more compact way of seeding and generating a random number would be:
1 2 3
srand(time(NULL));
int die=rand() %6 +1;
cout<<"You rolled a " <<die<<endl;
This is the same as what you did above, but it is more compact.
Last edited on Sep 3, 2011 at 9:01pm UTC
Sep 3, 2011 at 9:13pm UTC
hey guys i need help :( please
Sep 3, 2011 at 9:42pm UTC
Athar and davder123 gave you the answers you required. If you don't understand ask a more specific question.
Sep 3, 2011 at 10:07pm UTC
Hey that was my first post and was great help! Thank you for your time =D