Search code examples
c++for-loopstructcounter

c++ struct - modify inside for loop using counter


I am bit new to C++. I am trying to use struct to keep my data better organised.

in the following code I am trying to modify a struct member. all members are named using an int.

I have two problems:

My counder currentParticleCount wont increment after adding to the struct. there seem to be errors in the way I am using the counter i in the for loop to reference the stuct data...

As I said I am quite new to C++ I probably dont know the correct terms for what I am trying to achieve, and am not finding any real answers online.

int currentParticleCount = 0;
 
struct particle
{
 int velocity;
 int trajectory;
 int currentPosition[2] = {0, 0};
 int mass;
};

void createParticle()
{
  struct particle currentParticleCount = {
    random(1, 10),
    random(0, 360),
    {random(1, MAX_SCREEN_HEIGHT), random(1, MAX_SCREEN_WIDTH)},
    random(1, 10),
  };
  currentParticleCount++;
}

void runParticles()
{
  for (int i = 0; i < currentParticleCount; i++)
  {
    particle.i.currentPosition[0] = particle.i.currentPosition[0] + ((particle.i.velocity) * cos(particle.i.trajectory));

    particle.i.currentPosition[0] = particle.i.currentPosition[0] + ((particle.i.velocity) * sin(particle.i.trajectory));
  }

}

I get the following errors:

_particle: In function 'void createParticle()':

144:25: warning: no 'operator++(int)' declared for postfix '++', trying prefix operator instead [-fpermissive]
     currentParticleCount++;
                         ^

_particle:144: error: no match for 'operator++' (operand type is 'particle')

_particle: In function 'void runParticles()':

_particle:151: error: expected unqualified-id before '.' token
     particle.i.currentPosition[0] = particle.i.currentPosition[0] + ((particle.i.velocity) * cos(particle.i.trajectory));
             ^

_particle:153: error: expected unqualified-id before '.' token
     particle.i.currentPosition[0] = particle.i.currentPosition[0] + ((particle.i.velocity) * sin(particle.i.trajectory));
             ^

no match for 'operator++' (operand type is 'particle')

Solution

  • Here's a version that will run. It is far from a proper C++ version, it doesn't use the standard library or any of the newer C++ standards niceties but will hopefully show you where you're going wrong:

    #include <stdlib.h>
    #include <time.h> 
    #include <math.h>
    
    using namespace std;
    
    // Declare the struct
    struct particle
    {
     int velocity;
     int trajectory;
     int currentPosition[2]; // another struct such as struct point {int x; int y;} would be better here
     int mass;
    };
    
    const int PARTICLE_COUNT = 100; // A constant with the number of particles
    const int MAX_SCREEN_HEIGHT = 1080;
    const int MAX_SCREEN_WIDTH = 1920;
    
    int random(int from, int to) {
        return rand() % (to - from) + from; 
    }
    
    // Gets a reference to the particle and initializes it
    void initParticle(particle& currentParticle) {
        currentParticle.velocity = random(1, 10);
        currentParticle.trajectory = random(0, 360);
        currentParticle.currentPosition[0] = random(1, MAX_SCREEN_HEIGHT);
        currentParticle.currentPosition[1] = random(1, MAX_SCREEN_WIDTH);
        currentParticle.mass = random(1, 10);
    }
    
    // pass the array as reference
    void runParticles(particle (&particles)[PARTICLE_COUNT]) {
        for (int i=0; i<PARTICLE_COUNT; ++i) {
            particles[i].currentPosition[0] = particles[i].currentPosition[0] + ((particles[i].velocity) * cos(particles[i].trajectory));
        }
    }
    
    // pass a pointer to the first element in the array
    // it's good style to pass the size as well in this case
    void otherRunParticles(particle* particles, int size) {
        for (int i=0; i<size; ++i) {
            particles[i].currentPosition[0] = particles[i].currentPosition[0] + ((particles[i].velocity) * cos(particles[i].trajectory));
        }
    }
    
    int main()
    {
        srand(time(NULL));
        
        // declare an array of `struct particle` called `particles`
        particle particles[PARTICLE_COUNT];
        
        // initialize the array
        for (int i=0; i<PARTICLE_COUNT; ++i) {
            initParticle(particles[i]);
        }
    
        // Now you can do whatever you want with the particles
        runParticles(particles);
        
        otherRunParticles(particles, PARTICLE_COUNT);
    
        return 0;
    }