how could i create random objects? and assign values to them?

Oct 18, 2012 at 11:31am
i am attempting a lighter version of the bunny challenge, first i just want lots of objects popping up with certain variables that may call functions or get called by functions.

or if you want to just point me in the right direction a clue would be just as helpful
Oct 18, 2012 at 11:35am
Create a vector of objects (empty).

Each time your random trigger happens, add an object to the end of the vector.

When a function needs to do something with one of them, choose a random number from zero to size-of-vector minus one and use that object.
Oct 18, 2012 at 11:39am
ibut how do i create that object to be added to the vector? plus need to review vectors i thinks, thanks though be back when i reviewed vectors
Last edited on Oct 18, 2012 at 11:40am
Oct 18, 2012 at 11:54am
Here is how to create an object of type someType.
someType anObject;

Here is how to add it to a vector
thevector.push_back(anObject);
Oct 18, 2012 at 11:58am
Hey wait... how will i generate an object randomly,by witch i mean, one that doesnt allready exist into existence
Last edited on Oct 18, 2012 at 1:08pm
Oct 18, 2012 at 1:14pm
Generate random number.
Look at the number.
If the number is one you have decided means to make an object, make an object.
Oct 18, 2012 at 4:02pm
the thing i am finding hard is this

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
class directors{
()director constructo n stuff..
}
 
void directionfunction();
main ()

{
directors polanski
directors quentin

directorfunction ()
getdirector stuff....
return 0;
}


void directorfunction
{
directors (name=randomnamegeneratingfunction())

}

void random namegeneratingfuntion ()
{
}


my problem is popping a name right there in front of class and having it become the object...maybie i dont know what i am doing :P
Oct 18, 2012 at 4:15pm
1
2
3
4
5
6
7
8
9
vector<directors>  all_directors;

//  Add your directors to the vector
all_directors.push_back (polanski);
all_directors.push_back (quentin);

// generate a random number i from 0 to all_directors.size()-1
directors rand_dir;
rand_dir = all_directors.at(i);


IMO, your class name should be singular since each instance holds one director.
Oct 18, 2012 at 5:07pm
IT WORKS IT WORKS BWAAAHAHAHAHAHAAAAAAHAAA

thanks guys
Oct 18, 2012 at 6:54pm
why have i got a crashwhen i run??

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

class bunny
{
    public:

        bunny  (int x=5, int y=5): posx (x), posy (y) {} //declaring posx and posy
        void setname (string  x)
        {
            name = x;
        }
        int setposx (int x)
        {
            posx=x;
        }
        int setposy (int y)
        {
            posy=y;
        }
        int getposx ()
        {
            return posx;
        }
        int getposy ()
        {
            return posy;
        }
    string name;
    int posx;
    int posy;
    char bunicon;

    };



char z = '*';
char x = ' ';
char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };

int getrand ();
void getboard ();
string namelist ();
void createobj ();
//////////////////////////////////////////////////////////////////////////////
int main()
{
srand(time(0));
vector <bunny>bunnies;
createobj();
createobj();
createobj();

cout <<bunnies[1].name<<endl;

}

//////////////////////////////////////////////////////////////////////////////
string namelist ()
{
string namearray [18] = {"bon","fur","fluff","foof","bax","tig","uru","ira","esa","afe","ige","ifi","floof","tix","dar","vini","ter", "von"};

string none = namearray [getrand()%18];
string ntwo =namearray [getrand()*4%18];
string nthree=namearray [getrand()*7%18];
string fullname=none+ntwo+nthree;
return fullname;
}

void getboard ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }
}

int getrand ()
{
return 1+(rand()%6);
}

int getposx (int x)
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)

 {
  brdray [a] [b];
  if (brdray[a] [b]);

}
}
}

void createobj ()
{
    vector <bunny>bunnies;
    bunny namelist() ;
    bunnies.push_back (namelist());


}
Oct 18, 2012 at 7:56pm
cout <<bunnies[1].name<<endl;
Because bunnies is of size zero.

In your function createobj, you make a WHOLE NEW vector which you add the bunny to, and then when the function ends that new vector is destroyed. The one you made in main is untouched.
Oct 18, 2012 at 9:16pm
daaaah! any ideas on it doing what iwant?
Oct 18, 2012 at 9:24pm
Pass in the vector you want the function to change, by reference.
Oct 18, 2012 at 9:26pm
You're supposed to make a std::list (or your own list) of bunnies.

Edit: not very helpful...
Last edited on Oct 18, 2012 at 9:26pm
Oct 18, 2012 at 10:13pm
thanks guys...this reference thing is difficult to master...i have hit a wall in my learning..any advice for learning walls? alcohol? good answer.
Topic archived. No new replies allowed.