So, I have a hero structure that contains name, health, and attack. I'm allowing the user to enter how many heroes they want to create and create an array of that many heroes (I also had problems allowing the user to determine the array size, so the problem could be there). When trying to set the attributes using a loop of the array, I get an error: IntelliSense: no operator "[]" matches these operands. operand types are: Hero [ int ]
My question is, how do I loop through an array of a structures to set their attributes and if so, would displaying the hero's information be similar with a display function?
struct Hero
{
private:
string hName;
int hHealth;
int hAttack;
public:
void displayHeroData();
void setName(string);
void setHealth(int);
void setAttack(int);
};
void Hero::displayHeroData()
{
cout << "\n\n\n\nHERO INFO:\n" << endl;
cout << "Name: " << hName << endl;
cout << "Health: " << hHealth << endl;
cout << "Attack: " << hAttack << endl;
};
void Hero::setName(string name)
{
hName = name;
}
void Hero::setHealth(int health)
{
if(health > 0)
hHealth = health;
else
hHealth = 100;
}
void Hero::setAttack(int attack)
{
if(attack > 0)
hAttack = attack;
else
hAttack = 100;
}
int main()
{
string name;
int health;
int attack;
int num;
Hero *heroList; //declaring array
//getting size of array
cout << "How many hero's do you want to create? (greater than 0)" <<endl;
cin >> num;
heroList = new Hero[num]; //this is the array of Heroes
//looping through the array
for(int x = 0; x < num; ++x){
//creating a new hero, I think???
Hero heroList[x];
//setting hero's name
cout << "What is hero" << x <<"'s name?" << endl;
cin >> name;
heroList[x].setName(name);
//display the character after attributes have been set
heroList[x].displayCharacterData();
}//end of for loop
return 0;
}