Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
7  
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. –  Josh Petrie Aug 6 '12 at 16:22
1  
I will research on that ;) –  Angel Aug 6 '12 at 16:56
    
Look up the class keyword instead. It allows you to do very neat OOP things, but essentially it's a struct with functions. –  jco Aug 6 '12 at 21:30
1  
@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++ –  Maik Semder Aug 6 '12 at 21:49
2  
@Bane There's no rational reason, just habits, since again, both are similar –  Maik Semder Aug 6 '12 at 22:51
show 4 more comments

2 Answers 2

up vote 7 down vote accepted

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];
share|improve this answer
    
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 :) –  Angel Aug 6 '12 at 16:55
add comment
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.

share|improve this answer
1  
Getting 4 as a result will actually access outside the bounds of the array, which only has four elements. –  Josh Petrie Aug 6 '12 at 16:37
    
@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. –  Raceimaztion Aug 6 '12 at 18:38
    
My mistake. I took it out. –  Philip Aug 6 '12 at 20:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.