[Delphi]Przypisanie wartości do tablicy

Witam, główkowałem dosyć długo, ale w końcu nie wymyśliłem, co może być rozwiązaniem mojego problemu, stąd piszę tu.

Problem jest taki, że wywala mi błąd w trakcie pracy programu, kiedy chcę przypisać do elementu tablicy a[y+1] jakąś wartość. Nie ma tego problemu przy pojedynczej zmiennej.

procedure TForm1.Button1Click(Sender: TObject);

  var

  a: array [1..20] of INTEGER;

  b,c, x, q, g: INTEGER;

  r: string;

  y: INTEGER;


begin


b:=StrToInt(Edit1.Caption);

x:=StrToInt(Edit2.Caption);

c:=StrToInt(Edit3.Caption);


if RadioButton1.Checked and RadioButton7.Checked then begin

y:=1;

REPEAT

(!) a[y+1]:=b+x+c;

Inc(y);

UNTIL (y+1=q);


end;

r:=IntToStr(g);

Label2.Caption:=r;

end;

Problem tkwi w linjce (!) a[y+1]. Wywala błąd external SVGHJ czy coś takiego przy tym wierszu.

Bardzo proszę o pomoc.

tablica jest na 20 elementów, a ile wg ciebie wynosi wartość zmiennej “q” ?

nie korzystaj z takich udziwnień, zamień:

y:=1;

REPEAT

(!) a[y+1]:=b+x+c;

Inc(y);

UNTIL (y+1=q);

na:

for y:=2 to q do a[y+1]:=b+x+c;

Natychmiast widać że q - nie zainicjalizowane, jest jeden wiersz zamiast czterech.

@[alex] , jak zwykle pomagasz mi w takiej sytuacji, za co serdeczne dziękuję. Totalny fail, z tą pętlą. Tablica ma ograniczoną wartość, ponieważ szukałem błędu…

Ok, ten problem zażegnany, ale co u licha jest nie tak z kodem

var

  a: array of INTEGER;

  b,c, x, q, g: INTEGER;

  r: string;

  y: INTEGER;

begin    

a[y]:=StrToInt(Edit5.Caption);


q:=StrToInt(Edit6.Caption);

b:=StrToInt(Edit1.Caption);

x:=StrToInt(Edit2.Caption);

c:=StrToInt(Edit3.Caption);


if RadioButton1.Checked and RadioButton7.Checked then begin

for y:=1 to q do begin a[y+1]:=b+x+c; end;


end;

r:=IntToStr(g);

Label2.Caption:=r;

end;

Tablica a jest typu integer, chce zamienić text na integer, spoko, czyli StrToInt powinno śmigać, ale niestety znowu wywala mi przy a[y]:=StrToInt(Edit5.Caption) błąd, ten external bla bla. Proszę o pomoc.

Dokładnie to samo.

a[y]:=StrToInt(Edit5.Caption);

a - ma rozmiar zero elementów.

y - ma nieznaną wartość.

dla tablic dynamicznych ustawiasz rozmiar tak: SetLength(a,100);

teraz możesz użyć od: a[0] do a[99];

Ten kod jest totalnie bez sensu, może powiedz co próbujesz wykombinować.

Program działa, dzięki jeszcze raz [alex], jeszcze tylko jedna sprawa: czy da się ustawić jakoś Caption dla Edit? Bo w Lazarusie nie mogę tego znaleźć w menu Właściwości… Chodzi mi o to, żeby zamiast napisu “Edit1” pojawiało się puste pole… Pozycja Name na niewiele się sprawdza. Szukałem w google - bez skutku.

Dodane 13.11.2012 (Wt) 19:42

Ok, znalazłem w pozycji “ulubione” przy “Tekst”, prawidłowo?

:smiley:

Dodane 13.11.2012 (Wt) 19:48

Dobra, sprawa nadal aktualna, wszystko śmiga talala i błędzioch tutaj:

GetTextMethod := TMethod(@Self.GetTextBuf);

(plik control.inc)

Chyba się jeszcze długo namęczę…

Kod dla całego Buttona:

procedure TForm1.Button1Click(Sender: TObject);

  var

  a: array of Extended;

  b,c: Extended;

  r: string;

  y,q: integer;

 z:integer;

begin

Liczba1.Caption:=' ';

Liczba2.Caption:=' ';

Edit5.Caption:=' ';

Edit6.Caption:=' ';

y:=1;

a[y]:=StrToFloat(Edit5.Caption);

q:=StrToInt(Edit6.Caption);

b:=StrToFloat(Liczba1.Caption);

c:=StrToFloat(Liczba2.Caption);

  SetLength(a,100);


if RadioButton1.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b+a[y]+c;

if RadioButton1.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b+a[y]+c;

if RadioButton1.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b+a[y]*c;

if RadioButton1.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b+a[y]/c;

if RadioButton2.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b-a[y]+c;

if RadioButton2.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b-a[y]-c;

if RadioButton2.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b-a[y]*c;

if RadioButton2.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b-a[y]/c;

if RadioButton3.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b*a[y]+c;

if RadioButton3.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b*a[y]-c;

if RadioButton3.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b*a[y]*c;

if RadioButton3.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b*a[y]/c;

if RadioButton4.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b/a[y]+c;

if RadioButton4.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b/a[y]-c;

if RadioButton4.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b/a[y]*c;

if RadioButton4.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b/a[y]/c;

if NOT RadioButton1.Checked and NOT RadioButton2.Checked and NOT RadioButton3.Checked and NOT RadioButton4.Checked and NOT RadioButton7.Checked and NOT RadioButton8.Checked and NOT RadioButton9.Checked and NOT RadioButton9.Checked and NOT RadioButton10.Checked then Panel2.Caption:='Błąd!';


