Use of control structures

Hi guys,

I'll just start off by saying I'm fairly new to C++ so apologies if I say anything stupid.

I'm working with Allegro to make a simple game based on the Solar System. Currently, I find myself doing the same thing for every planet individually. For example;

1
2
3
4
5
6
7
8
earth.x = orbit_x(earth.angle, earth.orbit, earth.diameter);
	earth.y = orbit_y(earth.angle, earth.orbit, earth.diameter);

	mercury.x = orbit_x(mercury.angle, mercury.orbit, mercury.diameter);
	mercury.y = orbit_y(mercury.angle, mercury.orbit, mercury.diameter);

	venus.x = orbit_x(venus.angle, venus.orbit, venus.diameter);
	venus.y = orbit_y(venus.angle, venus.orbit, venus.diameter);


To me, this seems stupid, and I'm sure there must be a way to avoid this. I've had a look through the documentation to no avail. I've also tried using a string array to define each of the planets

string bodies [3] = {"earth", "mercury", "venus"};

and then using a for loop to refer back to this array:

1
2
3
4
5
for(int n=0;n<3;n++)
	{
		bodies[n].x = orbit_x(bodies[n].angle, bodies[n].orbit, bodies[n].diameter);
		bodies[n].y = orbit_y(bodies[n].angle, bodies[n].orbit, bodies[n].diameter);
	}


Now in all honesty this was little more than a guess at how it might work, so unsurprisingly it doesn't. If anyone could suggest a fix, or point me in the right direction, that would be appreciated.

Cheers :)

Close. It doesn't work because strings don't have an x, angle, orbit, or diameter. But, planets do. I assume earth, mercury, and venus are structs?

If so,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct planet
{
//...
};

planet bodies[3]; //bodies[0] is Mercury, bodies[1] is Venus, bodies[2] is Earth (or whatever order)

//initializing
bodies[0].angle = /*something*/;
bodies[1].angle = /*something*/;

//...

for(int n=0;n<3;n++)
{
     bodies[n].x = orbit_x(bodies[n].angle, bodies[n].orbit, bodies[n].diameter);
     bodies[n].y = orbit_y(bodies[n].angle, bodies[n].orbit, bodies[n].diameter);
}
Topic archived. No new replies allowed.