Hello, guys, and thank you in advance!
I am trying to make the program stop running for six seconds. I don't know, however, how to use the system clock. The code I came up with is something like this:

PROGRAM TIMER;

VAR
LINE:STRING;
X, S:INTEGER;

BEGIN

REPEAT
WRITE ('TYPE ANYTHING TO BEGIN: ');
READLN (LINE);
IF LINE<>'0' THEN
BEGIN
RANDOMIZE;
X := RandSeed
S := X + 6000;
REPEAT
RANDOMIZE;
UNTIL RandSeed >= S
WRITELN ('END OF TIME!');
END;
UNTIL LINE='0';

END.


It works, somehow, but it's not accurate, atleast not as acurate as a real timer. If somebody can help me, please, do so. Thank you, once more!

Dani AI

Generated

A short, practical summary that ties the replies together and gives an easy fix.

Busy-waiting with the random-seed loop in ’s first post will never match a real timer: loops depend on CPU speed and scheduling, and using RNG state as a time source is unreliable. On Windows in Dev‑Pascal (which bundles Free Pascal/GPC), use the runtime’s timing functions instead — they suspend the thread for an accurate number of milliseconds. (dev-cpp.com)

A minimal, reliable approach (console programs) is to call Sleep with milliseconds. For a six‑second pause you pass 6000:

uses SysUtils;

begin
  Sleep(6000);  // pause 6000 ms = 6 seconds
  Writeln('End of time!');
end.

Sleep is provided by the RTL and takes milliseconds. If the compiler complains about the unit, check the uses clause placement and your IDE/compiler settings. (freepascal.org)

Alternatives and cautions: was right that older Pascal exposes a Delay procedure in the CRT unit, but Delay also takes milliseconds (so use Delay(6000) for six seconds) and the old Turbo‑Pascal CRT had calibration issues on very fast machines — another reason to prefer Sleep under Free Pascal. ’s DateUtils/Now approach (compare current time to a target time) is also valid when you need non‑blocking checks or timestamp math. Use DateUtils for safe time arithmetic. (freepascal.org)

Quick troubleshooting: if Dev‑Pascal can’t find SysUtils/Crt, verify the project uses the Free Pascal backend and that the uses clause appears after the program/unit header. For GUI programs avoid blocking Sleep; use timers or a separate thread so the UI stays responsive.

Recommended Answers

All 3 Replies

program Timer;

{$mode objfpc}{$H+}

uses
  Classes, DateUtils, SysUtils
  { add your units here };
var
   TargetTime: TDateTime;
begin;
    TargetTime := IncSecond(Now, 6);
    repeat
    until CompareTime(Now, TargetTime) >= 0;
    WriteLn('Six seconds have elapsed!');
end.

The last time when I used Pascal was ages ago, but I do remember a function delay(), i.e. delay(6); gives u a delay from 6 seconds. Maybe you need to use Crt (or if there is a new name for it) to get it - this you have to check.

I have heard of that command before, but, unfortunately, it doesn't seem to be supported by the edition of Pascal I use (that would be DevPascal). thanks for the tip, anyway!

As for the source code sant by Jackrabbit, I have no clue as to how to use units. You see, I learnt Pascal on my own, using a book written in 1990... You understand that my knowledge is not sufficient to develop something REALLY good. I appreciate your help, though! I really do!

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.