r:=FloatToStr(a[y]);

Panel2.Caption:=r;

end;

Dla edit’ów wlaściwość: Text

Z tym: GetTextMethod := TMethod(@Self.GetTextBuf); Nie rozumiem o co ci chodzi.

Zrób w klasie deklarację TBX,TBY:array[0…3] of TRadioButton;

W konstruktorze lub OnCreate:

TBX[0]:=RadioButton1;

TBX[1]:=RadioButton2;

TBX[2]:=RadioButton3;

TBX[3]:=RadioButton4;

TBY[0]:=RadioButton7;

TBY[1]:=RadioButton8;

TBY[2]:=RadioButton9;

TBy[3]:=RadioButton10;

Po czym cały kod dla “button’a” możesz zrobić w 10 wierszach.

i zacznij używać indeksacji od zera.

Project Projekt1 raised exception class “External: SIGSEGV” i przenosi mnie do linijki błędu, standardowego pliku lazurowskiego, konkretnie linijka

GetTextMethod := TMethod(@Self.GetTextBuf);

Zamieniłem StrToFloat na r:=FloatToStrF(a[y], ffGeneral, 16, 4); , ale niestety bez skutku. Podejrzewam, że znowu coś z tym stringiem. Przegrzebałem kod, ale nie zauważam w nim nic złego…

Jak ci wywala wyjątek to zobacz call stack i sprawdź w którym miejscu.

Podejrzenie wywołanie metody wirtualnej dla nie zainicjalizowanego obiektu, np:

var f:TFileStream;

f.Read(…);

Obecnie jestem pewien, że chodzi o tę część kodu:

y:=1;

a[y]:=StrToFloat(Edit5.Caption);

q:=StrToInt(Edit6.Caption);

b:=StrToFloat(Liczba1.Caption);

c:=StrToFloat(Liczba2.Caption);

Zamiana String na Float wywołuje u lazarusa niestrawność

Czy jest jakiś zamiennik tego?

const UzywamZnakDziesietny:Strintg=’,’;

StrToFloat(StringReplace(Trim(Edit5.Caption),UzywamZnakDziesietny,DecimalSeparator,[]));

Bez rezultatu…ten sam błędzioch…

Wiem, że kod jest zaśmieciony i nieuporządkowany, ale aktualnie chodzi mi tylko o to, żeby działał…

unit Unit1;


{$mode objfpc}{$H+}


interface


uses

  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,

  ExtCtrls;


type


  { TForm1 }


  TForm1 = class(TForm)

    Button1: TButton;



    Liczba1: TEdit;

    Liczba2: TEdit;

    Edit5: TEdit;

    Edit6: TEdit;

    Panel2: TPanel;

    Label2: TLabel;

    Label5:TLabel;


    RadioButton1: TRadioButton;

    RadioButton10: TRadioButton;

    RadioButton2: TRadioButton;

    RadioButton3: TRadioButton;

    RadioButton4: TRadioButton;

    RadioButton7: TRadioButton;

    RadioButton8: TRadioButton;

    RadioButton9: TRadioButton;

    RadioGroup1: TRadioGroup;

    RadioGroup2: TRadioGroup;


    procedure Button1Click(Sender: TObject);




  private

    { private declarations }

  public

    { public declarations }

  end;


var

  Form1: TForm1;

const UzywamZnakDziesietny:String=',';


implementation


{$R *.lfm}


{ TForm1 }






procedure TForm1.Button1Click(Sender: TObject);

  var

  a: array [1..150] of Extended;

  b,c: Extended;

  r: string;

  y,q: integer;


begin

y:=1;

a[y]:=StrToFloat(StringReplace(Trim(Edit5.Caption),UzywamZnakDziesietny,DecimalSeparator,[]));

q:=StrToInt(Edit6.Caption);

b:=StrToFloat(StringReplace(Trim(Liczba1.Caption),UzywamZnakDziesietny,DecimalSeparator,[]));

c:=StrToFloat(StringReplace(Trim(Liczba2.Caption),UzywamZnakDziesietny,DecimalSeparator,[]));



if RadioButton1.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b+a[y]+c;

if RadioButton1.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b+a[y]+c;

if RadioButton1.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b+a[y]*c;

if RadioButton1.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b+a[y]/c;

if RadioButton2.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b-a[y]+c;

if RadioButton2.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b-a[y]-c;

if RadioButton2.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b-a[y]*c;

if RadioButton2.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b-a[y]/c;

if RadioButton3.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b*a[y]+c;

if RadioButton3.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b*a[y]-c;

if RadioButton3.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b*a[y]*c;

if RadioButton3.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b*a[y]/c;

if RadioButton4.Checked and RadioButton7.Checked then

for y:=1 to q do a[y+1]:=b/a[y]+c;

if RadioButton4.Checked and RadioButton8.Checked then

for y:=1 to q do a[y+1]:=b/a[y]-c;

if RadioButton4.Checked and RadioButton9.Checked then

for y:=1 to q do a[y+1]:=b/a[y]*c;

if RadioButton4.Checked and RadioButton10.Checked then

for y:=1 to q do a[y+1]:=b/a[y]/c;



r:=FloatToStrF(a[y], ffGeneral, 16, 4);

   Panel2.Caption:=r;

end;








end.

a pewnie wprowadzasz np:

12.34

?

To jaki w twoim przypadku jest używany znak dziesiętny?

Dzięki [alex] za pomoc :slight_smile:

Mam poczucie, że zmarnowałem Twój czas, ponieważ błąd był tak głupi, że szkoda o nim mówić, jeszcze raz dzięki.