stringif-statementpascalturbo-pascal

Turbo Pascal: check if string contains numbers


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;

Solution

  • 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: