Autor Beitrag
McKinley
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24

Win XP
Borland Delphi 6
BeitragVerfasst: Mo 01.05.06 15:20 
Hi,

eine Berechnung wird in einem Label (lblErgebnis) dargestellt. Dieses Ergebnis soll durch eine If-Abfrage ermittelt werden und dann je nach Ergbenis eine Antwort erscheinen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
// Auswerten der Berechnung
if lblergebnis.Caption < '30' then
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden';

if lblergebnis.Caption >= '30' then
lbltheorieauswertung.Caption := 'Ein Gespräch ist erforderlich';

if lblergebnis.Caption >= '50' then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';

if lblergebnis.Caption = '100' then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';


Zuvor wird die Berechnung wie folgt ausgeführt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
//Variablen dem jeweiligen MaskEdit-Control zuweisen
Theorie1 := StrToFloat(MEditTheorie1.Text);
Theorie2 := StrToFloat(MEditTheorie2.Text);
Theorie3 := StrToFloat(MEditTheorie3.Text);
// Überprüfen ob ein TMaskEdit leer ist
if (FloatToStr(Theorie1) = '') or (FloatToStr(Theorie2) = '') or (FloatToStr(Theorie3) = '') then
   lblErgebnis.Caption := 'Bitte alle Felder ausfüllen'
else // Berechnung starten
   Ergebnis := (Theorie1 * 0.4) + (Theorie2 * 0.4) + (Theorie3 * 0.2);
   lblErgebnis.Caption := FloatToStr((Round(Ergebnis*100))/100);


Die Variablen sind als extended deklariert.

Nun kommt aber eine falsche Auswertung in folgendem Bereich(also bei den If-Abfragen):
Bis zum Ergebnis 3,8 stimmt alles. Bei 4,0 kommt schon die Meldung "Ein Gespräch ist erforderlich"(soll ja eigentlich erst bei 30 kommen). Ab 6,0 kommt " Die Theorie ist bestanden"(soll erst ab 50 kommen). Ab 10,0 kommt dann wieder "Die Theorie ist nicht bestanden"(das stimmt). Ab 10,0 stimmt dann wieder alles, also ab 30 und ab 50 kommen die Meldungen dann wieder richtig.

Hat jemand eine Idee :gruebel:


Zuletzt bearbeitet von McKinley am Mo 01.05.06 15:26, insgesamt 1-mal bearbeitet
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Mo 01.05.06 15:23 
Hallo

ausblenden Delphi-Quelltext
1:
if lblergebnis.Caption >= '30' then					

Du vergleichst so Strings, keine Zahlen. "4,0" ist stringmäßig nunmal größer als "30"

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mo 01.05.06 15:28 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
// Auswerten der Berechnung
if strtoint(lblergebnis.Caption) < 30 then
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden';

if strtoint(lblergebnis.Caption) >= 30 then
lbltheorieauswertung.Caption := 'Ein Gespräch ist erforderlich';

if strtoint(lblergebnis.Caption) >= 50 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';

if strtoint(lblergebnis.Caption) = 100 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
huhn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 272
Erhaltene Danke: 1

WIN XP
D7Pers
BeitragVerfasst: Mo 01.05.06 15:40 
mach doch einfach so
ausblenden Delphi-Quelltext
1:
if strtofloat(lblergebnis.Caption) >= 30 then ...					

und anstatt immer if kannste au mal else if benützen^^
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var e:Extended;
begin
//guck obs wirklich ne zahl ist ;)
if trystrtofloat(lblergebnis.Caption,e)=false then exit;//oder mach sonst was

// Auswerten der Berechnung
if e < 30 then
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden'

else if e >= 30 then
lbltheorieauswertung.Caption := 'Ein Gespräch ist erforderlich'

else if e >= 50 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';

else if e = 100 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';


mfg huhn

//edit:arggh zu langsam *g*

_________________
Quod Erat Demonstrandum-Was zu beweisen war! *THX to Chrissivo!*
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mo 01.05.06 15:47 
user profile iconhuhn hat folgendes geschrieben:

//edit:arggh zu langsam *g*

12 Minuten :rofl: :zwinker:

Natürlich muss es statt strtoint oben strtofloat heißen.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot


Zuletzt bearbeitet von Marco D. am Mo 01.05.06 15:51, insgesamt 1-mal bearbeitet
McKinley Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24

Win XP
Borland Delphi 6
BeitragVerfasst: Mo 01.05.06 15:49 
user profile iconKeldorn hat folgendes geschrieben:
Hallo

ausblenden Delphi-Quelltext
1:
if lblergebnis.Caption >= '30' then					

