how do I rectify this switch case error in pascal? - pascal

This is a pascal program that returns factor . rate is the input which is given by the user.
This program is throwing me error.
please do have a look and help.
I want to rectify the error.
I'm not able to find out the error since im very new to pascal and im trying to learn
program Mss;
var
rate,factor:integer;
begin
readln(rate);
case rate of
1..2:begin
factor:=(2*rate)-1;
writeln(factor);
end
3:begin throws error here
factor:=(3*rate)-1;
writeln(factor);
end
4:begin
factor:=(4*rate)-1;
writeln:=(factor);
end
5:begin
factor:=(3*rate)-1;
writeln(factor);
end
6..8:begin
factor:=rate-2;
writeln(factor);
end
else begin
writeln(rate);
end
end;
This is a switch case which returns factor. rate is the input from user.
this throws me an error.
Fatal: Syntax error, ";" expected but "ordinal const" found

You have a few syntax errors. Your begin/end blocks need to be followed by ;.
writeln:=(factor) should be writeln(factor).
And you need an end. to finish the program.
program Mss;
var
rate,factor:integer;
begin
readln(rate);
case rate of
1..2:begin
factor:=(2*rate)-1;
writeln(factor);
end;
3:begin
factor:=(3*rate)-1;
writeln(factor);
end;
4:begin
factor:=(4*rate)-1;
writeln(factor);
end;
5:begin
factor:=(3*rate)-1;
writeln(factor);
end;
6..8:begin
factor:=rate-2;
writeln(factor);
end;
else begin
writeln(rate);
end
end
end.
Also, please note that ; in Pascal is a separator, so you could write code like:
6..8:begin
factor:=rate-2;
writeln(factor);
end;
As:
6..8:begin
factor:=rate-2;
writeln(factor)
end;

Related

Using TIdHTTPServer thread safe in D2005

This question has been asked very often and I've spent hours reading, trying, testing with no result.
I guess it has to do with my older 2005 version.
Below is the code I tried after reading a post in the Embarcadero forum answered by Remy Lebeau:
Thread: How to handle multiple HTTP sessions with Indy10 TIdHTTPServer
procedure TMainForm.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Msg : String;
begin
if ARequestInfo.QueryParams <> '' then
begin
Msg := DateTimeToStr(Now) + ': ReqParam "' + ARequestInfo.QueryParams + '"';
TThread.Queue(nil,
procedure
begin
Memo1.Lines.Add(Msg);
end
);
AResponseInfo.ContentText := '<HTML><BODY>Query Params found.</BODY></HTML>';
end
else
begin
AResponseInfo.ContentText := '<HTML><BODY>Error: No Query Params.</BODY></HTML>';
Msg := DateTimeToStr(Now) + ': Error: No Query Params';
TThread.Queue(nil,
procedure
begin
Memo1.Lines.Add(Msg);
end
);
end;
end;
What I'm aiming for is accessing a memo or log file entry in a thread safe manner. Somehow using TThread.Synchronize() or TThread.Queue() doesn’t compile.
When adding the TThread.Queue() line as suggested by Remy, the error I get is:
E2029 Expression expected but procedure found
Does somebody have an alternative that I can use in Delphi 2005?
Edit: this is what I see from code completion:

How to find out Error No. in ObjFPC in a try-except statement

I am trying to find a way to detect the error no in objfpc, what I tried is shown below:
Program ErrorHandling;
{$R+}
{$MODE objfpc}
Uses
SysUtils, crt;
Var
intvar: 1 .. 100;
Begin
Try
clrscr;
writeln( 'enter intvar: ');
readln(intvar);
Except
on
e: Exception
Do
Begin
writeln('In Exception, IOResult: ',IOResult);
Case IOResult Of
201: writeln('Range intvar out of range 1-100!'); {How can we find that Error no is 201}
Else
writeln('Unkown Error!'); readln;
End
End
End;
writeln('intvar: ' , intvar);
readln;
End.
But How can we find if the 201 Range Error occurs. I tried using IOResult command but it always shows "Unknown Error" string of my case statement.
Using exceptions you can check error type by type of exception itself.
The classic way is:
try
clrscr;
writeln('enter intvar: ');
readln(intvar);
except
on e: ERangeError do // Executes when raised exception is ERangeError
begin
Writeln('Range intvar out of range 1-100!');
end;
on e: Exception do // Executes for any other exceptions
begin
Writeln(e.ClassName); // Shows exception class you can use at the "on e:" constuction
Writeln(e.Message);
end;
end;
Look at The try...except statement in the official documentation.

