Compiles, but doesn't execute. Problem with lists lazarus / 61-Beginners ---------------------------------------------------------------------- medianna · 2018-11-12 00:30 Hi, I have a homework problem where I need to fill in parts of a given program. The program is intended to sort by insertion. Lists have just been introduced in the last part of the lecture but I haven't understood the entire concept. I'm guessing that the while loop goes to an infinite loop, but I can't quite figure out why. Any help is appreciated! Here is the code (I have marked the parts where my codes starts and ends): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program TesteSortiereListe(input, output);{ sortiert eine Liste durch Einfügen }{ das Programm testet, ob die Liste sortiert ist }{ Liste wird in der Prozedur sortiert }   type  tNatZahl = 0..maxint;  tRefListe = ^tListe;  tListe = record             wert : tNatZahl;             next : tRefListe;           end;   var  RefListe : tRefListe;   procedure SortiereListe (var ioRefListe : tRefListe);   { variable ioRefListe zeigt auf das erste Element der Liste RefListe }  { sortiert eine nicht-leere lineare Liste aufsteigend }     var    { Listenobjekte }    EinfuegePosition,    WirdJetztSortiert,    IstSchonSortiert : tRefListe;         istEingefuegt : boolean;   begin    { die einelementige Liste ist immer sortiert }    IstSchonSortiert := ioRefListe;    { wir beginnen das zweite Element zu sortieren }    WirdJetztSortiert := ioRefListe^.next;    { solange es noch etwas zu sortieren gibt }    while WirdJetztSortiert <> nil do    begin      istEingefuegt := false;     {THIS IS WHERE MY CODE STARTS }      { Fall 1: WirdJetztSortiert ist das neue kleinste Element }          if WirdJetztSortiert^.wert < ioRefListe^.wert then          begin                IstSchonSortiert^.next := WirdJetztSortiert^.next;                WirdJetztSortiert^.next := ioRefListe;                ioRefListe^.next := WirdJetztSortiert        end;           {Fall 2: WirdJetztSortiert bleibt wo es ist }         if WirdJetztSortiert^.wert > IstSchonSortiert^.wert then          begin                 IstSchonSortiert := WirdJetztSortiert          end;       { Fall 3: richtige EinfuegePosition im sortieten Teil suchen und verschieben }       EinfuegePosition := ioRefListe;      while istEingefuegt = false do      begin                  if WirdJetztSortiert^.wert < EinfuegePosition^.next^.wert then                begin                        IstSchonSortiert := WirdJetztSortiert^.next;                        WirdJetztSortiert^.next := EinfuegePosition^.next;                      EinfuegePosition^.next := WirdJetztSortiert;                                              istEingefuegt := true                  end;                                  EinfuegePosition := EinfuegePosition^.next          end; { THIS IS WHERE MY CODE ENDS }          WirdJetztSortiert := IstSchonSortiert^.next    end  end;   procedure Anhaengen(inZahl : tNatZahl; var ioListe : tRefListe);  { Haengt inZahl an ioListe an }      var     Zeiger : tRefListe;      begin    Zeiger := ioListe;    if Zeiger = nil then    begin      new(ioListe);      ioListe^.wert := inZahl;      ioListe^.next := nil;    end    else    begin      while Zeiger^.next <> nil do        Zeiger := Zeiger^.next;      new(Zeiger^.next);      Zeiger := Zeiger^.next;      Zeiger^.wert := inZahl;      Zeiger^.next := nil;  end;  end;   procedure ListeEinlesen(var outListe:tRefListe);  { liest eine Folge von Zahlen ein }      var    Liste : tRefListe;    Zahl : integer;      begin    Liste := nil;    read(Zahl);    while Zahl<>-1 do    begin      Anhaengen(Zahl, Liste);      read(Zahl)    end;    outListe := Liste  end;   procedure GibListeAus(inListe : tRefListe);  { Gibt die eine nicht-leere Liste aus }      var     Zeiger : tRefListe;      begin    Zeiger := inListe;    write(Zeiger^.wert);    Zeiger := Zeiger^.next;    while Zeiger <> nil do    begin      write(', ');      write(Zeiger^.wert);      Zeiger := Zeiger^.next;    end;    writeln('')    end; begin  ListeEinlesen(RefListe);  SortiereListe(RefListe);  GibListeAus(RefListe)end. ---------------------------------------------------------------------- Martin_fr · 2018-11-12 00:40 Added "code tags" for formatting/readability. ---------------------------------------------------------------------- medianna · 2018-11-12 00:41 Quote from: Martin_fr on November 12, 2018, 01:40:42 amAdded "code tags" for formatting/readability. Thanks! Will remember that the next time! ---------------------------------------------------------------------- Martin_fr · 2018-11-12 00:48 Well you should at least get to the input prompt. You can add extra writeln to see where the program goes (or start using the debugger). What OS are you on? On Windows, you should get a black console window, as soon as you run. (If you run from a console then the console is already open, use writeln, to check something happens) If not, you may have the wrong app type. Either -google {$APPTYPE}  -change project options => target => unselect GUI app (Lazarus only) - create a new project => type program (Lazarus only) In Lazarus on Linux you need to open menu > view > Debugger > Console ---------------------------------------------------------------------- medianna · 2018-11-12 01:01 Quote from: Martin_fr on November 12, 2018, 01:48:28 amWell you should at least get to the input prompt. You can add extra writeln to see where the program goes (or start using the debugger). What OS are you on? On Windows, you should get a black console window, as soon as you run. (If you run from a console then the console is already open, use writeln, to check something happens) If not, you may have the wrong app type. Either -google {$APPTYPE}  -change project options => target => unselect GUI app (Lazarus only) - create a new project => type program (Lazarus only) In Lazarus on Linux you need to open menu > view > Debugger > Console I'm on windows and do get the black console window but it doesn't ask me to input anything - which is what I at least expected to happen. When I type in writeln I get this error: writeln() Runtime error 106 at $004015A8   $004015A8   $00401742   $00407701 ------------------ (program exited with code: 106) ---------------------------------------------------------------------- Martin_fr · 2018-11-12 01:16 https://www.freepascal.org/docs-html/user/userap4.html Quote 106 Invalid numeric format     Reported when a non-numeric value is read from a text file, and a numeric value was expected. Not sure why. I just started your app on windows, I can enter numbers, once I type -1 the app outputs some numbers. In some cases it repeats one number forever. So some next pointer, points to its own node. (2 3 9 -1 triggers this endless output) Others stay forever in sort. Keep an eye what happens to the next pointer of nodes that you just sorted. So the new list, does not get any next that redirect in a loop. Do you use Lazarus or fp.exe as ide? Both should allow you do debug (single step) your app. Note: if it does not go into endless output, then the console may close before you can even see the result (depending how you started the app) ---------------------------------------------------------------------- Martin_fr · 2018-11-12 01:25 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i             if WirdJetztSortiert^.wert < ioRefListe^.wert then              begin                    IstSchonSortiert^.next := WirdJetztSortiert^.next;                    WirdJetztSortiert^.next := ioRefListe;                    ioRefListe^.next := WirdJetztSortiert  // zeigt auf das NEUE WirdJetztSortiert   // das NEUE WirdJetztSortiert.next zeigt auf ioRefListe  // Wenn du danach den next pointern folgst, geht es immer im Kreis              end; above creates a loop There may be more, but I am off ---------------------------------------------------------------------- medianna · 2018-11-12 06:09 Quote from: Martin_fr on November 12, 2018, 02:25:52 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i             if WirdJetztSortiert^.wert < ioRefListe^.wert then              begin                    IstSchonSortiert^.next := WirdJetztSortiert^.next;                    WirdJetztSortiert^.next := ioRefListe;                    ioRefListe^.next := WirdJetztSortiert  // zeigt auf das NEUE WirdJetztSortiert   // das NEUE WirdJetztSortiert.next zeigt auf ioRefListe  // Wenn du danach den next pointern folgst, geht es immer im Kreis              end; above creates a loop There may be more, but I am off Thanks for nudging me into the right direction. I have managed to make it work