PDA

View Full Version : Delphi error, isn't very informative; need help.



ShowerThoughts
04-13-2010, 01:18 PM
Hey,
So I am getting Delphi classes at school, piece of cake ofcourse and I enjoy it a lot.
But I have an error which doesn't give me enough info to fix it.
Can you help me?




//I have this in the global scope
const
AllowedChars : array[0..10] of char = ('1','2','3','4','5','6','7','8','9','.','0') ;

//the procedure
function Allow(str:string):string;
var
//AllowedChars: array of char;
AllowedLength, i, i2 :integer;
Switch : Boolean;
begin
AllowedLength := 11;
for i := 1 to Length(str) do
begin
for i2 := 0 to (AllowedLength - 1) do
begin
### if (str[i] = AllowedChars[i2]) then Switch := true;//acces violation error.
if (not((i2 =AllowedLength - 1) and (Switch = true))) then Delete(str, i, 1);
end;
switch:=false;
end;
end;

//The event(on edit text change)
//when I type a char as 9 it crashes immidiately
procedure TForm1.txtLengthChange(Sender: TObject);
var
Str:string;
begin
Str := txtlength.text;
txtlength.text := Allow(Str);
end;




I commented the line which gives me the error.
I marked it with ###(the line which gives me the error)

My goal is to chech the input and I want it to be one of my AllowedChars(array of char) so I loop the input string and but before any logical error can be created it already crashes on the first loop. It isn't a synatax error neither logical so something between.

In short: how can I fix the acces violation error on the ### marked line.

I haven't been visiting forums, especially english forums, anymore so my English sucks a lot. I will put this through a spell checker when I get home.

Greetings,
Hermen

nielsie95
04-13-2010, 02:14 PM
You delete something from Str so the length of Str will change, but the loop has the length cached, so it'll loop too far. Try switching to a while statement or perform the deletion on another (temporary) variable.

ShowerThoughts
04-14-2010, 02:59 PM
The fact is even when I give a number it shouldn't delete I get this error but I will try a temporary string! I appreciate your help!
Wait you're right! when I delete the deletion part it actually works!(it doesn't give a error at least)

edit:
I've got it working, thank you very very much!

g0tp0t
04-14-2010, 04:06 PM
const
AllowedChars : array[0..10] of char = ('1','2','3','4','5','6','7','8','9','.','0') ;
u sure that shouldnt be brackets[...] around the array instead of parenthesis(...)?


AllowedLength, i, i2 :integer;
try change the 2 to a b or something, i dont know but i never use numbers in my variables, even if you do it alot, you should try that, it could be something you are skipping causing the error. i do that alot sometimes...:D


for i := 1 to Length(str) do
try length(str)-1

also add a try statement...

try
//code in here shouldnt give any runtime errors, instead they are caught
except
//if the code above gave a runtime error, then this code will run, like writeln('ERROR!')
finally //this is optional
//i think this is run as cleanup after an error, but thats just a guess
end;

in basic it is 'try... catch exception...finally...end try' but im not positive for delphi, someone else can confirm.

also assign a value to switch and see what happens, also maybe rename the var, it could possibly be built into delphi or something.

nielsie95
04-14-2010, 06:37 PM
He already got in to work ;)

In Delphi, you can initialize variables with parenthesis.
Variable names with numbers in them are perfectly fine as long as you don't start with one (this also goes for SCAR/Simba).
Another thing is that string indices start with 1, so String[Length(String)] is, in fact, the last character of the string.
The try statement is a bit different in Delphi: you can have either try..finally or try..except, not both except and finally :)

ShowerThoughts
04-14-2010, 09:33 PM
Ha, thanks mates!

My program made some hell of progress today :), I have been long out of the Delphi business but I am happy with the result I am just having one silly error.

I would love if you could help me!



function CalculateBMI(LengthStr, MassStr:string; Woman:boolean):double;
var
Length2, RealLength: double;
begin
if Woman then Length2 := StrToFloat(LengthStr);
RealLength := ((Length2 - 6) / 100);
result := (StrToFloat(MassStr)) / (RealLength * RealLength);
end;


when I use Breakpoint debug mode it shows to me that LengthStr = '100'
but Length2 contains some super weird number =S and then my Delphi gives an error. when I try to use the Length2 variable, help please!

edit: sec I think I've found the problem :p such an idiot.