Recent

Author Topic: error handling  (Read 1692 times)

Ylainn

  • Newbie
  • Posts: 4
error handling
« on: October 14, 2023, 03:28:17 pm »
how can i make erro handling on this code like you shouldnt enter a number on string code and you shouldnt enter a letter on real code


program vizefinalnothesaplama;
uses crt;
Var vize,final,ortalama:real;
Var adsoyad,ders:string;
Begin
    writeln('Ogrenci adini soyadini girin = ');readln(adsoyad);
    writeln('Ders adini girin = ');readln(ders);
    writeln('Vize notunu girin = ');readln(vize);
    writeln('Final notunu girin = ');readln(final);
    ortalama:=vize*0.4 + final*0.6;
    writeln('Ogrenci Ad Soyad = ', adsoyad);
    writeln('Ders = ', ders);
    writeln('Ortalamanız = ',ortalama:5:1);
    writeln('Kapatmak icin herhangi bir tusa basınız');
    readln;
End.

Josh

  • Hero Member
  • *****
  • Posts: 1346
Re: error handling
« Reply #1 on: October 14, 2023, 04:14:07 pm »
you could try using
TryStrToFloat
ie
if TryStrToFloat('12.25') then it can bea number
else its not a valid number


Code: [Select]
// loop until a valid number is entered
repeat
  readln(avalue);
until TryStrToFloat(avalue)=true;

// loop until a valid non number is entered
repeat
  readln(avalue);
until TryStrToFloat(avalue)=false;


« Last Edit: October 14, 2023, 04:16:48 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Ylainn

  • Newbie
  • Posts: 4
Re: error handling
« Reply #2 on: October 14, 2023, 04:41:27 pm »
can you make me an example i couldnt get it exactly

jcmontherock

  • Sr. Member
  • ****
  • Posts: 274
Re: error handling
« Reply #3 on: October 14, 2023, 04:52:51 pm »
Did you try the "Keyboard" package ? With it you can check each character typed.
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

Ylainn

  • Newbie
  • Posts: 4
Re: error handling
« Reply #4 on: October 14, 2023, 04:54:01 pm »
np i havent try im using on vscode lazarus not working good on my mac

Kays

  • Hero Member
  • *****
  • Posts: 614
  • Whasup!?
    • KaiBurghardt.de
Re: error handling
« Reply #5 on: October 14, 2023, 05:20:53 pm »
Using Pascal’s traditional read procedures you will need use a string variable as Josh already suggested. In FPC it is furthermore possible to temporarily disable input checking:
Code: Pascal  [Select][+][-]
  1. program proceduralInputErrorHandling(input, output, stdErr);
  2.         var
  3.                 i: integer;
  4.         begin
  5.                 repeat
  6.                 begin
  7.                         write('Enter an integer: ');
  8.                         {$push}
  9.                                 {$IOChecks off}
  10.                                 readLn(i)
  11.                         {$pop}
  12.                 end
  13.                 until IOResult = 0;
  14.         end.
The IOResult function returns 106 if the user entered a non-integer.
[…] shouldnt enter a number on string code […]
In this case you will need to attempt a conversion to real. If it succeeds you will need to prompt again.

It is, however, not possible preventing the user from typing certain keys. If you want immediate feedback, your program has to filter keystrokes, for example with the aide of the keyboard unit like jcm********* suggested.
[…]
Code: [Select]
until TryStrToFloat(avalue)=true;
[…]
until TryStrToFloat(avalue)=false;
Please don’t promote = true checks. It’s already terrible enough that occasionally beginners write that.
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018