Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some FPC code. The problem, is that it takes two (2) presses of the down arrow key to get to the "bot_bar" procedure.

procedure oneliners;

var  Twriters  : array[1..10] of string;
var  Toneliner : array[1..10] of string;
var  S         : string;
var  Foneline  : string;
var  Ch1       : char;
var  Ch2       : char;
var  Ch3       : char;
var  Count     : byte;
var  Count2    : byte;


//### begin show procedure ######################################

Procedure show;

var Count : byte;

Begin
//  clrscr;
  printf('oneh.ans');
  For Count := 1 To 10 Do Begin;
    write('^[[1;37m+ ^[[0;37m');
    write(Twriters[Count]);
    write('^[[1;30m: ^[[0;37m');
    WriteLn(Toneliner[Count]);
  End;
End; // show

//### end show procedure ########################################
//### begin init procedure ######################################

Procedure init;

Var f1 : textfile;
var Count : byte;

Begin
//  GetThisUser;
  fOneLine := ('/home/imp/imp/data/oneliner.lst');
  If Not fileExists(fOneLine) Then Begin
    Assign(f1, fOneLine);
    ReWrite(f1);
    For Count := 1 To 10 Do Begin
      WriteLn(f1,'Ia! Cthulhu!');
      WriteLn(f1,'IGNATIUS');
    End;
    Close(f1);
  End;

  Assign(f1, fOneLine);
    Reset(f1);
    If IoResult = 0 Then Begin
      For Count := 1 To 10 Do Begin
        ReadLn(f1, Toneliner[Count]);
       ReadLn(f1, Twriters[Count]);
      End;
    Close(f1);
  End;
End; // Init

//### begin bot_bar procedure ###################################

Procedure bot_bar;
Begin
  printf('onen.ans');
  Ch2 := ReadKey;
  If Ch2 = #13 then Begin
    exit;
  End;
  show;
End; // bot_bar
//### end bot_bar procedure #####################################
//### begin top_bar procedure ###################################

Procedure top_bar;

Var f1 : textfile;
var Count : byte;
var ch3 : char;


Begin
  printf('oney.ans');
  Ch3 := ReadKey;
  If Ch3 = #13 then Begin
    printf('oneline.asc');
    Write('^[[3;3H');
    inputl(s,70);
    If s = '' then Begin
      WriteLn('^[[1;30maborted^[[0;37m');
      show;
      Exit;
    End;
    For Count := 1 To 9 Do Begin
      Count2 := Count + 1;
      Toneliner[Count] := Toneliner[Count2];
      Twriters[Count] := Twriters[Count2];
    End;

    tWriters[10]:=thisuser.name;
    tOneliner[10] := s;

    Assign(f1, fOneLine);
    ReWrite(f1);
    For Count := 1 To 10 do Begin
      WriteLn(f1,tOneliner[Count]);
      WriteLn(f1,tWriters[Count]);
    End;
    Close(f1);
  End;
  show;
End; // top_bar

//### end top_bar procedure #####################################
//### begin position procedure ##################################

Procedure position;

Begin
  show;
  Repeat
  printf('onen.ans');
  ch1:=ReadKey;
      case ch1 of
      #0 : begin
             ch1:=ReadKey;
             case ch1 of
             #80 : bot_bar;
             #72 : top_bar;
             end;
           end;
      end;
  until ch1=#13
end;

//### end position procedure ####################################

Begin
  init;
  Position;
End;


What I have tried:

I've tried simplifying the code. Ie, putting the "top_bar" and "bot_bar" procedures into the "position" procedure. No luck. If someone could look at this, and possibly tell me what's wrong with the code, i'd appreciate it.

Thank you
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jun-16 20:46pm    
It would be good if you explained what this program should do, what are those top/bot bars are, and so on.
Main problem with this code is that you hard-code everything. The worse parts of this hard-coding are keys and file path name. Practically, there are no cases when hard-coded file paths can be useful.

Also, FP is a kind of Object Pascal. Why don't you write code with classes? it's not a principle problem here, might be just a matter of convenient; I'm just curious...

What would be the meaning of the key ch1 = #0? Note that in this case you call ReadKey immediately.

Generally, this is poor event-oriented design. Don't be afraid of creating of more procedures/functions. Create a main loop, convert char into some actions (say, represented by some enumeration type) and dispatch do different functions... Even of you end up with a bit longer code, it will be more readable...

I have another advice. Maybe, don't get fixed on the interactive console-only programs. They have very little practical value. Just do some simple exercises, it will be enough. In practice, it's way more important to develop console programs where all input comes in a command line. In my FP article, you can find a well-tested command line parsing unit, by the way... Command-line input is much more convenient for console users.

—SA
Member 12551108 8-Jun-16 21:37pm    
Thanks for your input. The "bot_bar" is a procedure, that, when invoked exits the program. The "top_bar" is a procedure, that, when invoked adds a "oneliner" (a message thats just that, 1 line). The truth is, i'm a newbie to (Free) Pascal, and i'm not experienced enough to even know what "classes" are.
Member 12551108 8-Jun-16 22:02pm    
I think you're right about this:

#0 : begin

I don't think it belongs there.
Question is, what do I change it to?
Member 12551108 8-Jun-16 22:15pm    
Let me expand even further. I run a BBS (Impulse BBS), and this code is for a "oneliners" program that's internal in the code. Meaning the BBS doesn't invoke it as if it were an external executable. There are two choices 1.) Add a Oneliner (top_bar) and 2.) Exit the program (bot_bar). The arrow keys, up arrow (scancode #72) moves up to the top_bar procedure. And the down arrow moves down (scancode #80) to the bot_bar procedure.

Does that help?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900