How to initialize the variables after a certain calculations (Help)

 

Hi Friends,

I know the problem is so simple so that I coudn't solve it as I'm not expert on coding yet...

int a = 0;
int b = 2;

a += b;

 The result will be like a = 0, 2, 4, 6, 8, 10,... It's okay until a certain condition will be fulfilled but my problem is How to initialize the variables again to a = 0 / b = 2 once a condition is fulfilled. Compiling works well as a solution but it must be an automated solution inside the code.

Thank you.

 
Toufik BOUAROUA:

Hi Friends,

I know the problem is so simple so that I coudn't solve it as I'm not expert on coding yet...

 The result will be like a = 0, 2, 4, 6, 8, 10,... It's okay until a certain condition will be fulfilled but my problem is How to initialize the variables again to a = 0 / b = 2 once a condition is fulfilled. Compiling works well as a solution but it must be an automated solution inside the code.

Thank you.


a = 0;
b = 2;
 
honest_knave:


Thank you it's helpful but what if the variables are externals

extern int a = 0;
extern int b = 2;
 
Toufik:

Thank you it's helpful but what if the variables are externals

Same

Unless you declare them using input instead of extern (in which case you can not change their values)

 
Mladen Rakic:

Same

Unless you declare them using input instead of extern (in which case you can not change their values)


I need a solution to initialize their values as if I restart the EA or Compile at the middle of calculations, I think as I mentionned that "Compiling" is the best to understand what I meant. As you know the result of (a += b) is a = 0, 2, 4, 6, 8, 10... until I hit "Compile" button witch leads to initialize (a = 0) & (b = 2) and the calculation restarts again. In short, I need a solution instead of compiling.

Thank you. 

 

You have the solution. When you want to reset the values, use the code:

a = 0;
b = 2;

You need to decide when you want that to happen. Maybe on a new day, maybe after you click a button, maybe after a certain value is reached. Endless choices.

e.g.

a += b;

if(a >= 100)
  {
   a = 0;
   b = 2;
  }
 
honest_knave:

You have the solution. When you want to reset the values, use the code:

You need to decide when you want that to happen. Maybe on a new day, maybe after you click a button, maybe after a certain value is reached. Endless choices.

e.g.

The moment when I want that to happen is not a problem for me let's eliminate it from the discussion,

Your code works well in case values are entered inside the code but in this case they are external 

extern int a = 0;
extern int b = 2;

The user may enter different values than the example above this is why I can't use your code.

I don't know maybe a command that restarts the EA ? 

 

Restarting the EA is never the right answer. Any code that needs recompiling / restarting to work correctly has a problem.

Try working with an intermediate variable rather than changing the value of the extern variables directly.

extern int a = 0;
extern int b = 2;

// ...

int c = a;

// ...

c += b;
if(c>=100) c = a;
 
honest_knave:

Restarting the EA is never the right answer. Any code that needs recompiling / restarting to work correctly has a problem.

Try working with an intermediate variable rather than changing the value of the extern variables directly.


You're the Best !!

Reason: