Recent

Author Topic: Problems with case statement [SOLVED]  (Read 1106 times)

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Problems with case statement [SOLVED]
« on: September 16, 2024, 03:56:35 pm »
I'm having problems using a case statement in my code.

UPDATED to reflect Zvoni's comments... See comment on line 21 for the error...

Code: Pascal  [Select][+][-]
  1.             begin
  2.               //  In: 30,1,acid bass,aggressive + analog + distorted + stereo
  3.               // Out: 30, 1, 'acid bass', 'aggressive + analog + distorted + stereo'
  4.               for j := 1 to Length(Sub) do
  5.               begin
  6.                 SubOut := SubOut + Sub[j];
  7.                 if Sub[j] = ',' then
  8.                 begin
  9.                   Inc(Commas);
  10.                   case Commas of
  11.                     1:  begin
  12.                           SubOut := SubOut + ' ';
  13.                         end;
  14.                     2:  begin
  15.                           SubOut := SubOut + ' "';
  16.                         end;
  17.                     3:  begin
  18.                           SubOut := Copy(SubOut, 1, Length(SubOut) - 1) + '", ';
  19.                         end;
  20.                     4:  begin
  21.                         end;       // "Error: Illegal qualifier" occurs on this line now!
  22.                   end;
  23.                 end;
  24.               end;
  25.             end;
  26.  


« Last Edit: September 16, 2024, 04:31:48 pm by carl_caulkett »
"It builds... ship it!"

Zvoni

  • Hero Member
  • *****
  • Posts: 2724
Re: Problems with case statement
« Reply #1 on: September 16, 2024, 03:58:59 pm »
And having registered here in 2016, and after over 300 posts, you should know to NOT post screenshots, but Code (preferable within Code-Tags).

And going by your indentation, your "case" is missing a closing end;

Alternatively it might be, that the "4: ;" is not understood. Try with an empty Begin end
No idea
« Last Edit: September 16, 2024, 04:01:41 pm by Zvoni »
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

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement
« Reply #2 on: September 16, 2024, 04:08:43 pm »
Oops! That's me told!

Despite having been here on and off for several years, I honestly cannot remember anyone objecting to images of code.
Sorry, it won't happen again  ;)
"It builds... ship it!"

TRon

  • Hero Member
  • *****
  • Posts: 3520
Re: Problems with case statement
« Reply #3 on: September 16, 2024, 04:13:13 pm »
compiles just fine for me. Must be somewhere else in your code.

Code: [Select]
fpc -B test.pas
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test.pas
test.pas(13,36) Warning: Local variable "sub" does not seem to be initialized
test.pas(15,27) Warning: Local variable "subout" does not seem to be initialized
test.pas(18,29) Warning: Local variable "commas" does not seem to be initialized
Linking test
36 lines compiled, 0.1 sec
3 warning(s) issued

edit: please stop modifying your original post (or do it once but correct  ;D ). compiles both with and without the begin end for case 4.
« Last Edit: September 16, 2024, 04:17:40 pm by TRon »
This tagline is powered by AI

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement [SOLVED]
« Reply #4 on: September 16, 2024, 04:36:26 pm »
I commented out my case statement and replaced it with a chain of if then else statements.

The error was still showing up on the commented code  :o

I restarted Lazarus and reinstated the case statement. No error and it builds fine now.
I'd say that the finger of suspicion points at Lazarus  ;)
"It builds... ship it!"

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement [SOLVED]
« Reply #5 on: September 16, 2024, 04:44:12 pm »
The final working code...
Code: Pascal  [Select][+][-]
  1.             begin
  2.               //  In: 30,1,acid bass,aggressive + analog + distorted + stereo
  3.               // Out: 30, 1, "acid bass", "aggressive + analog + distorted + stereo"
  4.               for j := 1 to Length(Sub) do
  5.               begin
  6.                 SubOut := SubOut + Sub[j];
  7.                 if Sub[j] = ',' then
  8.                 begin
  9.                   Inc(Commas);
  10.                   case Commas of
  11.                     1:  SubOut += ' ';
  12.                     2:  SubOut += ' "';
  13.                     3:  SubOut := Copy(SubOut, 1, Length(SubOut) - 1) + '", "'
  14.                     else
  15.                       ;
  16.                   end;
  17.                 end;
  18.               end;
  19.               SubOut += '"';
  20.             end;
  21.  
  22.  
« Last Edit: September 16, 2024, 04:54:43 pm by carl_caulkett »
"It builds... ship it!"

MarkMLl

  • Hero Member
  • *****
  • Posts: 7909
Re: Problems with case statement [SOLVED]
« Reply #6 on: September 16, 2024, 04:51:30 pm »
I restarted Lazarus and reinstated the case statement. No error and it builds fine now.
I'd say that the finger of suspicion points at Lazarus  ;)

