Autor Beitrag
kreuzkrieter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 47



BeitragVerfasst: So 24.04.05 10:48 
hallo,
wir mussten in der schule mit dem befehle val überprüfen, ob in einem editfeld eine zahl vorkommt. bei klick auf einen button, sollte dann ein fenster mit der meldung erscheinen: "fehlerhafte eingabe an Stelle x" (zahlen solten nicht erlaubt sein). wie man umgekehrt auf buchstaben, statt auf zahlen prüft, weiß ich. Aber wie es mit diesem befehl bei zahlen funktioniert nicht. der lehrer hat auch rumprobiert, hats aber auch nicht fertiggebracht.
kann mir irgendjemand den quelltext sagen und erklären????
danke
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 24.04.05 12:00 
Die Delphi Hilfe verrät dir, wie man die Funktion Val einsetzt.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
 S: String;
 I, Code: Integer;
begin
 S := '123abc';
 Val(S, I, Code);
 if Code > 0 then
  ShowMessage(Format('Fehlerhafte Eingabe an Stelle %d.',[Code])); // "Fehlerhafte Eingabe an Stelle 4."
end;
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: So 24.04.05 12:06 
Der Code erklärt sich selbst:
ausblenden 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:
function isnumberinstring (S: String) : integer; //Gibt -1 zurück, wenn keine Zahl vorhanden ist, ansonsten die stelle an der die zahl ist
var l,i : integer;
char : string;

begin
l:=length(s);
result:=-1;
  for i:=1 to l do begin
    char:=copy(s,i,1);
    try
      strtoint(char);
      result:=i;
      exit;
    except
      result:=-1;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var t : integer;
begin
  t:=isnumberinstring(edit1.text);
if t = -1 then showmessage('nein'else showmessage('an stelle '+inttostr(t));
end;

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 24.04.05 12:28 
Ich denke die Aufgabe war so gemeint, wie ich sie gelöst habe. Einfach straight-forward Val.
Deine Problembeschreibung ist vielleicht etwas verkehrt rum formuliert?
Mit Val kannst du nämlich nur rausfinden, wo das erste nicht-Zahlen-Symbol vorkommt (abgesehen vom erlaubten Minus-Symbol am Anfang). Die Funktion dient zur Konversion von String zu Integer.
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: So 24.04.05 12:39 
Na und, ist aber auch ein möglicher Lösungsansatz. Ich lös es zwar nicht mit Val aber in der Schule sind meist auch Alternativlösungen erlaubt.

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
kreuzkrieter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 47



BeitragVerfasst: So 24.04.05 18:48 
danke für die vorschläge,
so wie delfiphan hab ich das auch gemacht, nur wollte ich halt die position der ersten zahl. aber wenn das mit diesem befehl nicht geht, hat sich der lehrer wohl vertan, denn er hats ja selbst im unterricht nicht hingekriegt...