0

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 ?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Solomone
  • 13
  • 5
  • Welcome to SO, Solomone! On which line is the error shown? – Tom Brunberg Apr 15 '15 at 05:06
  • You know that you can do this without iteration? – David Heffernan Apr 15 '15 at 06:19
  • "It just doesnt work" is not a valid problem description. You must be precise and clear about what happens. – David Heffernan Apr 15 '15 at 10:05
  • 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 Apr 15 '15 at 12:02

1 Answers1

1

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)
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • Tank you very much. I apreciate your answer – Solomone Apr 15 '15 at 05:28
  • @Solomone - No problem. If this solves your question, you can mark it as such by using the checkmark next to it. – Lieven Keersmaekers Apr 15 '15 at 05:37
  • 1
    Better to use the `odd` function. Also, in general, the best error handling involves **not** writing `try/except`. – David Heffernan Apr 15 '15 at 06:05
  • Oh, i just got another problem and i cant solve it.. :( Its just.. Weird.. No error shown, everything look so right, but it isnt.. :D (Ive added more information to topic) – Solomone Apr 15 '15 at 09:12
  • Solomone, use the debugger to step into the code to see what happens. – LU RD Apr 15 '15 at 09:53
  • 1
    @Solomone If this has solved the original question (ie: operator not applicable...) the correct thing to do is to mark it as accepted (put the green checkmark) and if you now have a new question, then ask that as a separate new question. – J... Apr 15 '15 at 11:25
  • 1
    :D Thanks for your replies guys.. It was just .. stupid mistake.. The Memo1.Clear cant be there.. It is in form1.Create :D – Solomone Apr 15 '15 at 13:12