Wilna Smith wrote:
> Is there a way in which can use a for-loop to read numerical values 
> from a edit box? I know a Inputbox will work better, but is there a 
> way to make this code work?

It would help if you described what you wanted it to do, and in what way 
it fails to accomplish that.

>    sum := 0;
>    iMax := 0;
>    iMin := 1000000000;

You sort of have the right idea. Set the minimum to a big number so that 
any smaller number forces the minimum down. But what if I enter 
1000000001? If that's the only number I've entered, it's clearly the 
minimum.

Set the minimum to the largest possible value, and set the maximum to 
the smallest possible value. Those are MaxInt and -MaxInt-1, 
respectively. You can also call them High(iMin) and Low(iMax).

>    for j := 1 to 5 do
>       begin
>          sNumber := edtNum.text;
>          iNumber := StrToInt(sNumber);
>          if iNumber > iMax then iMax := iNumber;
>          if iNumber < iMin then iMin := iNumber;
>          sum := sum + iNumber;
>          edtNum.text := '0';
>          edtNum.SetFocus;
>          aPPLICATION.ProcessMessages;
>       end;
>    btnGo.Enabled := false;
>    rAvg := sum / j;

The value of a loop-control variable is _undefined_ once the loop 
terminates.

But don't worry about that; a loop is inappropriate here.

>    lblMax.Caption := 'Maximum value: ' + IntToStr(iMax);
>    lblMin.caption := 'Minimum value: ' + IntToStr(iMin);
>    lblAvg.Caption := 'Average value: ' + FloatToStrF(rAvg, ffFixed, 
> 2,2);
> 
> The loop is only enters one value - due to the .ProcessMessages, else 
> no values gets read.

I think what you want is to read five values from the user, and you want 
to do it with just one edit box instead of five. Each time the user 
presses "Go," you want to add the next number to your collection to get 
statistics.

Phrased that way, the solution using event-based methods practically 
writes itself. If your assignment requires you to use a "for" loop, then 
please provide more details.

"Each time the user presses 'Go'": Make an OnClick event handler for the 
"Go" button. Do one number's worth of processing in that method. Keep 
track of how many numbers you've gotten, and when you've read the right 
number of them, make sure the user can't submit any more.

-- 
Rob

Reply via email to