srand error

I am writing a program for a magic 8ball. I believe that I have everything I need with the exception of when I go to compile and run, I receive an error message stating that srand is not declared. I tried adding #include <cstdlib> to the header files, and that just gave me a different error. What am I missing? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>
#include <string>
#include <ctime>


using namespace std;

int main ()
{

    string getAnswer(string m8Ball[], int nAnswers);

//define a constant that represents how many answers are in your array (at least 8)
	const int SIZE = 8;

	//declare and initialize an array of strings, representing possible answers from the 8-ball
	string m8ball[SIZE]{ "Certainly","Yes","Most Likely","Perhaps yes","Try again","Ask later","Don't do it", "No" };

	srand((unsigned int)time(NULL));



	//loop and ask the user to enter a question or enter "x" to stop
	while (true)
	{

		//use getline to get the question
		string question;
		cout << "What is your question? (Enter 'x' to exit)" << endl;
		getline(cin, question);

		if (question.compare("x") == 0)
			break;

		//call getAnswer with your array and number of possible answers to get an answer
		string answer = getAnswer(m8ball, SIZE);

		//output the answer
		cout << answer << endl << endl;

    }

}
you need its header, which I think is cstdlib
Line 11 makes no sense to be there.
You need to move it above main() function and add a body to it to declare a function.
Last edited on
Thank you!. That worked out well!
Topic archived. No new replies allowed.