Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Problem bei IF Then Konstruktion!


vatras - So 10.06.07 15:12
Titel: Problem bei IF Then Konstruktion!
Hallo,

Ich habe folgendes Problem bei einer eig. recht leichten IF Then Konstruktion:

Wenn Edit4 keinen Inhalt hat, soll die Variable test mit dem Wert 0 arbeiten.

Überlegung:


Delphi-Quelltext
1:
If edit4.text=' ' then test:=0;                    


Leider funktioniert, dass nicht so ganz. Das Programm rechnet dann nämlich nicht mit dem Wert 0 weiter.

Ich hoffe ihr könnt mir helfen.


Danniolo - So 10.06.07 15:15

Jetzt fragst du ab, ob das Edit ein Leerzeichen enthält..

Korrekt sollte es heißen:



Delphi-Quelltext
1:
2:
If Trim(Edit4.Text) = '' then 
  test:=0;


Narses - So 10.06.07 15:17

Moin und :welcome: im Forum!

user profile iconvatras hat folgendes geschrieben:
Wenn Edit4 keinen Inhalt hat, soll die Variable test mit dem Wert 0 arbeiten.

Zwei mögliche Lösungen: ;)

a) test := StrToIntDef(Edit4.Text,0);
Wenn Edit4.Text sich nicht in eine Zahl umwandeln lässt, wird der Vorgabewert 0 genommen. :idea:

b)

Delphi-Quelltext
1:
2:
if (Trim(Edit4.Text) = ''then
  test := 0;

Trim() entfernt führende und folgende Leerzeichen und Vergleichen musst du natürlich mit einem Leerstring! ;)

cu
Narses


vatras - So 10.06.07 15:38

Danke für eure Hilfe! Es läuft nun perfekt ;)

//Edit:

Ich habe noch ein Problem bei meinem Programm (was den Notendurchschnitt berechnen soll):


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
var
  Form1: TForm1;
  var eng,ma,de,lat,franz,span,bio,ch,ph,poli,ge,edk,mu,ku,sp,inf,ds,anzahl,gesamt : integer ;
  notendurchschnitt : real;

implementation

{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);
begin
 eng:=strtoint (edit1.text)  ;
 de:=strtoint (edit2.text)    ;
 ma:=strtoint (edit3.text)    ;
 lat:=StrToIntDef(Edit4.Text,0);
 franz:=StrToIntDef(Edit5.Text,0);
 span:=StrToIntDef(Edit6.Text,0);
 bio:=StrToIntDef(Edit8.Text,0);
 ch:=StrToIntDef(Edit9.Text,0);
 ph:=StrToIntDef(Edit10.Text,0);
 poli:=StrToIntDef(Edit11.Text,0);
 ge:=StrToIntDef(Edit12.Text,0);
 edk:=StrToIntDef(Edit13.Text,0);
 mu:=StrToIntDef(Edit14.Text,0);
 ku:=StrToIntDef(Edit15.Text,0);
 sp:=StrToIntDef(Edit16.Text,0);
 inf:=StrToIntDef(Edit17.Text,0);
 ds:=StrToIntDef(Edit18.Text,0);
 anzahl:=18;
 if lat=0 then anzahl-1;
 if franz=0 then anzahl-1 ;
 if span=0 then anzahl-1   ;
 if ch=0 then anzahl-1      ;
 if ku=0 then anzahl-1       ;
 if mu=0 then anzahl-1        ;
 if ds=0 then anzahl-1         ;
 if inf=0 then anzahl-1         ;
 gesamt:=eng+ma+de+lat+franz+span+bio+ch+ph+poli+ge+edk+mu+ku+sp+inf+ds   ;
 notendurchschnitt:=gesamt/anzahl;
 edit7.text:=floattostr (notendurchschnitt) ;


und zwar kommt es nicht zum Start wegen folgender Fehlermeldung: "Anweisung erforderlich, aber Ausdruck vom Typ "Integer" gefunden!"

Mir ist schon klar, wie es zu diesem Fehler kommt. Nur weiß ich keine Alternative!

Ich bitte nochmal um eure Hilfe^^