-1

I am currently using the CRT to allow for a delay to create a multitude of random numbers without having a bunch of numbers be the same (<- im terrible with grammar). so i used a writeln to see the numbers my script was generating and it wouldnt allow me to see all the numbers.

 For x := 1 to 100 Do
 With people[x] Do
 Begin

  randomize;
  people[x].Address:='4562 South abigail lane';
  people[x].Phonenum:='555-555-1234';
  people[x].licence:='ABC3456789';
  people[x].tickets:=random(5);
  People[x].age:=16+random(10000) mod 80 + 1;
  delay(50);
  writeln(people[x].age);
  End;   

so as you can tell it makes a little issue with checking the numbers. so if possible could someone provide me with an alternative?

  • Remove `crt` unit from your program and use [Sleep() procedure](https://www.freepascal.org/docs-html/rtl/sysutils/sleep.html) from `SysUtils` unit instead of `Delay()`. – Abelisto Apr 20 '18 at 18:44
  • 1
    Use write, can put some space between numbers. – Sertac Akyuz Apr 20 '18 at 18:46

2 Answers2

2

You can use WindMaxY of the crt unit to increase vertical screen buffer.

crt.WindMaxY := 110; 
Randomize;
For x := 1 to 100 Do
  ...

Now you'll be able to scroll up and observe previously written output.


Alternatively you can use cursor position functions in the crt unit to retrieve the cursor position and to set it wherever you want.

Below example outputs the 100 numbers in an easy to read 10x10 tabular format. Note that I removed unrelated code from the example and I also shifted the loop counter to begin from 0 for easy calculation which may require your array to be modified. But of course, instead, you may choose to modify the code example.

var
  x: Integer;

begin
  Randomize;
  WriteLn;
  For x := 0 to 99 Do
  Begin
    GotoXY((x mod 10) * 6, WhereY - 1);
    if WhereX = 1 then
       GotoXY(1, WhereY + 1);
    WriteLn(16 + Random(10000) mod 80 + 1);
  End;

  Readln;
end.


Example output:

example output

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
1

i used a writeln to see the numbers my script was generating and it wouldnt allow me to see all the numbers.

One thing you could do is to add a line

readln; 

after your for loop. That way, you will need to press any key when your app reaches that line. You should then find that you don't need the delay(50); line at all.

Two other things:

1) Move call to Randomize outside the loop. It should be called exactly once, at the start of your program. The FreePascal documentation does not specify this, but it demonstrates it in the example of using Random. The Delphi RTL documentation makes this much clearer:

Do not combine the call to Randomize in a loop with calls to the Random function. Typically, Randomize is called only once, before all calls to Random.

2) (Combined in the code below), get out of the habit of using with, ever. The time is saves in typing by using it is trivial compared with the time you may end up spending trying to track down obscure errors that are caused by using it.

Randomize;
for x := 1 to 100 do
begin
  people[x].Address := '4562 South abigail lane';
  people[x].Phonenum := '555-555-1234';
  people[x].licence := 'ABC3456789';
  people[x].tickets := random(5);
  people[x].age := 16 + random(10000) mod 80 + 1;
  //delay(50);  pointless
  writeln(people[x].age);
end;
readln;  
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • I merged my answer into yours and then deleted mine, as yours better solved the stated problem. Please edit as you'd like so that it is appropriate for your style. I won't be offended. :-) – Ken White Apr 21 '18 at 02:42
  • The delay should not be there. The asker put it in as a bogus workaround to the wrong placement of Randomize. Fix that and the delay just wastes time. – David Heffernan Apr 22 '18 at 22:38
  • The randomized function was used inside the loop to guarantee a random number within the loop instead of the same number multiple times – Mark Slayrin Apr 23 '18 at 14:06