* * *

Author Topic: get back EConvertError  (Read 408 times)

krull

  • New member
  • *
  • Posts: 12
get back EConvertError
« on: April 03, 2017, 01:59:33 pm »
I just tried to program a small calculator.

The program runs but as soon as I hit the 'calculate'-button the error EConvertError appears.

Here the program. Help me please. I don't know witch conversion is meant.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. var
  36.  term1: Extended;
  37.  term2: Extended;
  38.  sepperator: String;
  39.  compTerm :String;
  40.  i : integer;
  41.  buffer : String;
  42.  j : integer;
  43.  begin
  44.   buffer:= '';
  45.   sepperator:='';
  46.   compTerm := Edit1.Text;
  47.  
  48.   for i:=0 to length(compTerm) do
  49.   begin
  50.     if (compTerm[i] = '+') or (compTerm[i] = '-') or (compTerm[i] = '*') or (compTerm[i] = '/')
  51.     then
  52.     begin
  53.       sepperator:= compTerm[i];
  54.       for j:=0 to i-1 do
  55.       begin
  56.         buffer:=buffer + compTerm[j];
  57.       end;
  58.       term1:= StrToFloat(buffer);
  59.       buffer:= '';
  60.       for j:=i+1 to length(compTerm) do
  61.       begin
  62.         buffer:=buffer + compTerm[j];
  63.       end;
  64.       term2:= StrToFloat(buffer);
  65.       buffer:=''
  66.     end;
  67.   end;
  68.   case sepperator of
  69.   '+' : Edit2.Text:= FloatToStr(term1 + term2);
  70.   '-' : Edit2.Text:= FloatToStr(term1 - term2);
  71.   '*' : Edit2.Text:= FloatToStr(term1 * term2);
  72.   '/' : Edit2.Text:= FloatToStr(term1 / term2);
  73.   end;
  74. end;
  75. end.      
  76.  
« Last Edit: April 03, 2017, 02:01:06 pm by krull »

wp

  • Hero Member
  • *****
  • Posts: 3620
Re: get back EConvertError
« Reply #1 on: April 03, 2017, 02:12:43 pm »
Since I don't know what's in Edit1.Text, just a guess: StrToFloat gets an error if you try to convert a string with DecimalSeparator to a floating point value, and the DecimalSeparator is not the one in your system.

Other problems are the loops: compterm and buffer are declared as strings, and the indexes of strings run from 1 to the length of the string, not from 0 to length-1.
Lazarus trunk / fpc 3.0.0 / Win32

krull

  • New member
  • *
  • Posts: 12
Re: get back EConvertError
« Reply #2 on: April 03, 2017, 02:40:46 pm »
Thank you. That I counted the strings from zero was the problem.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus