using the srand function

I am using Code:blocks 10.05 to compile my programs. I cannot seem to understand why code blocks does not recognizes the srand function. Look at the program above:
Can someone please explain this to me.

//Guess the secret number
# include <iostream>
using namespace std;

int myAbs (int i)
{
if (i < 0)
i += -1;
return i;
}

int main ()
{
int secret, guess, absError;

srand (time (0))
secret = 1 + rand ( ) % 100;

cout << "Enter the guess:";
cin >> guess;

absError = myAbs(secret - guess);
cout <<"You were out by"<< absError << endl;

return 0;
}
Last edited on
[deleted]
Last edited on
You need the #include <random> library for srand and rand

srand and rand are in cstdlib
http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand


I am using Code::blocks 10.05 to compile my programs

code blocks is not a compiler.
The srand() and rand() functions are in library random (#include<random>) and time() function in time.h (#include<time.h>)

Also, I've noticed an error in your myAbs function. It should be i*=-1 not +=-1.

Finally, please use code tags when posting code:

http://i.imgur.com/EBuaMoE.png
Yeah, @Yanson is right. Code::Blocks isn't compiler, it's just an IDE. If you have the default compiler it would probably be GCC/G++, but technically you use Code::Blocks to sort of compile them so you can say you're both right.
Last edited on
Yanson is correct.
In C++ the required headers are
1
2
3
#include <iostream>
#include <cstdlib>
#include <ctime> 
There's also #include<random> It works for me.
Thank you guys. I am now sorted.
http://i.imgur.com/l37JIH1.png

As you can see, time.h and random are also right.
Yes jgg2002. Thanks
Topic archived. No new replies allowed.