Show quoted text 22 lines
> Rather..
>
> var
> i: integer;
> begin
> i:= StrToInt(s);
> if (s = '0') or (i 0) then
> writeln('S is an integer, and i is now: ', i)
> else
> writeln('S is not an integer: ', s);
> end;
>
> Alternatively:
>
> function IsInteger(s: string; i: integer);
> begin
> result:= false;
> if (s = '0') or (i 0) then result:= true;
s can be 00 000 0000 etc. as well as +0 +00 etc.
And '-0' '-00'
Army of critical programmer brains can help,
program test; {$mode objfpc} {$H+}
function IsInteger(s: string; i: integer): boolean;
var idx: integer;
begin
result:= true;
if i 0 then exit;
for idx:= 1 to length(s) do
begin
if (idx = 1) then
if (s[idx] = '-') or (s[idx] = '+') then
continue;
if (s[idx] '0') then result:= false;
end;
// could also check for invalid length..
// not sure if 00000000000000000 is acceptable
end;
function StrToInt(s: string): integer;
var dummy: integer;
begin
val(s, result, dummy);
end;
var tmp: string;
IsInt: boolean;
someint: integer;
begin
tmp:= '0000';
someint:= StrToInt(tmp);
IsInt:= IsInteger(tmp, someint);
writeln('Should be true: ', IsInt);
tmp:= '0';
someint:= StrToInt(tmp);
IsInt:= IsInteger(tmp, someint);
writeln('Should be true: ', IsInt);
tmp:= '-0000';
someint:= StrToInt(tmp);
IsInt:= IsInteger(tmp, someint);
writeln('Should be true: ', IsInt);
tmp:= '+0000';
someint:= StrToInt(tmp);
IsInt:= IsInteger(tmp, someint);
writeln('Should be true: ', IsInt);
tmp:= '000ab';
someint:= StrToInt(tmp);
IsInt:= IsInteger(tmp, someint);
writeln('Should be false: ', IsInt);
tmp:= 'x';
someint:= StrToInt(tmp);
IsInt:= IsInteger(tmp, someint);
writeln('Should be false: ', IsInt);
tmp:= '0';
someint:= StrToInt(tmp);
IsInt:= IsInteger(tmp, someint);
writeln('Should be true: ', IsInt);
readln;
end.What about +-+000 and ++-+0 though?
And does StrToInt in the current sysutils check this?
