Recent

Author Topic: [Solved]Identifier not found "StrToIntDef" even though mode objfpc turn on  (Read 751 times)

Terno

  • Newbie
  • Posts: 3
Hello, i am writing a small convert some C function into Pascal code.
It look like this
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$typedAddress on}
  3.  
  4. function StringToInt(var str: string; var endPtr: PChar): Integer;
  5. var
  6.   tempStr: string;
  7.   i: Integer;
  8. begin
  9.   // Trim leading whitespace
  10.   while (str <> '') and (str[1] <= ' ') do
  11.     Delete(str, 1, 1);
  12.  
  13.   // Find the end of the string
  14.   endPtr := Addr(str);
  15.  
  16.   while endPtr^ <> #0 do
  17.     Inc(endPtr);
  18.  
  19.   // Convert the string to integer
  20.   tempStr := Copy(str, 1, endPtr - Addr(str)); // Get substring until endPtr
  21.   Val(tempStr, Result, i);
  22.  
  23.   // Update endPtr to point to the actual end of the substring
  24.   Dec(endPtr);
  25.  
  26.   // Trim trailing whitespace
  27.   while (tempStr <> '') and (tempStr[Length(tempStr)] <= ' ') do
  28.     Delete(tempStr, Length(tempStr), 1);
  29.  
  30.   // Return the integer value
  31.   Result := StrToIntDef(tempStr, 0);
  32. end;
  33.  
  34.  
  35. var
  36.   str: string;
  37.   endPtr: PChar;
  38.   intValue: Integer;
  39. begin
  40.   str := '12345   ';
  41.  
  42.   intValue := StringToInt(str, endPtr);
  43.  
  44.   // Access the end position using endPtr
  45.   Writeln('End position pointer: ', Ord(endPtr^));
  46.  
  47.   // Print the converted integer value
  48.   Writeln('Integer value: ', intValue);
  49. end.
  50.  
  51.  
But the compiler alway throw error
Code: Pascal  [Select][+][-]
  1. Identifier not found "StrToIntDef"
How can I resolve this?
« Last Edit: September 17, 2023, 09:12:23 am by Terno »

Fibonacci

  • Hero Member
  • *****
  • Posts: 653
  • Internal Error Hunter
Re: Identifier not found "StrToIntDef" even though I have turn on mode objfpc
« Reply #1 on: September 17, 2023, 07:29:22 am »
Its function from SysUtils so you need to add it to the uses

https://www.freepascal.org/docs-html/rtl/sysutils/strtointdef.html

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$typedAddress on}
  3.  
  4. uses SysUtils;
  5.  
  6. function StringToInt(var str: string; var endPtr: PChar): Integer;

Terno

  • Newbie
  • Posts: 3
Re: Identifier not found "StrToIntDef" even though I have turn on mode objfpc
« Reply #2 on: September 17, 2023, 07:33:36 am »
Now it run! Thank you!
« Last Edit: September 17, 2023, 08:59:11 am by Terno »

Fibonacci

  • Hero Member
  • *****
  • Posts: 653
  • Internal Error Hunter
Re: Identifier not found "StrToIntDef" even though I have turn on mode objfpc
« Reply #3 on: September 17, 2023, 07:36:28 am »
BTW. This whole program can be shorten to this

Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. var
  4.   str: string;
  5.   int: integer;
  6.  
  7. begin
  8.   str := '12345   ';
  9.   int := StrToIntDef(str.Trim, 0);
  10.  
  11.   writeln('str to int = ', int);
  12. end.

Or

Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. var
  4.   str: string;
  5.   int: integer;
  6.  
  7. begin
  8.   str := '12345   ';
  9.  
  10.   if TryStrToInt(str.Trim, int) then
  11.     writeln('int = ', int)
  12.   else
  13.     writeln('cant do it');
  14. end.

Terno

  • Newbie
  • Posts: 3
Re: Identifier not found "StrToIntDef" even though I have turn on mode objfpc
« Reply #4 on: September 17, 2023, 08:17:57 am »
But I want the function have a pointer to point to the end of the string like strtol in C/C++.
It is a bit pain staking to do this.

cdbc

  • Hero Member
  • *****
  • Posts: 1964
    • http://www.cdbc.dk
Re: [Solved]Identifier not found "StrToIntDef" even though mode objfpc turn on
« Reply #5 on: September 17, 2023, 03:42:59 pm »
Hi
Maybe this can fulfill your requirements:
Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. var
  4.   str: string;
  5.   int: integer;
  6.   EndPtr: pchar;
  7.  
  8. begin
  9.   str := '12345   ';
  10.  
  11.   if TryStrToInt(str.Trim, int) then begin
  12.     EndPtr:= pchar(str[length(str)]);
  13.     writeln('int = ', int)
  14.   end else begin
  15.     EndPtr:= nil;
  16.     writeln('cant do it');
  17.   end; // else ~ failure
  18. end.
  19.  
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018