I have a problem dealing of how can I call the values inside the arrays correctly. First of all, I am into making an array of monsters and a set of hp for them. I made it this way:
string monsterlist [] = {"Imp", "Orc", "Troll", "Gnome"};
float monsterhealth [] = {5.0f, 8.0f, 9.0f, 10.0f};
int monsterlife;
int monsternumber;
wherein, since i can not call the variables correctly, I made a very simple way to do it, which is:
srand(time(0));
monsternumber = 1 + (rand() % 3);
if (monsternumber == 1)
cout << monsterlist[1];
else if (monsternumber == 2)
cout << monsterlist[2];
else if (monsternumber == 3)
cout << monsterlist[3];
else if (monsternumber == 4)
cout << monsterlist[4];
Is that correct? I know it might be not the proper way. This is what I wanted:
I want to create a random number generation from 1 to 4 and store that variable in monsternumber. The next thing that will happen is, if the monsternumber is equal to 1, for example, should make the monster Imp and 5.0 as its health, seen from string monsterList and monsterHealth.
Is there a proper way to make this simplier or rather the correct way of doing it?
structkeyword -- it will not directly solve your problem but it will help you package up your monster names and HP values, et cetera, into a single object you can have a single Monster monsters[] = ... array instead of a bunch of separate arrays you need to keep in sync. – Josh Petrie Aug 6 '12 at 16:22classkeyword instead. It allows you to do very neat OOP things, but essentially it's astructwith functions. – Yannbane Aug 6 '12 at 21:30