Help With Vectors

Hello everyone,
As a high school student I've been working game in my spare time as a way of furthering my programming skills, however I recently ran into a roadblock when attempting to learn vectors and convert some of my arrays into vectors.

I've been running into segmentation faults no matter how much I work on it and I think its due to how I'm defining objects in the vector.

Here's some snippets of the related code:

1
2
3
4
5
6
7
//Asteroid.h

vector <float> GlobalAsteroidX;
vector <float> GlobalAsteroidY;

vector <int> AsteroidCurrentHealth;
vector <int> AsteroidHeading;



1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Asteroid.cpp

void Asteroid::Rand(int AsteroidCount) //randomly generates the X,Y and Heading of asteroids
{
    for (int c = 0; c < AsteroidCount; c++)
    {
        GlobalAsteroidX[c] = rand()%GameWidth+1;
        GlobalAsteroidY[c] = rand()%GameHeight+1;

        AsteroidHeading[c] = rand()%360+1;

        AsteroidCurrentHealth[c] = 100;
    }
}


1
2
3
4
//Main.cpp
//If Mouse clicks button -> then
    global.AsteroidCount = 350;
    as.Rand(global.AsteroidCount);


Its seems the program crashes after the Rand function runs. Can anyone give me advice on if I'm doing something wrong here? Thanks in advance.
closed account (zb0S216C)
With the code that you posted, you seem to be violating the bounds of the vector (s). The vector (s) need to be pushed-back for it/them to grow. For example:

1
2
3
std::vector<int> ints; // No items within this vector.

ints.push_back(10);    // There's 1 item within the vector, now. 


Try altering the for to something like this:

1
2
3
4
5
6
7
8
for(unsigned int cycle(0U); cycle < AsteroidCount; ++cycle)
{
    GlobalAsteroidX.push_back((rand() % GameWidth) + 1);
    GlobalAsteroidY.push_back((rand() % GameHeight) + 1);

    AsteroidHeading.push_back((rand() % 360) + 1);
    AsteroidCurrentHealth.push_back(100);
}


At the end of the loop, AsteroidCount items will be pushed into the corresponding vector. I would suggest structures, though, for an asteroid object.

Wazzak
Last edited on
Thank you, that makes a lot more sense!
Unfortunately I'm still running into some errors. Regarding using the information within the vector, how exactly can I check and/or modify those values? For example, I have the code pasted below and I'm not sure if the syntax stays the same or do I need to change it for checking within vectors?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Asteroid::InRange(float GlobalPlayerX, float GlobalPlayerY, float PlayerArmor, int& PlayerCurrentHealth, int AsteroidCount, bool Debug)
{
    for (int c = 0; c < AsteroidCount; c++)
    {
        if ((GlobalAsteroidY[c] - GlobalPlayerY >= AsteroidRange * -1 && GlobalAsteroidY[c] <= GlobalPlayerY) || (GlobalAsteroidY[c] - GlobalPlayerY <= AsteroidRange && GlobalAsteroidY[c] >= GlobalPlayerY)) //Checks if Player's Y values is close enough
        {
            if ((GlobalAsteroidX[c] - GlobalPlayerX >= AsteroidRange * -1 && GlobalAsteroidX[c] <= GlobalPlayerX) || (GlobalAsteroidX[c] - GlobalPlayerX <= AsteroidRange && GlobalAsteroidX[c] >= GlobalPlayerX)) //Checks if Player's X value is close enough
            {
            PlayerCurrentHealth -= AsteroidAttack / PlayerArmor; //Player takes damage

            if(Debug == true)
            {
            std::cout << "Asteroid #" << c <<" (Hit) || Player Health: " << PlayerCurrentHealth << endl; //Print out
            }
            }
        }
        else
        {
            //Place holder
        }
    }
}

closed account (zb0S216C)
dev geo wrote:
"how exactly can I check and/or modify those values?" (sic)

There's two ways you can access the data within a vector:

1) Use the vector::at() method. This returns a non-constant reference to the specified item.
2) Use the overloaded vector::operator[] operator.

Both do the same thing. However, vector::at() throws an std::out_of_range exception when the bounds of the vector are exceeded. The overloaded operator, however, doesn't, which effectively allows you to write to an unknown section of memory without generating a run-time error. The choice is yours.

Here's an example that uses both of the latter:

1
2
3
4
5
6
7
8
9
10
11
12
// Create a new vector with two items, both of which have the value of zero.
std::vector<int> new_vector(2, 0);

std::cout << "Values are: ";

// Using the overloaded sub-script operator:
std::cout << new_vector[0U] << ", ";

// Using vector::at():
std::cout << new_vector.at(1U)

std::endl(std::cout);


dev geo wrote:
"I'm not sure if the syntax stays the same or do I need to change it for checking within vectors?" (sic)

The syntax is the same.

Wazzak
Last edited on
Thanks again!
You've helped me tons.
Topic archived. No new replies allowed.