Recent

Author Topic: Why are the posts of my column uneven?  (Read 895 times)

Styganthropus Ioanni

  • Newbie
  • Posts: 2
Why are the posts of my column uneven?
« on: May 03, 2024, 03:47:27 pm »
Hello all!
I've been working on getting my program to write all the strings in an array to the screen.. There are quite a few variables in the array, so I've been trying to split it into two columns. However the second column looks rather uneven, not the extent that the text becomes unreadable, but its a bit ugly. I don't understand why it is uneven though.

At the present the code looks like this
Code: Pascal  [Select][+][-]
  1. program Columns;
  2. uses crt;
  3. var
  4.         SkillName : array[1..30] of string = ('Botanik', 'Desarmera och  Gillra fällor', 'Dyrka upp lås', 'Finna dolda ting', 'Främmande kulturer', 'Första hjälpen', 'Geografi', 'Gömma sig', 'Heraldik/genealogi', 'Historia', 'Hoppa', 'Klättra', 'Köpslå', 'Lyssna', 'Navigera', 'Rida', 'Schack och brädspel', 'Simma', 'Sjunga och spela', 'Spåra', 'Stjäla föremål', 'Upptäcka fara', 'Värdesätta', 'Zoologi', 'Övertala', 'Slagsmål', 'Lönnmord', 'Närstrid', 'Skjuta', 'Sjökunnighet');
  5.  
  6.         r : byte;
  7. begin
  8.         for r := 1 to 30 do
  9.         begin
  10.                 Write(SkillName[r],': ');
  11.                 GotoXY (30,WhereY);
  12.                 if (r mod 2 = 0) then WriteLn;
  13.         end;   
  14. end.
  15.  
I thought the GotoXY would make sure that all the strings in the second column start at the same point, but apparently not. What is it that I'm missing here?
« Last Edit: May 03, 2024, 03:50:49 pm by Styganthropus Ioanni »

Handoko

  • Hero Member
  • *****
  • Posts: 5551
  • My goal: build my own game engine using Lazarus
Re: Why are the posts of my column uneven?
« Reply #1 on: May 03, 2024, 04:44:20 pm »
Hello Styganthropus Ioanni,
Welcome to the forum.

After some tests, I believe that issue was a bug when using Write with certain characters. Non-ASCII, unicode or multi-byte, I am not sure that is not my expertise.

My solution is, use only WriteLn.

Code: Pascal  [Select][+][-]
  1. program columns;
  2.  
  3. uses Crt;
  4.  
  5. var
  6.   SkillName: array[0..29] of string = (
  7.     'Botanik', 'Desarmera och  Gillra fällor', 'Dyrka upp lås',
  8.     'Finna dolda ting', 'Främmande kulturer', 'Första hjälpen',
  9.     'Geografi', 'Gömma sig', 'Heraldik/genealogi', 'Historia', 'Hoppa',
  10.     'Klättra', 'Köpslå', 'Lyssna', 'Navigera', 'Rida', 'Schack och brädspel',
  11.     'Simma', 'Sjunga och spela', 'Spåra', 'Stjäla föremål', 'Upptäcka fara',
  12.     'Värdesätta', 'Zoologi', 'Övertala', 'Slagsmål', 'Lönnmord', 'Närstrid',
  13.     'Skjuta', 'Sjökunnighet'
  14.     );
  15.   i: Integer;
  16.  
  17. begin
  18.   for i := 0 to Length(SkillName)-1 do
  19.     case Odd(i) of
  20.       False:
  21.         begin
  22.           GotoXY(1, WhereY);
  23.           WriteLn(SkillName[i],': ');
  24.         end;
  25.       True:
  26.         begin
  27.           GotoXY(30, WhereY-1);
  28.           WriteLn(SkillName[i],': ');
  29.         end;
  30.     end;
  31. end.

Some suggestions for improving the code quality:

Line #6, most Pascal programmers prefer to start an array from 0.
Line #15, programmers usually name the variable i for looping.
Line #15, use integer for looping because many processors are optimized to use integer.
Line #19, I prefer using Odd instead of mod 2.
Line #19, I use select-case statement instead of if-then-else to improve the readability.
« Last Edit: May 03, 2024, 05:17:12 pm by Handoko »

Styganthropus Ioanni

  • Newbie
  • Posts: 2
Re: Why are the posts of my column uneven?
« Reply #2 on: May 03, 2024, 05:17:58 pm »
Yes, that works quite well.

Thank you for the tips, the solution and the welcome!

 

TinyPortal © 2005-2018