Recent

Author Topic: How to call from a .txt file into an array record?  (Read 19065 times)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to call from a .txt file into an array record?
« Reply #15 on: March 03, 2013, 04:03:31 pm »
Quote
I keep getting an external SIGSEGV with this code, is it because there are too many variables or something, I know its something to do with memory allocation.
It won't be due to too many variables (the compiler will reject first), neither with memory allocation (you'll get EOutOfMemory instead), but rather memory access. Have you turned on range checking? The only possibility I can see is that one of your array indexing is out of range.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to call from a .txt file into an array record?
« Reply #16 on: March 03, 2013, 04:05:11 pm »
Please post new questions in new threads, mention Lazarus+OS, see link to the faq in my signature below

Haven't looked at the code.

You can format your code first (source/jedi code format) so you get nice indenting etc that often shows errors already.

Also, try going through the code with the debugger (Run/Run or F9) to see where things go wrong.

Thanks

Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #17 on: March 03, 2013, 04:17:54 pm »
How would I know if the issue was with the arrays, how can i fix this if it is the range check thing?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to call from a .txt file into an array record?
« Reply #18 on: March 03, 2013, 04:48:52 pm »
Quote
How would I know if the issue was with the arrays, how can i fix this if it is the range check thing?
Just do as I say and run your program UNDER DEBUGGER, or at least activate also -gl then run your program. It will generate the error name/code and backtrace showing which line causes the error. Fix by modifying your code so the error doesn't happen again.

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #19 on: March 03, 2013, 05:12:55 pm »
When I run it with the debugger it doesn't say anything but a window pops up wit 'external sigsegv' and the assembler opens and shows me the address '40BE15'.

I'm using 32bit V1.0.6 with MINGW debugger.

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #20 on: March 03, 2013, 05:16:49 pm »
I get the same error with this.
Code: [Select]
Crun:=Datetostr(dat);
  SB:=1;
  SC:=2;
  SD:=3;
  SE:=4;
  SF:=5;





  for IP:=1 to 20 do begin
  If (A[IP].date[SB]=crun) or (A[IP].date[SC]=crun) or (A[IP].date[SD]=crun) or (A[IP].date[SE]=crun) or (A[IP].date[SF]=crun) then begin
 W:=W+1;
  end;

  If (B[IP].date[SB]=crun) or (B[IP].date[SC]=crun) or (B[IP].date[SD]=crun) or (B[IP].date[SE]=crun) or (B[IP].date[SF]=crun) then begin
       W:=W+1;
      end;

  If (C[IP].date[SB]=crun) or (C[IP].date[SC]=crun) or (C[IP].date[SD]=crun) or (C[IP].date[SE]=crun) or (C[IP].date[SF]=crun) then begin
     W:=W+1;
  end;

      If (D[IP].date[SB]=crun) or (D[IP].date[SC]=crun) or (D[IP].date[SD]=crun) or (D[IP].date[SE]=crun) or (D[IP].date[SF]=crun) then begin
     U:=U+1;
      end;

  If (E[IP].date[SB]=crun) or (E[IP].date[SC]=crun) or (E[IP].date[SD]=crun) or (E[IP].date[SE]=crun) or (E[IP].date[SF]=crun) then begin
     U:=U+1;
  end;

      If (F[IP].date[SB]=crun) or (F[IP].date[SC]=crun) or (F[IP].date[SD]=crun) or (F[IP].date[SE]=crun) or (F[IP].date[SF]=crun) then begin
      U:=U+1;
      end;

  If (G[IP].date[SB]=crun) or (G[IP].date[SC]=crun) or (G[IP].date[SD]=crun) or (G[IP].date[SE]=crun) or (G[IP].date[SF]=crun) then begin
  X:=X+1;
  end;

     If (H[IP].date[SB]=crun) or (H[IP].date[SC]=crun) or (H[IP].date[SD]=crun) or (H[IP].date[SE]=crun) or (H[IP].date[SF]=crun) then begin
     X:=X+1;
     end;

  If (I[IP].date[SB]=crun) or (I[IP].date[SC]=crun) or (I[IP].date[SD]=crun) or (I[IP].date[SE]=crun) or (I[IP].date[SF]=crun) then begin
  X:=X+1;
  end;

  If (J[IP].date[SB]=crun) or (J[IP].date[SC]=crun) or (J[IP].date[SD]=crun) or (J[IP].date[SE]=crun) or (J[IP].date[SF]=crun) then begin
  X:=X+1;
     end;

  If (K[IP].date[SB]=crun) or (K[IP].date[SC]=crun) or (K[IP].date[SD]=crun) or (K[IP].date[SE]=crun) or (K[IP].date[SF]=crun) then begin
  X:=X+1;
  end;
  end;

  writeln('£',(W*AtoC)+(U*DtoF)+(X*GtoK):8:2);
  readln(da5) 

