dimanche 1 octobre 2017

Generating randomized objects in a loop in c++

I have a person object class which is made up of name, address, and phone number object classes. The name, address, and phone number are randomly generated.

The user selects how many random people to generate and a function is called that generates them with a loop and stores them in a vector:

vector<P> pV;  //vector of people

for(int i = 0; i < num; i++){
    P x; //create new person object x
    pV.push_back(x) //add to vector of people
    cout << pV[i].getName();  //print for text
}

The problem is, all the objects generated are identical (same name, address, etc). If I generate a new list during the same session, a person with different information gets generated but they'll still all be identical.

I know this can happen in Java if you use static when randomizing... I'm not sure of the equivalent in C++.

Any suggestions?

Thanks




Aucun commentaire:

Enregistrer un commentaire