zweed4u
zweed4u

Reputation: 217

Create a user desired input number of objects C++

I have a pretty simple question. I have a program that prompts the user for how many people are desired for a simulation within the program. I am wondering how I would go about initializing the value of the cin objects. Here is a snippet of what I'm looking at:

cout<<"Number of users? ";
int users;
cin>>users;

The basics^. I want to take the input I get for users and make this many people objects. I have a class called CPerson that has several basic member functions like getName() and getGender(). I'm not so concerned with these. I then need to be able to put the number of users created into a queue which I figure won't be so hard once I have the objects initialized.

Thank you for any help.

UPDATE: I ended up with something that looked like this which yielded desired results. Thanks to all.

vector<CPerson*> people;
for (unsigned int x=0; x<users; x++) {
    CPerson *user = new CPerson(Names[x]);
    people.push_back(user);
    cout<<user->getName()<<endl;
}

Upvotes: 0

Views: 1093

Answers (1)

David G
David G

Reputation: 96810

Once you get the input from the user you can then create a dynamic array:

int n;
std::cin >> n;

int* array = new int[n];
// ...
delete[] array;

Or you can use std::vector where the size can accommodate the users input.

Upvotes: 1

Related Questions