BBB
BBB

Reputation: 59

Can someone please check my timer program in C++

I want to write a program that checks if the changes in the values of X,Y and Z don't exceed a difference of 10 within 5 seconds, the initialValues() function should be called.

I have written the following program but it crashes while running and the program doesn't work. Then I have to force close the program.

below is the main part of my program for timer. My program doesn't crash when I remove this specific part.

How do I fix this?

 while(X<=X+10 || X>=X-10 && Y<=Y+10 || Y>=Y-10 && Z<=Z+10 || Z>=Z-10)
   {
       clock_t t;
       t = clock();
       t = clock()-t;
       float timer = t/CLOCKS_PER_SEC;
       if(timer==5)
       {
           initialValues(X,Y,Z);
           cout<<"X = "<<initialX<<" Y = "<<initialY<<" Z = "<<initialZ<<endl;
       }
   }

Upvotes: 0

Views: 149

Answers (1)

DrD
DrD

Reputation: 439

You don't say what the function initialValues does, so we cannot know whether something dodgy is done there. However, it seems likely that you have an infinite loop. Check the conditions in the while loop, and insert brackets to separate conditions that should be evaluated together, such as, for exmaple:

while( ( X<=X+10 || X>=X-10 ) && ( Y<=Y+10 || Y>=Y-10 ) && ( Z<=Z+10 || Z>=Z-10) )

Note the extra brackets.

In particular, you should check whether the conditions that you have specified are always true, as suggested by another user. It seems to me that X<=X+10 always, and the same applies to the rest of conditions.

Upvotes: 1

Related Questions