Recent

Author Topic: Issue with code  (Read 1647 times)

nugax

  • Full Member
  • ***
  • Posts: 232
Issue with code
« on: March 09, 2022, 05:24:00 am »
Can someone tell me whats wrong the this while do loop. It gives an error at the TStringList free command.

Code: Pascal  [Select][+][-]
  1. procedure ListConferences();
  2. var
  3.   iNumbMess: integer;
  4.   iNumbUnreadMess: integer;
  5.   ConferenceIni: TIniFile;
  6.   iCnt: integer;
  7.   sGroup: string;
  8.   iIdxSysMail: integer;
  9.   sConfDesc: string;
  10.  
  11. begin
  12.   lConferenceList := TStringList.Create;
  13.   ConferenceIni := TIniFile.Create(programinfo.cbbsInfo.sMailConferencesIni);
  14.  
  15.   //Load Sections From Conferences
  16.   ConferenceIni.ReadSections(lConferenceList);
  17.  
  18.   //Add System Mail
  19.   lConferenceList.add('local.System_Mail');
  20.   iIdxSysMail := lConferenceList.IndexOf('local.System_Mail');
  21.   lConferenceList.Move(iIdxSysMail, 0);
  22.  
  23.  
  24.  
  25.   cyberio.PipeWriteCRLF;
  26.   cyberio.PipeWriteLnString('|11 Current Conferences:');
  27.  
  28.   iCnt := 0;
  29.   while (lConferenceList.Count > iCnt) do
  30.  
  31.     if (lConferenceList.Strings[iCnt] <> 'local.System_Mail') then
  32.     begin
  33.       sGroup := ConferenceIni.ReadString(lConferenceList.Strings[iCnt], 'group', '');
  34.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + sGroup + '.' + lConferenceList.Strings[iCnt] + '    --> Local System Private Mail');
  35.     end
  36.     else
  37.     begin
  38.       //Get Conference Description
  39.        sConfDesc := GetConfDesc(lConferenceList.Strings[iCnt]);
  40.  
  41.       //Add System Mail Conference
  42.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + lConferenceList.Strings[iCnt] + '    --> ' + sConfDesc);
  43.     end;
  44.     Inc(iCnt);
  45.   end;
  46.  
  47.  
  48.  
  49.   // lConferenceList.Free;
  50.   ConferenceIni.Free;
  51.   cConferences.ConferencePrompt();
  52.  
  53. end;  
-Nugax

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Issue with code
« Reply #1 on: March 09, 2022, 07:12:27 am »
Perhaps placing the two statements in a begin..end; block.
keep it simple

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Issue with code
« Reply #2 on: March 09, 2022, 07:40:37 am »
You didn't tell which error, but the indentation is wrong.
Code: Pascal  [Select][+][-]
  1. procedure ListConferences();
  2. var
  3.   iNumbMess: integer;
  4.   iNumbUnreadMess: integer;
  5.   ConferenceIni: TIniFile;
  6.   iCnt: integer;
  7.   sGroup: string;
  8.   iIdxSysMail: integer;
  9.   sConfDesc: string;
  10.  
  11. begin
  12.   lConferenceList := TStringList.Create;
  13.   ConferenceIni := TIniFile.Create(programinfo.cbbsInfo.sMailConferencesIni);
  14.  
  15.   //Load Sections From Conferences
  16.   ConferenceIni.ReadSections(lConferenceList);
  17.  
  18.   //Add System Mail
  19.   lConferenceList.add('local.System_Mail');
  20.   iIdxSysMail := lConferenceList.IndexOf('local.System_Mail');
  21.   lConferenceList.Move(iIdxSysMail, 0);
  22.  
  23.  
  24.  
  25.   cyberio.PipeWriteCRLF;
  26.   cyberio.PipeWriteLnString('|11 Current Conferences:');
  27.  
  28.   iCnt := 0;
  29.   while (lConferenceList.Count > iCnt) do
  30.  
  31.     if (lConferenceList.Strings[iCnt] <> 'local.System_Mail') then
  32.     begin
  33.       sGroup := ConferenceIni.ReadString(lConferenceList.Strings[iCnt], 'group', '');
  34.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + sGroup + '.' + lConferenceList.Strings[iCnt] + '    --> Local System Private Mail');
  35.     end
  36.     else
  37.     begin
  38.       //Get Conference Description
  39.        sConfDesc := GetConfDesc(lConferenceList.Strings[iCnt]);
  40.  
  41.       //Add System Mail Conference
  42.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + lConferenceList.Strings[iCnt] + '    --> ' + sConfDesc);
  43.     end;
  44.   Inc(iCnt);
  45. end;
  46.      

