Changing variable in for loop

Hi folks, I have a problem with my code which I can't work out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double Mi = 200*pow(10,30);

cout << "\nEnter accreted mass increment in solar masses \n";
cin >> dm;

cout << "\nEnter total mass accreted onto black hole in solar masses \n";
cin >> total_accreted;

double nsteps = total_accreted / dm;

for (int i=0; i<nsteps; i++)		
	{
		Macc = dm*2*pow(10,30);

                //Lots of equations here...

		Mi = Mi + Macc;
	}


Basically the loop works, but gives the wrong results.

I need, at the end of the loop, to sort of "redefine" Mi as "Mi + Macc". I then need it to repeat the loop, and at the end add another Macc so that Mi becomes "Mi + Macc + Macc", etc.

Hope that makes sense. I assumed that was what the last line of my above code would do, but it doesn't and I have no idea how!

Any help appreciated, oh and please try to keep it "n00b-friendly" ;)
Last edited on
It should behave. Is it possible that some other statement changes Mi?
give me whole code if u want help
keskiverto - so the statement in line 17 should actually work? All other equations inside the loop fundamentally depend on Mi, but that shouldn't change the value of Mi. Starting think something else may be the problem.

topnik1 - if I can't work it out soon then I'll quote the whole code. It's just full of confusing equations so I thought it'd be simpler for people to work out this way.
give me just the pow function, the nsteps and thats all what i need :P
Yes, line 17 should work.

Note though, that you do unnecessary work and nsteps could(?) be integer type.
1
2
3
4
5
6
7
8
9
10
11
const double Mpow = pow(10,30)
double Mi = 200 * Mpow;
..
const size_t nsteps = total_accreted / dm;

const double Macc = dm * 2 * Mpow;
for (size_t i=0; i<nsteps; ++i)		
	{
                //Lots of equations here...
		Mi += Macc; // A += B; is shorthand for A = A + B;
	}

Topic archived. No new replies allowed.