Given an array, with data about cars (StringGrid1), I need to enter the name of the car in Edit, if there is one, output the full string in StringGrid2.

procedure TForm1.Button1Click(Sender: TObject); var a,b:array of TCar; f:boolean; i,j,n,m:integer; begin n:=spinedit1.value; setlength(a,n); for i := 0 to n-1 do begin a[i+1].carbrand:=stringgrid1.cells[1,i+1]; a[i+1].registr:=stringgrid1.cells[2,i+1]; a[i+1].lastname:=stringgrid1.cells[3,i+1]; a[i+1].date:=stringgrid1.cells[4,i+1]; end; m:=0; f:=false; repeat if (edit1.Text=stringgrid1.cells[1,m]) then f:=true else m:=m+1; until ((m>n) or f); { if f then showmessage('+') else showmessage('-'); } for i := 0 to m-1 do setlength(b,m); begin stringgrid2.Cells[1,i+1]:=b[i].carbrand; stringgrid2.Cells[2,i+1]:=b[i].registr; stringgrid2.Cells[3,i+1]:=b[i].lastname; stringgrid2.Cells[4,i+1]:=b[i].date; end; end; 

Please tell me what the error is, does not display the appropriate strings in a StringGrid2?

  • Try to write more detailed questions. To get an answer, explain exactly what you see the problem, how to reproduce it, what you want to get as a result, etc. - Kromster

1 answer 1

Not

 for i := 0 to m-1 do setlength(b,m); begin stringgrid2.Cells[1,i+1]:=b[i].carbrand; ... 

a

 setlength(b,m); for i := 0 to m-1 do begin stringgrid2.Cells[1,i+1]:=b[i].carbrand; ... 

I do not understand the meaning of the last cycle. You allocate memory for array b , and then immediately start reading from there. How should b get into the data?

With the first cycle, too, not everything is in order. You allocate memory in an array of n elements. The indexing of dynamic arrays goes from 0 to Length - 1 . You in the first cycle address elements а from 1 to n . Do you have Range Checking enabled in your project options?

  • Thanks, but I have to press bitbtn 3 times to get the whole line) - Artem Belik
  • Range Checking not included - Artem Belik
  • @ArtyomBelyk Turn it on, and never turn it off. - Igor
  • new mistakes arrived)) I will understand - Artem Belik
  • @ArtyomBelyk - Good luck! - Igor