Recent

Author Topic: Validating this function?  (Read 3260 times)

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Validating this function?
« on: May 29, 2015, 11:55:12 am »
Hiya,


How can I make this function below only allow purely integers and nothing else and be in the range of 11-99?


Code: [Select]

  Procedure GetMove(Var StartSquare, FinishSquare : Integer);
    Begin
      Write('Enter coordinates of square containing piece to move (file first): ');
      Readln(StartSquare);
      Write('Enter coordinates of square to move piece to (file first): ');
      Readln(FinishSquare);
    End;
ShowMessage('Yes');

balazsszekely

  • Guest
Re: Validating this function?
« Reply #1 on: May 29, 2015, 02:45:08 pm »
Quote
@TheOddOne
How can I make this function below only allow purely integers and nothing else and be in the range of 11-99?

Use the Val function(http://lazarus-ccr.sourceforge.net/docs/rtl/system/val.html).

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Re: Validating this function?
« Reply #2 on: May 29, 2015, 03:08:57 pm »
Code: [Select]

  Procedure GetMove(Var StartSquare, FinishSquare : Integer);
  Var
    Code: Integer;
    ErrorType : String;
    Begin
            Write('Enter coordinates of square containing piece to move (file first): ');
            Readln(StartSquare);
            Val(ErrorType,StartSquare,Code);
      If Code <> 0 Then Writeln('Incorrect input! It must be an integer!')
       Else
         Begin
            Write('Enter coordinates of square to move piece to (file first): ');
            Readln(FinishSquare);
       End;
    End; 


I tried the val function as so above, although it still crashes if one enters say the string 'aa';
ShowMessage('Yes');

Bart

  • Hero Member
  • *****
  • Posts: 5289
    • Bart en Mariska's Webstek
Re: Validating this function?
« Reply #3 on: May 29, 2015, 03:58:24 pm »
Don't use:
Code: [Select]
  Readln(StartSquare);

Read a String variable, then try to convert to integer and chck the desired range.
Keep repeating this until the answer is correct.

Untested code:
Code: [Select]
var
  S: String;
...
begin
...
  repeat
    write ('Enter coordinates of square containing piece to move (file first): ');
    readln(S);
    Val(S, StartSquare, ErrCode);
  until (ErrCode = 0) and (StartSquare in [11..99])
...
 

Alternatively you can use the TryStrToInt() function (unit SysUtils), it returns True if conversion succeeds, otherwise false.
Mind you it will still crash if user enters e.g. 99999999999999999999999999999999999999999999999999999999999999

To be toatally safe you have to write your own input routine (e.g. allowing only digits as input, and no more than 2).
This is rather hard in console programming, but since you seem to have Lazarus, why not move to a GUI, where you can e.g. use a TSpinEdit control for input.

Bart

balazsszekely

  • Guest
Re: Validating this function?
« Reply #4 on: May 29, 2015, 04:04:29 pm »
Code: [Select]
function GetMove(const AType: Integer): Integer;
var
  Mess, Str: String;
  Code: Integer;
begin
  case AType of
    1: Mess := 'Enter coordinates of square containing piece to move (file first):';
    2: Mess := 'Enter coordinates of square to move piece to (file first):';
  end;
  repeat
    writeln(Mess);
    readln(Str);
    val(Str, Result, Code);
    if Code <> 0 then
      Writeln('Incorrect input, it must be an integer! Please try again...');
  until Code = 0;
end;

var
  StartSquare, FinishSquare: Integer;
begin
  StartSquare := GetMove(1);
  FinishSquare := GetMove(2);
  writeln(StartSquare);
  writeln(FinishSquare);
  readln;
end. 

PS: I did not see Bart's post. You can add the (Result in [11..99]) condition if you like + change the error message text.
« Last Edit: May 29, 2015, 04:10:41 pm by GetMem »

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Re: Validating this function?
« Reply #5 on: May 29, 2015, 04:57:20 pm »
Code: [Select]

  Procedure GetMove(Var StartSquare, FinishSquare : Integer);
  Var
    Code: Integer;
    ErrorType : String;
    Begin
            Write('Enter coordinates of square containing piece to move (file first): ');
            Readln(StartSquare);
            Val(ErrorType,StartSquare,Code);
      If Code <> 0 Then Writeln('Incorrect input! It must be an integer!')
       Else
         Begin
            Write('Enter coordinates of square to move piece to (file first): ');
            Readln(FinishSquare);
       End;
    End; 


I tried the val function as so above, although it still crashes if one enters say the string 'aa';


Thank you so much for your help! It actually worked and I added some alterations.
ShowMessage('Yes');

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: Validating this function?
« Reply #6 on: May 29, 2015, 10:16:19 pm »
I have one suggestive idea to you try:
Put the code below in lpi ( project1.lpi ) tab:

Code: [Select]
program project1;

{$mode objfpc}{$H+}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX} {$IFDEF UseCThreads}
    cthreads, {$ENDIF}
  {$ENDIF} {$IFDEF WINDOWS}
    Windows, {for setconsoleoutputcp} {$ENDIF}
  SysUtils, Classes, crt;

var
  Success: boolean;
  intOne, intTwo, {intEntered, }ErrCode: integer;

function ValueToSquare( myText: string; var intEntered: integer ): boolean;
var
  s: string;
  mychar, limit: char;
  flag: boolean;
begin
  s:= '';
  limit:= '1';
  mychar:= #0;
  flag:= false;
  Result:= false;
  writeln;
  writeln( 'Please values into 11 up to 99 or Esc to exit >' );
  write( 'Enter coordinates of square ' +  myText + ' (file first) : ');
  repeat
    mychar:= readkey;
    if mychar in [ limit .. '9' ] then
    begin
      s:= s + mychar;
      write( mychar );
      if mychar <> '1' then
        limit:= '0'; { For '10' exclude. }
      if length( s ) = 2 then
       begin
        WriteLn;
        flag:= true;
        Result:= true;
        val( s, intEntered, ErrCode );
      end;
    end
    else
      beep;
  until (( flag ) or ( mychar = #27 ));  { Escape to exit. }
end;

begin

  {$IFDEF WINDOWS}
  SetConsoleOutputCP(CP_UTF8);
  {$ENDIF}

  Success:= false;
  if ValueToSquare( 'containing piece to move', intOne ) then
    if ValueToSquare( 'to move piece to', intTwo ) then
      Success:= true; { ===> Integer results in intOne and intTwo. <===}

  Beep;
  writeln;
  writeln;
  if Success then
    writeln( '.................... Succesful!....................' )
  else
    writeln( '......................Aborted!.....................' );
  writeln;
    writeln( '...............Strike one key to exit..............' );
  repeat until KeyPressed;

end. 

Good luck.
« Last Edit: May 29, 2015, 10:29:14 pm by marcio2003 »
Lazarus 2.0.10 Windows 10 64bits

 

TinyPortal © 2005-2018