Carl Jerwin Gensaya
Carl Jerwin Gensaya

Reputation: 13

Delay() issue in C

Even though I set different Sleep() timer for each print statement, it seems to only do the one with the highest timer. Need some help fixing this. TIA!

#include <stdio.h>
#include <windows.h>

void gotoxy(short int x, short int y);

int main()
{
    int c = 1, d = 1, e, x, y, z, a;
    srand ( time(NULL) );

    for(a = 0; a <= 100; a++) {
        x = rand() % 9 + 1;
        y = rand() % 9 + 1;
        z = rand() % 9 + 1;
        gotoxy(5, 5); 
        Sleep(200); printf("%i", x);
        gotoxy(8, 5); 
        Sleep(50); printf("%i", y);
        gotoxy(11, 5); 
        Sleep(500); printf("%i", z);
   }

   return 0;
}

void gotoxy(short int x, short int y){
    COORD pos = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

Upvotes: 0

Views: 267

Answers (1)

Roman Nikitchenko
Roman Nikitchenko

Reputation: 13046

  1. Don't expect too much accuracy from application-level sleep() - like implementation. Usually their timers can easily miss 10-s of milliseconds.
  2. In accordance to documentation this Sleep() parameter is in milliseconds. So it is easy just to not notice such pauses. Especially having not so accurate implementation.

My recommendation is just to multiply time intervals and check. Or log current wall clock time.

Upvotes: 2

Related Questions