Help with randomizer


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
cout <<"This Program will randomly pick a name"<<endl;
	Sleep (2000);
	 srand((unsigned)time(0)); 
    
    for(int index=0; index<20; index++)
        random_integer = (rand()%20)+1; 
  Sleep (5000);
        
        if (random_integer = 1)
        {
        	cout <<" name 1"<<endl;
        }
        if (random_integer = 2)
        {
        	cout <<" name 2"<<endl;
        }
        if (random_integer = 3)
        {
        	cout <<"name 3"<<endl;
        }
        if (random_integer = 4)
        {
        	cout <<"name 4"<<endl;
        }
        if (random_integer = 5)
        {
        	cout <<" name 5"<<endl;
        }


The if statements continue until 20. Later the "name 1, name 2".. stuff will just be a string. The problem with the current code is that the if statements don't work. No matter what the random number is, all of the names are displayed. Help please?
Last edited on
You used a single = instead of a double ==
I know the single = sign is called the assignment operator, so whats the double one called
The equality comparison operator, it lets you see if two things are equivalent.
Why are you doing this?
1
2
for(int index=0; index<20; index++)
    random_integer = (rand()%20)+1;


You are recalculating random_integer 20 times. Second, I'd probably re-organize this like so:
1
2
3
string names[20] = { "Fred", "James", "Jordan", "Jillian", /* ... */ "Jack" };
random_integer = rand() % 20;
cout << names[random_integer];

A combination of if statements like this can almost always be shorter. Copy+paste is not a programming technique.
Topic archived. No new replies allowed.