Pascal comparison error

I have a problem with my very simple Pascal code. (I just started to learn Pascal.)
So it's about an age comparison code then rest can be seen through the code.
program Test;
uses crt;
var
age : real;
Begin
writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.
When I enter a value below 18 as the age, I expect the program to respond:
ACCESS DENIED
Reason:You are way to young
but I don't get any output. Why?
Sometimes text indentation helps you to see the issue. Here's your code with indentation added:
program Test;
uses crt;
var
age : real;
Begin
writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.
And to make the implemented logic more obvious, I will now represent the nested ifs without the code that they execute:
if age>18 then
if age<100 then
... // Access Granted
else
if age<18 then
... // You are way too young
else
... // You are way too old
;
It is easy to see now that the branch marked as You are way too young is never reached. It is supposed to be executed when age is less than 18, but that if statement is nested into another if which will call it only when age is greater than 18. So, age should first qualify as greater than 18, then less than 18 in order for that branch to execute – you can see now why you do not get the expected result!
The intended logic could possibly be implemented this way:
if age>18 then
if age<100 then
... // Access Granted
else // i.e. "if age >= 100"
... // You are way too old
else // now this "else" belongs to the first "if"
... // You are way too young
;
I believe you should be able to fill in the missing code blocks correctly.
Just one last note: you might want to change age>18 to age>=18, so that 18 proper does not qualify as "too young".

Get Actual Error Message From Oracle Exception In Dot Net

I have this procedure:
create or replace PROCEDURE MyProc
(
<some-parameters>
)
AS
BEGIN
if(<some-condition>) then
RAISE_APPLICATION_ERROR('my custom error message');
end if;
END;
When calling it from C#:
try
{
<call procedure>
}
catch(OracleException x)
{
lblMessage.Text = x.Message;
}
I am getting error message like:
ORA-28008: my custom error message ORA-06512: at blah, line blah ORA-06512: at line blah
I want only:
my custom error message
There is no innerException. Errors collection don't help. Same situation when use Exception instead of OracleException.
What am I missing?
I can use string manipulation but how fixed is format of error message?
I use return parameters to capture with php. I recon the same technique will be useful in .net.
(I assume it's possible to catch the custom message in the exception handling and re-raising only that custom message, but underneath method works for sure. :)
create or replace PROCEDURE MyProc (p_result out varchar2)
is
...
begin
...
if error then
p_result := 'my custom error message';
return; -- exit procedure
end if;
p_result := 'ok';
end;
Try
PKG_MSG.RAISE_ERROR( 0,null,'my custom error message',null,null,null,null,null,null,null,null,null,null );
instead of
RAISE_APPLICATION_ERROR('my custom error message');

Why do I get a NZEC error?

I'm working in Pascal, I don't understand why I'm getting an NZEC (Non Zero Exit Code) error when I'm submitting my problem. On my PC it works perfectly. Can you give me your opinion please?
program super_factor_sum;
var k,i,j,s:longint; f,g:text;
function prim(x:integer):boolean; var d:longint;
begin
prim:=true;
for d:=2 to x div 2 do if x mod d=0 then prim:=false;
end;
begin assign(f,'input.txt'); reset(f); assign(g,'output.txt'); rewrite(g);
while not eof(f) do
begin readln(f,k); s:=0; i:=2;
while (k<>1) or (i<=k) do
begin if (prim(i)) and (k mod i=0) then
begin j:=0;
repeat k:=k div i; j:=j+1;
until k mod i<>0;
s:=s+i*j; i:=i+1;
end
else i:=i+1;
end;
write(s); writeln(g,s);
end;
close(f); close(g);
end.
Avoid NZEC error in Erlang in SPOJ
"module name always has to be tested and the entry point should be function main . For example, after compilation it should be run as tested:main()"

Resources