Du vergleichst so Strings, keine Zahlen. "4,0" ist stringmäßig nunmal größer als "30"

Mfg Frank

Stimmt...daran muss ich mich erst noch gewöhnen, dass man das immer umwandeln muss. Danke.

user profile iconMarco D. hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
// Auswerten der Berechnung
if strtoint(lblergebnis.Caption) < 30 then
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden';

if strtoint(lblergebnis.Caption) >= 30 then
lbltheorieauswertung.Caption := 'Ein Gespräch ist erforderlich';

if strtoint(lblergebnis.Caption) >= 50 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';

if strtoint(lblergebnis.Caption) = 100 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';

Das funzt so nicht.

user profile iconhuhn hat folgendes geschrieben:
mach doch einfach so
ausblenden Delphi-Quelltext
1:
if strtofloat(lblergebnis.Caption) >= 30 then ...					

und anstatt immer if kannste au mal else if benützen^^
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var e:Extended;
begin
//guck obs wirklich ne zahl ist ;)
if trystrtofloat(lblergebnis.Caption,e)=false then exit;//oder mach sonst was

// Auswerten der Berechnung
if e < 30 then
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden'

else if e >= 30 then
lbltheorieauswertung.Caption := 'Ein Gespräch ist erforderlich'

else if e >= 50 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';

else if e = 100 then
lbltheorieauswertung.Caption := 'Die Theorie ist bestanden';


mfg huhn

//edit:arggh zu langsam *g*
Danke. Überprüfen ob es eine Zahl ist brauch ich nicht, da ich TMaskEdit verwende und dort die Eingabe schon auf Zahlen eingegrenzt habe. Deshalb kann ich das weglassen. So...werd mir jetzt mal die richtige Lösung basteln.

Danke an alle für die Antwort.
Saarpoint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 199

Win XP Home, Vista Home Premium
D5 Std, D7 pers., D2005 Pro. SP3, TurboDelphi
BeitragVerfasst: Mo 01.05.06 16:03 
Hi!

Könntest Du Dir die letzte IF-THEN nicht sparen?
Wenn E = 100 ist, dann trifft dies ja schon bei der
vorletzten ( E >= 50 ) zu.

Die Caption-Zuweisung ist ja eh die selbe...

Gruß, Andy

_________________
Standart := false; Standard := true;
huhn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 272
Erhaltene Danke: 1

WIN XP
D7Pers
BeitragVerfasst: Mo 01.05.06 16:05 
@marco D.: habs eingetippt gehabt aba no et abgeschickt^^ und dann hat meine mutter geschriehen xD, als ich zurück kam hab ichs dann abgeschickt^^

_________________
Quod Erat Demonstrandum-Was zu beweisen war! *THX to Chrissivo!*
McKinley Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24

Win XP
Borland Delphi 6
BeitragVerfasst: Mo 01.05.06 16:08 
user profile iconSaarpoint hat folgendes geschrieben:
Hi!

Könntest Du Dir die letzte IF-THEN nicht sparen?
Wenn E = 100 ist, dann trifft dies ja schon bei der
vorletzten ( E >= 50 ) zu.

Die Caption-Zuweisung ist ja eh die selbe...

Gruß, Andy
Eigentlich ja, aber komischerweise kommt dann bei 100 die Meldung "Die Theorie ist nicht bestanden" wenn ich das weglasse. Das liegt vermutlich an der Umwandlung mit StrToFloat usw.
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mo 01.05.06 16:22 
Versuchs mal so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
var eZahl : extended;
    sAusgabe : string;
begin
  if TryStrToFloat(lblergebmos.Caption,e) = false then
    exit;

  case eZahl of
    0..29 : sAusgabe  := 'Die Theorie ist nicht bestanden';
   30..49 : sAusgabe  := 'Ein Gespräch ist erforderlich';
   50..100 :  sAusgabe  := 'Die Theorie ist bestanden';
   else sAusgabe := 'FEHLER!';
  end;

  lbltheorieauswertung.Caption := sAusgabe ;
end;

_________________
Delphi: XE - OS: Windows 7 Professional x64
McKinley Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24

Win XP
Borland Delphi 6
BeitragVerfasst: Mo 01.05.06 16:39 
user profile iconHorschdware hat folgendes geschrieben:
Versuchs mal so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
var eZahl : extended;
    sAusgabe : string;
begin
  if TryStrToFloat(lblergebmos.Caption,e) = false then
    exit;

  case eZahl of
   0..29 : sAusgabe  := 'Die Theorie ist nicht bestanden';
   30..49 : sAusgabe  := 'Ein Gespräch ist erforderlich';
   50..100 :  sAusgabe  := 'Die Theorie ist bestanden';
   else sAusgabe := 'FEHLER!';
  end;

  lbltheorieauswertung.Caption := sAusgabe ;