That's not reasonable, since what you're reporting is a compilation error and the IDE (noting that you're not telling us the version etc.) runs that ab initio every time.

I believe that the only place where Lazarus /could/ be implicated is if you've got some odd path structure (typically including symlinks, but I'm afraid that you've not told us the OS either so that might not mean very much to you :-)

The usual cause of an error somewhere near a case statement is that it triggers the "dangling else" problem, but that is more likely to show up at runtime (i.e. the program just doesn't behave like you expect). Another potential issue is if you've declared 4 as a label in the same scope.

Please could we have a standalone, compilable program that demonstrates the error, since if it is something in the tools it's in everybody's interest to try to resolve things.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alpine

  • Hero Member
  • *****
  • Posts: 1271
Re: Problems with case statement [SOLVED]
« Reply #7 on: September 16, 2024, 04:56:45 pm »
Quote
The error was still showing up on the commented code  :o
Looks like the source file on disk wasn't identical to the one in the editor for whatever reason.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement [SOLVED]
« Reply #8 on: September 16, 2024, 05:05:54 pm »
That's not reasonable, since what you're reporting is a compilation error and the IDE (noting that you're not telling us the version etc.) runs that ab initio every time.

Check my sig  ;)

I believe that the only place where Lazarus /could/ be implicated is if you've got some odd path structure (typically including symlinks, but I'm afraid that you've not told us the OS either so that might not mean very much to you :-)

No symlinks here, though I do use them quite a lot...

The usual cause of an error somewhere near a case statement is that it triggers the "dangling else" problem, but that is more likely to show up at runtime (i.e. the program just doesn't behave like you expect). Another potential issue is if you've declared 4 as a label in the same scope.

I suspect that what has happened is that Lazarus quite correctly highlighted a genuine error in my code, but somehow held on to the errors, for whatever reason.

Please could we have a standalone, compilable program that demonstrates the error, since if it is something in the tools it's in everybody's interest to try to resolve things.

I suppose I could try to recreate the errant code...
"It builds... ship it!"

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement [SOLVED]
« Reply #9 on: September 16, 2024, 05:14:50 pm »
I suppose I could try to recreate the errant code...

Sorry, it resolutely refuses to break now  ;)
"It builds... ship it!"

MarkMLl

  • Hero Member
  • *****
  • Posts: 7909
Re: Problems with case statement [SOLVED]
« Reply #10 on: September 16, 2024, 05:18:51 pm »
Check my sig  ;)

Missed it, because when commenting sigs aren't visible.

Also: DO NOT PUT VERSION INFO IN A SIG. Sorry for shouting, but the problem is that as soon as you change your sig- because you've upgraded your system or changed compiler version- every time that somebody reads one of your messages they will see your new configuration, not the one that applied when you raised the issue. Worse, every time Google etc. or an AI scrapes the forum it will associate the problem with your new configuration, which is grossly unhelpful to anybody- quite possibly not a regular member of the forum or mailing lists- who reads about it in that way.

So by all means put something humorous in your sig, or make a social or political statement. But it IS NOT the place to put information on what compiler version etc. is associated with a problem being discussed.

Going back to where we were, it /would/ be useful if you could recreate the problem. But I'm bothered by this "vanished when I restarted Lazarus" aspect, which suggests that you might have stumbled upon some very odd cache misbehaviour or suchlike. Are you using any conditional compilation ($ifdef etc.) or include files in your code?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement [SOLVED]
« Reply #11 on: September 16, 2024, 05:24:51 pm »
Quote
The error was still showing up on the commented code  :o
Looks like the source file on disk wasn't identical to the one in the editor for whatever reason.

Even on the Mac, we still use the time-honoured approach of Ctrl-S (Cmd-S, actually ;)) to save at regular intervals...
"It builds... ship it!"

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement [SOLVED]
« Reply #12 on: September 16, 2024, 05:48:56 pm »
Also: DO NOT PUT VERSION INFO IN A SIG. Sorry for shouting, but the problem is that as soon as you change your sig- because you've upgraded your system or changed compiler version- every time that somebody reads one of your messages they will see your new configuration, not the one that applied when you raised the issue. Worse, every time Google etc. or an AI scrapes the forum it will associate the problem with your new configuration, which is grossly unhelpful to anybody- quite possibly not a regular member of the forum or mailing lists- who reads about it in that way.

All very reasonable points. Though, as I pointed out to Zvoni, earlier, when I used to use Lazarus (and this forum) a lot, back in the late 2010's, no-one ever commented on posting style.

So by all means put something humorous in your sig, or make a social or political statement. But it IS NOT the place to put information on what compiler version etc. is associated with a problem being discussed.

I'll steer clear of political statements, I think ;0

Going back to where we were, it /would/ be useful if you could recreate the problem. But I'm bothered by this "vanished when I restarted Lazarus" aspect, which suggests that you might have stumbled upon some very odd cache misbehaviour or suchlike. Are you using any conditional compilation ($ifdef etc.) or include files in your code?
MarkMLl

It's really just a very straightforward console app...

