0

As it's said in the title Im having trouble finding a solution on how to check if a string PW contains a number or not. How can i check in TP if the string PW contains a digit?

repeat
        writeln;
        writeln('Ok, please enter your future password.');
        writeln('Attention: The Text can only be decoded with the same PW');
        readln(PW);
        pwLength:= Length(PW);
        error:=0;
          for i:= 1 to Length(PW) do begin
            if Input[i] in ['0'..'9'] then begin
            error:=1;
            end;
          end;

           if Length(PW)=0 then
           begin
           error:=1;
           end;

            if Length(PW)>25 then
            begin
            error:=1;
            end;

        if error=1 then
        begin
        writeln('ERROR: Your PW has to contain at least 1character, no numbers and has to be under 25characters long.');
        readln;
        clrscr;
        end;

      until error=0;
  • You've got 2 down-votes and 3 votes to close. This is probably because your q lacks a clear statement of what you are trying to do regarding numeric characters in the password and the writeln texts are not in English. If you don't update your q to fix these things, it'll likely get two more votes-to-close and then no-one will be able to post an answer. – MartynA Sep 30 '16 at 19:22
  • As you have read in the introduction to [**Tour**](http://stackoverflow.com/tour), SO is a question and answer site. There's no question in your post. You need to edit your post to clearly state what possible problem you want to ask about and formulate that as a question. Proper formatting of code makes it easier to read, and if the text in the code is important for understanding your question, it must be in english. – Tom Brunberg Oct 01 '16 at 08:06
  • @Tom Brunberg edit the post after your suggestions, thank you for the info. I'm sorry if some posts aren't up to standard for this site as I'm still very unfamiliar with it. –  Oct 01 '16 at 09:35

1 Answers1

2

This is how I would write your code:

var
  PW : String;
  Error : Integer;

const
  PWIsOk = 0;
  PWIsBlank = 1;
  PWTooLong = 2;
  PWContainsDigit = 3;

procedure CheckPassword;
var
  i : Integer;
begin

  writeln;
  writeln('Ok, please enter your future password.');
  writeln('Attention: The Text can only be decoded with the same PW');
  writeln('Your password must be between 1 and 25 characters long and contain no digits.');

  repeat
    error := PWIsOk;
    readln(PW);

    if Length(PW) = 0 then
      Error := PWIsBlank;

    if Error = PWIsOk then begin
      if Length(PW) > 25 then
        Error := PWTooLong;
      if Error = 0 then begin
        for i := 1 to Length(PW) do begin
          if (PW[i] in ['0'..'9']) then begin
            Error := PWContainsDigit;
            Break;
          end;
        end;
      end;
    end;

    case Error of
      PWIsOK : writeln('Password is ok.');
      PWIsBlank : writeln('Password cannot be blank.');
      PWTooLong : writeln('Password is too long.');
      PWContainsDigit : writeln('Password should not contain a digit');
    end; { case}
  until Error = PWIsOk;
  writeln('Done');
end;

These are some of the things to notice:

  • Don't use the same error code value to represent different types of error. Using the same value for different errors just makes it more difficult for you to debug your code, because you can't tell which test gave Error the value 1.

  • Define constants to represent the different types of error. That way, readers don't have to wonder "What does 3 mean" in if error = 3 ...

  • Once you've detected a digit character in the password, there is no point examining the characters after it, hence the Break in my for loop.

  • If I were a user I would be annoyed not to be told what the rules are until after the program tells me I've done something wrong. Tell the use beforehand what the rules are.

  • Actually, it would be better to include an additional constant Unclassified with a value of say, -1, and start each iteration of the loop by assigning Error to it and, in the subsequent steps, test for Error = Unclassified rather than PWIsOk.

  • A case statement is a tidy and easily-maintainable way of selecting one of a number of mutually exclusive execution paths based on an ordinal value.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • thanks a lot for the quick respond, i will try to keep those tips in mind for the rest of the code. lets see how it goes :D –  Oct 01 '16 at 13:53
  • also i would like to ask why it is better to add an additional constant rather than doing it with "PWIsOk"? –  Oct 01 '16 at 19:37
  • @Nikolas: Well, in this case, there is not much difference, but if you had a lot more complicated series of checks (say, with more than one way of concluding that the PW is ok), from experience, I think you'd find it easier to debug and check with Error starting off with a different value. – MartynA Oct 01 '16 at 19:42