end;


Dann wird die Zeile case eZahl of markiert und "Ordinal type required" gemeldet. Damit kann ich leider nichts anfangen und die Hilfe hat mir auch nicht weiter geholfen. :?

[edit]OK, das scheint wohl was mit der Deklaration zu tun zu haben. Allerdings konnte ich den Fehler noch nicht finden :autsch:

Ich werd erst mal die "alte" Methode mit der If-Abfrage versuchen hinzubekommen.
McKinley Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24

Win XP
Borland Delphi 6
BeitragVerfasst: Mo 01.05.06 18:04 
:bawling: Ich versteh das nicht. :autsch:
ausblenden Delphi-Quelltext
1:
2:
3:
// Auswerten der Berechnung
if strtofloat(lblergebnis.Caption) < '30' then
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden';

Das will einfach nicht funktionieren. Ich hab in der letzten Stunde meiner Meinung nach alles probiert, aber es kommt immer wieder eine Fehlermeldung "Incompatible types: String and Extended". Ich hab schon "lblergebnis.caption" durch "Ergebnis" ersetzt (berechne Ich ja vorher, siehe erstes Posting), aber auch das und andere Möglichkeiten haben nicht geholfen.

Ich weiß nicht mehr weiter. :autsch: Als es bei einigen Versuchen dann doch compiliert wurde, trat der Fehler wieder auf. Man muss doch nicht jede Änderung abspeichern, damit sie wirkt? Das hab ich nämlich nicht immer gemacht.
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mo 01.05.06 18:09 
Nimm die Anführungszeichen um die 30 weg.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
starsurfer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 334

Win 95, Win 98, Win XP, Win Vista, Linux
D5 Enterprise ,D2005, D6 Personal, Visual C++ Express 2005, C++ Builder 6 E, Dev-C++
BeitragVerfasst: Mo 01.05.06 18:09 
ausblenden Delphi-Quelltext
1:
2:
3:
// Auswerten der Berechnung  
if strtofloat(lblergebnis.Caption) < '30' then  
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden';


da lieg der Fehler:


ausblenden Delphi-Quelltext
1:
strtofloat(lblergebnis.Caption)//hier hast du eine ZAHL vorliegen					


ausblenden Delphi-Quelltext
1:
'30'//hier hast du ein TEXT vorliegen					


Text und Zahlen lassen sich eben nicht vergleichen

mach das:
ausblenden Delphi-Quelltext
1:
2:
3:
// Auswerten der Berechnung  
if strtofloat(lblergebnis.Caption) < 30 then  
lbltheorieauswertung.Caption := 'Die Theorie ist nicht bestanden';

_________________
GEIZ IST GEIL! - Ihr Sozialamt
McKinley Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24

Win XP
Borland Delphi 6
BeitragVerfasst: Mo 01.05.06 18:14 
Oh nein...so ein "einfacher" Fehler :autsch: Jetzt funzt es. Ihr seid ja super :D :D :zustimm: :dance2:

Darauf muss man erst mal kommen als Anfänger. Vielen vielen Dank :zustimm:
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mo 01.05.06 19:16 
user profile iconMcKinley hat folgendes geschrieben:
user profile iconHorschdware hat folgendes geschrieben:
Versuchs mal so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
var eZahl : extended;
    sAusgabe : string;
begin
  if TryStrToFloat(lblergebmos.Caption,e) = false then
    exit;

  case eZahl of
   0..29 : sAusgabe  := 'Die Theorie ist nicht bestanden';
   30..49 : sAusgabe  := 'Ein Gespräch ist erforderlich';
   50..100 :  sAusgabe  := 'Die Theorie ist bestanden';
   else sAusgabe := 'FEHLER!';
  end;

  lbltheorieauswertung.Caption := sAusgabe ;
end;


Dann wird die Zeile case eZahl of markiert und "Ordinal type required" gemeldet. Damit kann ich leider nichts anfangen und die Hilfe hat mir auch nicht weiter geholfen. :?

[edit]OK, das scheint wohl was mit der Deklaration zu tun zu haben. Allerdings konnte ich den Fehler noch nicht finden :autsch:

Ich werd erst mal die "alte" Methode mit der If-Abfrage versuchen hinzubekommen.


ach.. sorry, mein fehler. hab gar nicht gemerkt, dass eZahl ein extended ist. Hab das gerade so auf die schnelle ins wordpad gehauen. btw: wenn das nicht unbedingt ein extended sein muss, würde das mit einem integer wunderbar funktionieren.

_________________
Delphi: XE - OS: Windows 7 Professional x64