Problems with generating random numbers

I've been writing a very simple chatbot but I'm stuck on generating random responses. I'm attempting to generate a random number between 1 and 5, and each number corresponds to a response in a switch statement. Here's my code:

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
45
46
47
48
49
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
    srand() = time(NULL);
    string name = "hat is your name";
    string userName = "USER";
    size_t found;
    while(true)
    {
        string input;
        string inputPrev = input;
        input = "";
        cout << userName << ": ";
        getline(cin,input);
        cout << "MARTY: ";
        found = input.find(name);
        if(found != string::npos)
        {
            int random = rand();
            while(random < 1 || random > 5)
            {
            	random = rand();
            }
            switch(random)
            {
                case 1:
                    cout << "My name is Marty.\n";
                case 2:
                    cout << "My name's Marty.\n";
                case 3:
                    cout << "The name's Marty.\n";
                case 4:
                    cout << "You can call me Marty.\n";
                case 5:
                    cout << "Call me Marty.\n";
            }
        }
        else
        {
            cout << "I don't understand.\n";
        }

    }
    return 0;
}


However, when compiling in g++ on Ubuntu, I get this error message:

marty.cpp:8:11: error: too few arguments to function ‘void srand(unsigned int)’


Please keep in mind that I don't know exactly what I'm doing. I'm basing the random number generating code off of various tutorials. So what's wrong? Thanks in advance for your assistance!
Last edited on
That error's saying that the srand function expects an unsigned int as a parameter, but you're not supplying any.

This:

srand() = time(NULL);

should be this:

srand( time(NULL) );
I remember thinking of doing that, and I have no idea why I didn't! Anyway, thanks a ton. My project is back underway!
By the way, you can provide bounds for your random function:
int random = rand()%5+1; // random is now between 1 and 5, inclusive.
You'll also need to break; each of your cases in your switch statement or flow will "spill over" (ie: option 1 will print all 5 strings)
Topic archived. No new replies allowed.