Array of Objects

int main(){

cout<<"How many players ar you?"<<endl;
cin >> players;

for(int i = 0; i <= players; i++){

Character* player = new Character[i];//dynamic array

cout<<i+" "+ player[i].getLives()<<endl;
cout<<i+" "+ player[i].getStrength()<<endl;
cout<<i+" "+ player[i].getCraft()<<endl;


}

When I run it the command console opens but then crashes once you enter the # of
players. Can some one help me out? All I want to do is store multiple objects
in each index of an array, then display what the index/ object(player) has as attributes. Thank you
You shall place this statement outside the loop

1
2
3
4
5
6
7
8
9
10
11
Character* player = new Character[players];//dynamic array

for(int i = 0; i < players; i++){
 

 cout<<i+" "+ player[i].getLives()<<endl;
 cout<<i+" "+ player[i].getStrength()<<endl;
 cout<<i+" "+ player[i].getCraft()<<endl;
 

}
Thank you
Topic archived. No new replies allowed.