0
votes

im a beginner to Aplication forms in Delphi, i need a little help please. So its basically a program that does Aritmetic count for numbers from Memo box. I wanna also add interval to it. (-15;20> And i wanna do it for all ODD numbers.

Variables are listed here soucet:SUM, pocet:count of numbers, Prumer:Arithmetic mean

procedure TForm1.Button3Click(Sender: TObject);
var soucet,prumer,x:  Real;
i,pocet:Integer;
begin
Memo1.Clear;
soucet:=0;
pocet:=0;
i:=0;
While i<= Memo1.Lines.Count-1 do begin     --
x:=StrToFloat (Memo1.lines[i]);          --
If (x>-5) and (x<=5) then begin
   soucet:= soucet + x;
   inc(pocet);
end;
 inc(i);
end;
If pocet>0 then begin
  prumer:=soucet/pocet;
  Memo1.Text:= floattostr(prumer);
 end
 else Memo1.Text:= 'Žádná čísla z intervalu (-15;20>';

But i only want this code to be for ODD numbers...

procedure TForm1.Button3Click(Sender: TObject);
var soucet,prumer,x:  Real;
i,pocet:Integer;
begin
Memo1.clear;
soucet:=0;
pocet:=0;
i:=0;
While i<= Memo1.Lines.Count-1 do begin     --
x:=StrToFloat (Memo1.lines[i]);          --
If (x>-5) and (x<=5) then begin
  If x mod 2<>0 then begin
    soucet:= soucet + x;
    inc(pocet);
  end;
end;
  inc(i);

end;
If pocet>0 then begin
  prumer:=soucet/pocet;
  Memo1.Text:= floattostr(prumer);
 end
 else Memo1.Text:= 'Žádná čísla z intervalu (-15;20>';

The problem is that it shows : Operator not aplicable to this operand type. What should i do to remove this error ?

1
Welcome to SO, Solomone! On which line is the error shown? - Tom Brunberg
You know that you can do this without iteration? - David Heffernan
"It just doesnt work" is not a valid problem description. You must be precise and clear about what happens. - David Heffernan
I reverted your edit. A new question is, a new question. You asked about a specific compiler error, and your question was well answered. You should accept that answer. You can't keep adding extra questions until we've written and debugged your entire program for you. Please read the area of the site carefully and try to follow the advice. - David Heffernan

1 Answers

1
votes

You have your xdeclared as real but the modoperator works on integer

Either

  • Declare x as integer and use StrToInt, TryStrToInt or StrToIntDef io StrToFloat
  • Truncate the real to an int like this: if Trunc(x) mod 2 <> 0 or even better use the built-in odd function like this: if odd(Trunc(x))

This will solve your immediate problem but you might want to read up on

  • input validation
  • clean code

and not related to your current code but important enough to mention

  • error/resource handling (try...finally)