This code simply receives a very basic 'password' (numbers, letters, nothing fancy). After you press enter, it will display it back. The only special key I'm handling is backspace (code 8 in ASCII).

uses crt;

    pass: string;
    key: char;
    len: integer;



key := chr(0);
pass := chr(0);

write('Write the password:');

while key<>chr(13) do
    key := readkey;
    len := length(pass);
    case ord(key) of
        8: begin
                if len > 0 then
                    delete(pass, len, 1);
            pass := concat(pass, key);

writeln('Password: ', pass);

readln; //pauses the code


You will probably notice that the program block is missing. It was on purpose, since Free Pascal Compiler has issues with it.

The code compiles / runs fine, to the best of my knowledge. Remember that this is simply and purely made for ASCII 7-bit (I think the extended set will work).

In terms of clarity, structure and indentation, what else can I improve?

  • \$\begingroup\$ Can I ask you what issues did you experience with the program statement? I am experiencing none with Free Pascal Compiler version 2.6.4+dfsg-4 [2014/10/14] for x86_64 \$\endgroup\$ – cpicanco Aug 21 '15 at 13:44
  • \$\begingroup\$ @cpicanco I can provide a printscreen when I get home. I use Notepad++ as my IDE and I use the binary directly to compile it. I have the latest version, but I'm not sure which one exactly. \$\endgroup\$ – Ismael Miguel Aug 21 '15 at 14:05
  • \$\begingroup\$ @cpicanco With so many specifications about the environment, I forgot to inform about the issues with the compiler. When I wrote program anyname; it would complain that the block wasn't recognized or similar \$\endgroup\$ – Ismael Miguel Aug 21 '15 at 14:14
  • \$\begingroup\$ Well, take your time. I was using Sublime Text on Linux, for instance. \$\endgroup\$ – cpicanco Aug 21 '15 at 15:13


  1. From the Coding Style recommendation, I am using 2-space indentation, which works for me. The guide suggests that you should avoid tabs as well:

    Do not use TAB characters (ASCII HT, 0x09). There is no standard default TAB setting, so the look of source files using TAB characters will depend on client settings. This may result in a chaotic view of source files. Align by space characters (also see Indentation).

  2. However, the same coding style says that you should not use space around operators, symbols...:

    Don't use spaces around operators, colons, parentheses etc. e.g. write p:=p+i; instead of p := p + i ;.

    Since I am not using colored symbols, I've chosen to follow the "Guideline of the Lazarus 1.4.0 color preview":

    enter image description here

    As you can see, the color preview of the IDE uses spaces around symbols (:=, +, -, *, and so on). Whoever wrote this preview is a point out of curve like me.


  1. Repeat is a posttest loop, and your code needs to run at least once, so why not a repeat..until loop.. In another words, you know the first key will always be different from #13, you hardcoded it. You can save a begin..end block, as well.

  2. You do not need begin..end blocks for one line statements inside case conditions.


For code reuse and exploration I would find it more practical to create a function that would only wait for and then return the password:

program HandlePassword;


function WaitForPassword: string;
  key: char;
  len: integer;
  Result := Chr(0);
  key := Chr(0);
    key := ReadKey;
    len := Length(Result);
    case Ord(key) of
      8: if len > 0 then Delete(Result, len, 1);
      else Result := Result + key;
  until key = Chr(13);

  ClrScr; // clear screen
  Write('Write the password:');
  ReadLn; // pauses the code
| improve this answer | |
  • \$\begingroup\$ I agree with everything in your code, except the assumption that the key will never be 13. What if the user simply presses enter? I use tabs for a simple reason: tabs can have any width you wish: 2 spaces, 4 spaces, 10 spaces, 50.... It's up to you \$\endgroup\$ – Ismael Miguel Aug 21 '15 at 17:35
  • \$\begingroup\$ The code here (CodeExchange) and in Lazarus does not use tabs for a reason. Not all word processors deals with them in the same manner. I should have noted that it is a recommendation in the guideline as well. \$\endgroup\$ – cpicanco Aug 21 '15 at 17:59
  • \$\begingroup\$ About the key issue, I think it was your assumption, not mine. If you do not want blank (#0) passwords, or passwords with length < 6, you could treat those as exceptions, for example. \$\endgroup\$ – cpicanco Aug 21 '15 at 18:04
  • \$\begingroup\$ Well, I always used tabs on everything. But go ahead and make that point. Also, that is quite a keen observation.... Stupid me.... Add that to the answer and I'll accept it. But comment after so I know you have done your changes \$\endgroup\$ – Ismael Miguel Aug 21 '15 at 18:06
  • \$\begingroup\$ Changes about the Tabs were done. \$\endgroup\$ – cpicanco Aug 21 '15 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.