Hi everybody,

I am not new to c++ but am far from a intermediate. I would appreciate any help or suggestions. However, my problem is I need to store dynamic data into different variables. For example time. I am trying to write an if statement that would take time store it in t1 and then when my function time gets called .05 seconds later it will take time and store it in t2 without changing t1.

I know this is simple but I cannot seem to get it. Below is my code. This is my inefficient way of coding it. It should work but for some reason even if I do a cout my t1 will change with time even though it is suppose to stop at .8 seconds.

if(tm<0)
{return;}

if(time>0, time<.8)
{t1=time; y1=position;}

if(time>.1, time<.15)
{t2=time; y2=position;}

if(time>.2, time<.25)
{t3=time; y3=position;}

Like I said I am fairly new, but would an array work better here. If so can I take the values of an array and assign each data box to a variable?

Why don't you capture the salient information before the decision tree. e.g.

struct TRes
{
  double pTime;   // time in seconds 
  double yVal;    // y positions
  A(const double Y) : pTime(time()),yVal(Y) {}
};

// Note this becomes the fixed point
TRes A(position);

if (A.pTime<0.08) 
  y1=A.yVal;
else if (A.pTime<0.15)
// etc...

You might need to have a set of if, since you seem to be assigning y1 AND y2 if the time is say 0.12. But that is just your algorithm requirements.

What you don't want is to access the time and position variable at different points
after each test etc. That will lead to having say t2 >=0.15 or something you don't want.

Thank you, this helps out perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.