Peter_Vadasz

  • New Member
  • *
  • Posts: 35
Re: How to call from a .txt file into an array record?
« Reply #21 on: March 03, 2013, 07:27:40 pm »
Please take a look at your arrays declaration. A lot of arrays contains less elements than 20.
Oh, and take a look at this also: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
OS: Ubuntu 12.04.2 32 bit
Lazarus: 1.0.8
FPC: 2.6.2

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #22 on: March 03, 2013, 07:54:34 pm »
Here is my set up for my variables, there are 11 arrays A-K
Code: [Select]
rec =
surname:array[1..5] of string
name:array[1..5] of string
date:array[1..5] of string
az
price


i have A:array[1..20] of rec

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to call from a .txt file into an array record?
« Reply #23 on: March 04, 2013, 08:39:21 am »
Other than program being still horribly illogical in all possible ways, i don't see anything wrong with the code lines you showed, when it comes to indexing. Error is propably somewhere else. I already converted your whole app for dynamic arrays, but you just insist on using the static one...

I mean, trust me when i say,
A:array[1..20] of rec;
B:array[1..20] of rec;
C:array[1..20] of rec;
D:array[1..20] of rec;
E:array[1..20] of rec;
F:array[1..20] of rec;
G:array[1..20] of rec;
H:array[1..20] of rec;
I:array[1..20] of rec;
...
Z:array[1..20] of rec;
... Is not a database for any reasonable person. All you need is 1 array
mydata: array of rec;
« Last Edit: March 04, 2013, 08:46:58 am by User137 »

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #24 on: March 04, 2013, 12:38:09 pm »
What is a dynamic array? How could i improve this?
I don't understand how it is illogical.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to call from a .txt file into an array record?
« Reply #25 on: March 04, 2013, 12:53:57 pm »
dynamic array declaration
Code: [Select]
var
  A: Array of rec;

Usage.....

Code: [Select]
SetLength(A, 20);
For Counter := Low(A) to High(A) do
  A[Counter] := MyCustomRec;

Clean Up
Code: [Select]
SetLlength(A,0);
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to call from a .txt file into an array record?
« Reply #26 on: March 04, 2013, 01:41:13 pm »
I apologize, it will not happen again.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #27 on: March 04, 2013, 01:41:52 pm »
Good.

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #28 on: March 04, 2013, 01:42:38 pm »
Nah, jus' messin'.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to call from a .txt file into an array record?
« Reply #29 on: March 04, 2013, 02:03:07 pm »
I already converted your whole app for dynamic arrays...
Oops sorry, i mixed with another very similar thread. It shows some use of dynamic arrays though.

Quote
How could i improve this? I don't understand how it is illogical.
Lets say we have 5 different schools A, B, C, D, E, and we want to make simple array database of all the students in them. Would you go with 5 arrays "StudentA, StudentB...StudentC"? It is simpler to make with 1 array "student". For example a record:
Code: [Select]
type
  TStudent = record
    name, school: string;
    // add more data if needed...
  end;
  PStudent = ^TStudent;

var student: array of TStudent;

// Add 1 student
procedure AddStudent(const name, school: string);
var index: integer;
begin
  setlength(student, length(student)+1); // Increase array size by 1
  index:=high(student);
  student[index].name:=name;
  student[index].school:=school;
end;

// Print all students that study in school that is given in parameter
procedure PrintCertainSchool(const school: string);
var i: integer;
begin
  writeln('Students in ', school);
  for i:=0 to high(student) do
    if student[i].school=school then
      writeln(i, '. ', student[i].name);
  // Notice how i use indents to tell there starts a code block.
  // Every time there's 2 spaces, i could use begin..end pair if more
  // lines would be required.
end;
And other thing that is illogical is the use of indents, there are spaces here and there. Lazarus converts them to spaces automatically, so it's certainly not a tab character messing with forum.

Other than that, you have written more code than before (we don't know what the program does at all), so giving more assistance might require posting the entire code again.
« Last Edit: March 04, 2013, 02:10:22 pm by User137 »

 

TinyPortal © 2005-2018