Code: Pascal  [Select][+][-]
  1. program Search_Replace;
  2.  
  3. uses
  4.   SysUtils, Classes, StrUtils;
  5.  
  6. const
  7.   InputFilePath = 'OsmoseCategories.pas';
  8.   OutputFilePath = 'OsmoseCategories OUT.pas';
  9.  
  10. var
  11.   InputFileStream: TFileStream;
  12.   OutputFileStream: TFileStream;
  13.   StringStream: TStringStream;
  14.   Content: string;
  15.   LinesIn: TStringList;
  16.   i: Integer;
  17.   j: Integer;
  18.   Sub: string;
  19.   SubOut: string;
  20.   Commas: Integer;
  21.  
  22. begin
  23.   try
  24.     // Read input file into TStringStream
  25.     InputFileStream := TFileStream.Create(InputFilePath, fmOpenRead or fmShareDenyWrite);
  26.     try
  27.       StringStream := TStringStream.Create('');
  28.       try
  29.         StringStream.CopyFrom(InputFileStream, 0);  // Copy entire file
  30.         Content := StringStream.DataString;
  31.  
  32.         // Manipulate content
  33.         LinesIn := TStringList.Create;
  34.         try
  35.           LinesIn.Text := Content;
  36.  
  37.           for i := 0 to LinesIn.Count - 1 do
  38.           begin
  39.             Sub := LinesIn[i];
  40.             SubOut := '';
  41.             Commas := 0;
  42.  
  43.             if Sub[1] = ',' then
  44.             begin
  45.               SubOut := Sub;
  46.             end
  47.             else
  48.             begin
  49.               //  In: 30,1,acid bass,aggressive + analog + distorted + stereo
  50.               // Out: 30, 1, 'acid bass', 'aggressive + analog + distorted + stereo'
  51.               for j := 1 to Length(Sub) do
  52.               begin
  53.                 SubOut := SubOut + Sub[j];
  54.                 if Sub[j] = ',' then
  55.                 begin
  56.                   Inc(Commas);
  57.                   case Commas of
  58.                     1:  SubOut += ' ';
  59.                     2:  SubOut += ' "';
  60.                     3:  SubOut := Copy(SubOut, 1, Length(SubOut) - 1) + '", "'
  61.                     else
  62.                       ;
  63.                   end;
  64.                 end;
  65.               end;
  66.               SubOut += '"';
  67.             end;
  68.  
  69.             LinesIn[i] := SubOut;
  70.             WriteLn(SubOut);
  71.           end;
  72.           Content := LinesIn.Text;
  73.         finally
  74.           LinesIn.Free;
  75.         end;
  76.  
  77.         // Write manipulated content to output file
  78.         OutputFileStream := TFileStream.Create(OutputFilePath, fmCreate);
  79.         try
  80.           StringStream.Position := 0;
  81.           StringStream.WriteString(Content);
  82.           OutputFileStream.CopyFrom(StringStream, 0);
  83.  
  84.           WriteLn('File processing completed successfully.');
  85.         finally
  86.           OutputFileStream.Free;
  87.         end;
  88.       finally
  89.         StringStream.Free;
  90.       end;
  91.     finally
  92.       InputFileStream.Free;
  93.     end;
  94.   except
  95.     on E: Exception do
  96.     begin
  97.       WriteLn('An error occurred: ', E.Message);
  98.     end;
  99.   end;
  100. end.  
  101.  

The input file is `OsmoseCategories.pas` (see attached) which, the observant will notice, is not currently a Pascal source file. All this text mangling is intended to turn it into a Pascal source file eventually ;)
I stripped out the hard coded paths, but you may need to tweak the paths.
"It builds... ship it!"

MarkMLl

  • Hero Member
  • *****
  • Posts: 7909
Re: Problems with case statement [SOLVED]
« Reply #13 on: September 16, 2024, 06:30:23 pm »
Do you have a $mode at the top of your original? FPC 3.2.2 with no extra options etc. is objecting to the try at line 23. Otherwise if compiling from Lazarus please Project -> Project Options -> Show Options (button at bottom of dialog(ue).

If I do this

Code: Text  [Select][+][-]
  1. ~$ fpc -MObjFPC Search_Replace.pas
  2. Free Pascal Compiler version 3.2.2 [2023/09/04] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling Search_Replace.pas
  6. Linking Search_Replace
  7. 101 lines compiled, 0.2 sec
  8.  

...then as you can see compilation is successful with that version of compiler.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

carl_caulkett

  • Hero Member
  • *****
  • Posts: 635
Re: Problems with case statement [SOLVED]
« Reply #14 on: September 16, 2024, 10:09:51 pm »
Do you have a $mode at the top of your original? FPC 3.2.2 with no extra options etc. is objecting to the try at line 23. Otherwise if compiling from Lazarus please Project -> Project Options -> Show Options (button at bottom of dialog(ue).

No, there is no $mode at the top of my file. The only significant difference between my code and yours is, possibly, that mine is being compiled as a Lazarus project, with the source code being held in the `Search_Replace.lpr` file. Otherwise my code is exactly as in the message labelled "The final working code...".
"It builds... ship it!"

 

TinyPortal © 2005-2018