Skip to main content

Thread: C++ Timing Problem


hi all,

facing problem past 2 days, , makes me sick. trying make c++ program count number of goals team score during 1 minute, , write result on external file. , make counter = 0 again, , start counting number of goals next minute. etc.

counter used work fine , believe logical, once added timing, subtracting old time new time (timenew - timeold)and if(result%60 ==0) write goals counter file. program write 0 , not see correct value goal counter.

code:
int igoalcounter = 0;  time_t tout = time(0);//old time  while( wm->getplaymode() != pm_time_over  && bcontloop )//while game running  {        time_t t = time(0);//new time        int seconds = t - tout;                          if(wm->getplaymode() == pm_goal_left)//we scored goal        {                     igoalcounter += 1;                      while(wm->getplaymode() == pm_goal_left)            {                //do nothing            }        }        cout<<endl<<endl<<"igoalcounter after addition = "<<igoalcounter<<endl<<endl;//this print correct value of counter!!        int temp = igoalcounter;        if(seconds % 60 == 0)//if 1 minute passed, write data file.        {          cout<<endl<<endl<<"igoalcounter  = "<<igoalcounter<<endl<<endl;//this prints 0 always!! here problem          ofstream outfile;          char outputfilename[] = "input.txt";          outfile.open(outputfilename, ios::out);          if (!outfile)          {              cerr << "can't open output file " << outputfilename << endl;              exit(1);          }                  outfile<<temp;//igoalcounter;          outfile.close();          igoalcounter = 0;          tout = time(0);                }  }
it not make sense, wish if can me.

have time.
ibrahim

in future, suggest experiment simpler code before plopping test code production code. not able focus on problem, should have issues , decide post on forum, can post minimal code.

here's trivial program may you:
code:
#include <ctime>  #include <unistd.h>  #include <iostream>    int main(int argc, char** argv)  {     int period = atoi(argv[1]); // should have better error checking; atoi() not safe       time_t start = time(0);       while (true)     {        time_t = time(0);          if (now - start == (period - 1))        {           std::cout << period << " seconds have elapsed." << std::endl;           start = now;        }        else        {           std::cout << "delaying 1 second..." << std::endl;           sleep(1);        }     }  }
the period should greater 0, did not place error checking in code.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk C++ Timing Problem


Ubuntu

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support