Autor Beitrag
GBrainI
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Mi 10.03.10 11:18 
Hallo,

ich habe folgendes Problem :
Ich habe mehrere Zahlen in einem Memofeld untereinander und möchte das diese addiert werden. Jedoch kann sich die anzahl der einträge ändern also es kann sein, dass nur 2zahlen drin stehen oder auch 10. wie mache ich das ?

Kann mir jemand helfen? ich habs so probiert :
ausblenden Delphi-Quelltext
1:
Label2.caption := Inttostr(strtoint(memo1.lines[0]) + strtoint(memo1.lines[-1]));					

aber dann bekomme ich bloß ne fehlermeldung, "is not a ... " ist euch sicherlich bekannt.

was kann ich tun ?

Danke im Voraus

Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
JoelH
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 806
Erhaltene Danke: 17

Win10
Delphi Alexandria 11.2 Patch 1
BeitragVerfasst: Mi 10.03.10 11:28 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
var i, e : Integer;
begin
  e := 0;
  for i := 0 to memo1.Lines.Count - 1 do
  begin
    try
      e := e + StrToInt(memo1.Lines.Strings[i]);
    except
      showmessage('In Zeile '+IntToStr(i+1)+' steht keine Integerzahl!');
    end;
  end;
  label1.Caption := IntToStr(e);
end;

_________________
mfg. Joel
ub60
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 765
Erhaltene Danke: 130



BeitragVerfasst: Mi 10.03.10 11:30 
Die beiden grundlegenden Ideen hast Du ja schon mal (Anzahl der Zeilen und StrToInt).
Jetzt musst Du nur noch eine Schleife drum basteln. Hier mal der Pseudo-Quelltext

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
Summe gleich 0
Anzahl ermitteln
von 0 bis Anzahl-1
  Wert ermitteln
  Wert addieren
Ausgabe


ub60

edit: zu langsam :(
und ich wollte eigentlich zum Denken anregen, keine Lösung präsentieren :)
GBrainI Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Do 11.03.10 17:01 
Super Sache vielen dank, jedoch komm ich direkt mit dem nächsten problem =) wie kann ich den Kommazahlen wie zum beispiel 12,5 + 3,5 welche sich auch in dem selben Memofeld befinden ausrechnen

hab Probiert die StrtoINT einträge durch StrtoFloat zu ändern un statt Integer,Double eingegeben jedoch bringt das nix.

Danke im Voraus
DonManfred
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 148
Erhaltene Danke: 2

Windows 7
Delphi XE3 Pro + HTML5Builder
BeitragVerfasst: Do 11.03.10 17:10 
mach aus 12,5 12.5 dann klappt das auch mit strtofloat

_________________
Gruss Manfred
GBrainI Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Do 11.03.10 17:18 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button2Click(Sender: TObject);
var i, e : Double;
begin
  e := 0;
  for i := 0 to memo1.Lines.Count - 1 do
  begin
    try
      e := e + StrToFloat(memo1.Lines.Strings[i]);
    except
      showmessage('In Zeile '+FloatToStr(i+1)+' steht keine Integerzahl!');
    end;
  end;
  label1.Caption := FloatToStr(e);
end;


[Error] Unit1.pas(32): For loop control variable must have ordinal type
[Error] Unit1.pas(35): Incompatible types: 'Integer' and 'Double'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'


was nun ? :-(

Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: Do 11.03.10 17:20 
user profile iconGBrainI hat folgendes geschrieben Zum zitierten Posting springen:
was nun ? :-(

i muß ein Integer sein.
GBrainI Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Do 11.03.10 17:23 
hmm das minimiert definitiv die fehlernazahl jedoch kommt nun das hier

[Error] Unit1.pas(35): Incompatible types: 'Integer' and 'Extended' wat nu?

in dieser zeile ...
ausblenden Delphi-Quelltext
1:
e := e + StrToFloat(memo1.Lines.Strings[i]);					


Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
SvenAbeln
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 334
Erhaltene Danke: 3



BeitragVerfasst: Do 11.03.10 17:27 
Welchen Type hat den e bei dir?
AScomp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 162


Delphi 5, Delphi 7, Delphi 2007, Delphi 2009, Delphi XE, Delphi 10 Seattle
BeitragVerfasst: Do 11.03.10 20:01 
Ggf. solltest du vorher den DecimalSeparator setzen.

Dann gibt's das Problem mit , und . auch nicht - vorausgesetzt, du sorgst für eine korrekte Dateneingabe im Programm. ;)
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 12.03.10 08:35 
user profile iconGBrainI hat folgendes geschrieben Zum zitierten Posting springen:
hmm das minimiert definitiv die fehlernazahl jedoch kommt nun das hier

[Error] Unit1.pas(35): Incompatible types: 'Integer' and 'Extended' wat nu?
Ich rate mal... ;-)
Nur i soll ein Integer sein, das e muss schon ein double bleiben. Das sieht so aus als hättest du einfach bei beiden wieder den Typ Integer benutzt statt die beiden Deklarationen einzeln zu schreiben und den nur bei i zu ändern...
GBrainI Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 12.03.10 11:48 
Das Leben kann so schön sein =D

Danke sehr =)