Help request


Referring to Free Pascal Tutorial 7 - Guess My Number, the YouTube user from Lithuania writes:


You would be very kind if could help to solve one problem of this tutorial. When the program is running and asks to guess a number I enter a letter and it just goes off.  If I try this on Lazarus it gives me error. Is there solution for this problem?


The problem happens when the computer is expecting an integer or floating-point number and gets a non-number character. This causes the program to crash and / or produce error messages.


Solution: Prevent non-numbers from being placed into integer or floating point variables.


This can be done by using val.


Val  converts strings to numbers


Syntax:

Val(StringVar, NumberVar, ErrorVar);


If Error = 0, then the conversion was successful

If Error is something other than 0 then we know there is a problem.




Integer Bullet Proof Code:



program Project1;


{$mode objfpc}{$H+}


uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


var

  x:real;

  UserInput:string;

  Error:Integer;


begin

  repeat

    write('Enter An Integer: ');

    readln(UserInput);

    val(UserInput, x, Error);


    if Error <> 0 then

     writeln('The Input Is Not Correct, Please Try Again');

  until Error = 0;

  writeln('You Entered: ', x:2:2);


  writeln;

  writeln;

  writeln('Press <Enter> To Quit');

  readln




Floating Point Bullet Proof Code:


program Project1;


{$mode objfpc}{$H+}


uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


var

  x:real;

  UserInput:string;

  Error:Integer;


begin

  repeat

    write('Enter An Integer: ');

    readln(UserInput);

    val(UserInput, x, Error);


    if Error <> 0 then

     writeln('The Input Is Not Correct, Please Try Again');

  until Error = 0;

  writeln('You Entered: ', x:2:2);


  writeln;

  writeln;

  writeln('Press <Enter> To Quit');

  readln




Modified Guess My Number:


program PHS5;


{$mode objfpc}{$H+}


uses

  crt, sysutils,

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


var

  answer, guess, cnt:integer;

  game:string;

  UserInput:string;

  error:integer;


begin

   repeat

     repeat

       clrscr();  { Clears the Screen, needs crt}

       writeln('Please Select A Game: ');

       writeln;

       writeln('A) 10 ');

       writeln('B) 100 ');

       writeln('C) 1000 ');

       writeln;

       write('Selection : ');

       readln(game);

       game := UpperCase(game); {converts to uppercase, needs sysutils}

       randomize;  {Needed to get random numbers}

     until (game = 'A') or (game = 'B') or (game = 'C'); {makes sure that

                                              a correct reponce is given}


     clrscr();


     if game = 'A' then

     begin

       answer := random(10) + 1; {random (10) gets 0 - 9, so add one for 1 - 10}

       writeln('Game 1 - 10');

     end

     else if game = 'B' then

     begin

       answer := random(100) + 1;

       writeln('Game 1 - 100');

     end

     else if game = 'C' then

     begin

       answer := random(1000) + 1;

       writeln('Game 1 - 1000');

     end;    {end if game statement}


     cnt := 0;    {set the counter to zero}


     repeat

       repeat

         write('Please Enter A Guess: ');

         readln(UserInput);

         val(UserInput, guess, error);

         if error <> 0 then

           writeln('Please Enter a Whole Number');

       until error = 0;

         cnt := cnt + 1;


       if guess < answer then

         writeln('Too Low! ')

       else if  guess > answer then

         writeln('Too High! ')

       else

         writeln('You Guessed My Number In ', cnt, ' Guesses! ');


     until guess = answer;  {repeat until the computer's number is guessed}


     repeat

       writeln;

       write('Play Again Y/N : ');

       readln(game);

       game := UpperCase(game);  {convert to uppercase}

     until (game = 'Y') or (game = 'N');  {repeat untill the response is correct}


  until game = 'N'; {repeat until the user select N to quit the game}


end.




Video: Free Pascal Helper Series - Video 5 Bullet Proofing Strings and Numbers With Val (8:32)


































 
























Questions for this series can be submitted at YouTube Personal Message System. Our YouTube name is SchoolFreeware

Previous                 Next