Besides, when accessing items from a stringlist, you don't need refer to strings-property. These are the same.
Code: Pascal  [Select][+][-]
  1. lConferenceList.Strings[iCnt]
  2. lConferenceList[iCnt]  


Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Issue with code
« Reply #3 on: March 09, 2022, 08:02:26 am »
Code: [Select]
// lConferenceList.Free;
  ConferenceIni.Free;
Where are you free-ing the TStringList?
You're free-ing the INI-File
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

WooBean

  • Full Member
  • ***
  • Posts: 230
Re: Issue with code
« Reply #4 on: March 09, 2022, 08:03:41 am »
Perhaps placing the two statements in a begin..end; block.

munair should omit "perhaps".
A good habit to code while loops is  to start them at the begining as a compound statement ready to insert some simple statements.
Code: Pascal  [Select][+][-]
  1. while condition do
  2. begin
  3.  
  4. end;
  5.  
« Last Edit: March 09, 2022, 09:00:08 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: Issue with code
« Reply #5 on: March 09, 2022, 09:51:19 am »
hi

In your code cint is not increased in the while loop; so goes on forever i suspect, this is because the while loop is only looping the next statement ie the if (lConferenceList.Strings[iCnt]..

Code: Pascal  [Select][+][-]
  1. iCnt := 0;
  2.   while (lConferenceList.Count > iCnt) do
  3.  
  4.     if (lConferenceList.Strings[iCnt] <> 'local.System_Mail') then
  5.     begin
  6.       sGroup := ConferenceIni.ReadString(lConferenceList.Strings[iCnt], 'group', '');
  7.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + sGroup + '.' + lConferenceList.Strings[iCnt] + '    --> Local System Private Mail');
  8.     end
  9.     else
  10.     begin
  11.       //Get Conference Description
  12.        sConfDesc := GetConfDesc(lConferenceList.Strings[iCnt]);
  13.  
  14.       //Add System Mail Conference
  15.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + lConferenceList.Strings[iCnt] + '    --> ' + sConfDesc);
  16.     end;
  17.     Inc(iCnt);  // THIS IS OUTSIDE THE LOOP STATEMENT
  18.  

If you wiush the inc(cint) to be part of the while statement; then place he code between begin ... end
Code: Pascal  [Select][+][-]
  1. iCnt := 0;
  2.   while (lConferenceList.Count > iCnt) do
  3.   begin
  4.     if (lConferenceList.Strings[iCnt] <> 'local.System_Mail') then
  5.     begin
  6.       sGroup := ConferenceIni.ReadString(lConferenceList.Strings[iCnt], 'group', '');
  7.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + sGroup + '.' + lConferenceList.Strings[iCnt] + '    --> Local System Private Mail');
  8.     end
  9.     else
  10.     begin
  11.       //Get Conference Description
  12.        sConfDesc := GetConfDesc(lConferenceList.Strings[iCnt]);
  13.  
  14.       //Add System Mail Conference
  15.       cyberio.PipeWriteLnString('|11[|14' + (iCnt + 1).ToString + '|11] |15' + lConferenceList.Strings[iCnt] + '    --> ' + sConfDesc);
  16.     end;
  17.     Inc(iCnt);
  18.   end; // end of while loop
« Last Edit: March 09, 2022, 09:57:25 am by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Issue with code
« Reply #6 on: March 09, 2022, 08:27:22 pm »
keep it simple

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Issue with code
« Reply #7 on: March 18, 2022, 03:57:15 pm »
Thanks - you guys are awesome. Appreciate the help. I did fix it almost right after I asked..

I looked over ti so much I didn't see the mis-indented sections.

I usually do start my while, do or repeat, until or whatever then fill it in. it just gets messy when I start trying to debug it. I always go back and clean it up once I get it fixed but I understand how that can be tough the read. By the time i post here, its all a mess, typically.


I appreciate you guys helping me, and not be judgmental, however. The world needs more of this. Everyone on this forum has been very nice and helpful.

Thanks again!
-Nugax

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Issue with code
« Reply #8 on: March 18, 2022, 11:00:56 pm »
but the indentation is wrong.

Careful please. Pascal, in common with most ALGOL derivatives, is a free-format language so indentation can never be "wrong".

"Unconventional", perhaps. As an "old hand" who predates most standards other than the King James Version I'm sure I'm guilty of that.

"Inconsistent", perhaps. And inconsistent indentation often indicates a problem.

"Indicative of a problem"... now that's the real issue. But if indentation goes some way towards indicating a problem then it's hardly /wrong/ :-)

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: Issue with code
« Reply #9 on: March 19, 2022, 12:21:11 am »
Now I think the indention in the original code was in fact completely 'right', in that Inc(iCnt) is on the same level as while, indicating precisely that it is outside the while-loop but the next step to be processed after while has finished.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018