Search code examples
c++ncurses

It seems like ncurses is causing my loop to break early


I'm running a program using ncurses with a loop that is supposed to generate a window for character movement, wait ~five seconds, clear the window, and start again. My thought is to have this be in a nested window that will do this ten times. However, after the first five seconds ends, the entire loop is broken and the program finishes.

int i=0;
while(i < 10)
{
   playwin.makeWindow();
   wtimeout(playwin.getWindow(), 0);

   originalTime = time(NULL);
   while(newTime < 5)
   {
      newTime = time(NULL);
      newTime -= originalTime;
      player.display();
      playwin.drawWindow();
      player.getMv();
   }
   wclear(playwin.getWindow());
   i++;
}

I expect that the window gets made, the wtimeout function will stop the getMv function from blocking, the nested while loop will display the window and allow the player to move around. After about five seconds, I expect that while loop to end, the window to get cleared, the iterator to increase, and for the loop to start again.

However, after the nested while loop ends, the iterator is already at 10 (verified with printf), so the whole loop breaks.


Solution

  • However, after the nested while loop ends, the iterator is already at 10 (verified with printf), so the whole loop breaks.

    there is no visible initialization of newtime, if its initial value (may be undefined if there is no init at all) is not less than 5 the while(newTime < 5)... is never executed because newTime never changes and i become quikly 10

    If its initial value if less than 5 the while(newTime < 5)... is only executed one time because you do not reset its value after

    Just add for instance newTime = 0; (or a value less than 5) before while(newTime < 5) ...