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:

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?

  • 7
    \$\begingroup\$ You may want to look into the struct keyword -- 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. \$\endgroup\$
    – user1430
    Aug 6 '12 at 16:22
  • 1
    \$\begingroup\$ I will research on that ;) \$\endgroup\$
    – Angel
    Aug 6 '12 at 16:56
  • \$\begingroup\$ Look up the class keyword instead. It allows you to do very neat OOP things, but essentially it's a struct with functions. \$\endgroup\$
    – jcora
    Aug 6 '12 at 21:30
  • 1
    \$\begingroup\$ @Bane class and struct is the same in C++ except everything is public by default in a struct, private is default for classes. Everything else including member functions is similar for structs and classes in C++ \$\endgroup\$ Aug 6 '12 at 21:49
  • 2
    \$\begingroup\$ @Bane There's no rational reason, just habits, since again, both are similar \$\endgroup\$ Aug 6 '12 at 22:51

Your series of if/else statements can be simplified to this:

cout << monsterlist[monsternumber];

However, the line monsternumber= 1+(rand()%3); is problematic in the context of the rest of your program. rand() % 3 produces a value between 0 and 2, which you then add one to. That means monsternumber can be from 1 to 3. It will never be zero and it will never be four.

But arrays in C++ use zero-based indexing. That means the first element in the array is at index 0 -- in order words, monsterlist[0] is the "Imp" string.

Similarly you don't want to try to access monsterlist[4] because you only have four elements in that array, so the only valid indices are 0 through 3. Thus the ideal solution to your problem is something like this:

string monsterlist [] = {"Imp", "Orc", "Troll", "Gnome"};
float monsterhealth [] = {5.0f, 8.0f, 9.0f, 10.0f};
int monsterlife;
int monsternumber = rand() % 4;

cout << monsterlist[monsternumber];
  • \$\begingroup\$ I guess this helps me, thank you. I tried it and it worked. I can continue with the other parts of the scene I am making in this simple C++ program / game. Thanks so much Josh :) \$\endgroup\$
    – Angel
    Aug 6 '12 at 16:55
monsternumber = rand()%4;
cout << monsterlist[monsternumber];

That will keep you from having to do a large if ... else sequence.

Also, do rand()%4, or else you will never get a 4 as a result.

Other than that, it looks like it should be doing what you say you want it to do.

  • 1
    \$\begingroup\$ Getting 4 as a result will actually access outside the bounds of the array, which only has four elements. \$\endgroup\$
    – user1430
    Aug 6 '12 at 16:37
  • \$\begingroup\$ @Philip: You've forgotten that array indices in C++ start at 0 and go to one less than the number of elements in the array, so adding one to the random number isn't a good idea. \$\endgroup\$ Aug 6 '12 at 18:38
  • \$\begingroup\$ My mistake. I took it out. \$\endgroup\$
    – Philip
    Aug 6 '12 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .