im new using Pascal and i have to program a game called seven eleven, can you guys give me some tips?
Ive tried:
program sevele;
var capitalinicial: integer
begin
writeln('ingrese un capital')
readln(capitalinicial)
writeln('su capital es capitalinicial')
Answering your question, There are a few things you need to know:
You have to put a semicolon ; at the end of every sentence, except after a keyword that denotes the beginning of a control structure (for,while,if,else,etc) or after a begin or end keyword.
Use the same amount of begins and ends keywords. Note that the last end in the program is followed by a dot.
When you use writeln you can print one or more variables or strings. You have to use simple quotes ' to print strings, and just the name of a variable without any quote to print it's value, also you need to separe the diferent arguments with commas ','.
for example:
program example;
var
a,b:integer;
begin
a:=3;
b:=5;
writeln ('this is just a string');
writeln (a);
writeln (a,b);
writeln ('the value of a is: ',a,' and the value of b is: ',b);
readln;
end.
The code you attemped to write probably is:
program sevele;
var capitalinicial: integer;
begin
writeln('ingrese un capital');
readln(capitalinicial);
writeln('su capital es ',capitalinicial);
readln; //use this to give you time to view the output
end.
This is how it works
program sevele;
var capitalinicial: integer;
begin
writeln('ingrese un capital');
readln(capitalinicial);
writeln('su capital es',capitalinicial);
end.
Related
I am trying to write something to a file but pascal adds a bunch of whitespaces between each record's value and also puts 2 records on one line.
The first file is just a list of strings.
The second file (the one I create through the program) should have title and description.
How can I get rid of the whitespaces pascal is adding?
program Wiki;
{$mode objfpc}
TYPE wiki=record
title:string;
description:string;
end;
var
f:text ;
g:file of wiki ;
row:wiki;
fileName: string;
oldFileName:string;
begin
writeln('Old file name:');
readln(oldFileName);
ASSIGN(f,oldFileName);
RESET(f);
writeln('New file name:');
readln(fileName);
ASSIGN(g,fileName);
REWRITE(g);
REPEAT
Readln(f,row.title);
writeln('give a description:');
Writeln(row.title);
Readln(row.description);
Write(g,row)
until EOF(f);
CLOSE(f);
CLOSE(g);
writeln;
writeln('press enter to close.');
readln();
end.
In objfpc mode without {$H+}, I guess that row.description is a fixed size Turbo Pascal style ShortString. It is 255 characters long, and that is probably why you get all that whitespace.
Rather write the output file as a text file:
var
f: Text;
g: Text;
and:
Writeln(g, row.title, ';', row.description);
That should produce text output like:
Finding Nemo;The adventures of two fish trying to find the lost son of one of them
Toy Story;The adventures of a merry bunch of toys
etc.
Im just starting up with pascal and I'm doing the good old bhaskara solver with the following code:
Program bhaskara;
var
a,b,c: real;
begin
writeln('Ingrese a, b y c');
readln(a,b,c);
if sqr(b) >= 4*a*c then
begin
writeln('tiene raices reales');
end
else
begin
writeln('no tiene raices reales');
end
readln(a);
end.
The last line: readln(a), which is there just to pause the program and see the output is making the program not compile(program works fine without it), it says:
bhaskara.pas(15,2) Fatal: Syntax error, ";" expected but "identifier READLN" found
Im sure it's something simple but i can't find the answer, please help.
Pascal requires a semicolon as a statement separator between statements.
Your else block is a statement and because it is not the final line
of the program and is followed by your readln(a), it requires a ';'
after it.
In fact, because your else clause contains only a single statement,
it does not require the begin & end.
So you could simply write
else
writeln('no tiene raices reales');
readln(a);
You need a semicolon (;) after the "end" statement right before the readln statement.
Reading this Pascal BNF grammar I can't understand why is a ; required to appear after end in a function definition. After a function-heading is seen, a function-block that's block may appear:
function-declaration =
function-heading ";" function-body |
function-heading ";" directive |
function-identification ";" function-body .
function-body =
block .
When a begin appear, that's part of a statement-par, that's part of a block, it's processed by statement-part, right?
block =
declaration-part statement-part .
statement-part =
begin statement-sequence end .
Note statement-part. There's no ; here after end keyword and this is not part of a statement-sequence. So, I don't get how the compiler claims about lack of ; after end keyword, like in this example:
function myabs(i : integer) : integer;
begin
if i < 0 then begin i := -i; end; < -- it's process by statement-sequence, so, ';' may appear
myabs := i;
end; <-- it is the semicolon what about I'm speaking
What am I missing? am I reading wrong the grammar? all Pascal compilers I've tried give an error if I omit this.
ANTLRWorks is your best friend here.
If you try some pascal grammar such as http://www.monperrus.net/martin/pascal-antlr3 using antlrworks (http://www.antlr3.org/works/) you'll see that a program like
program first;
function myabs(i : integer) : integer;
begin
end;
begin
end.
will be parsed like this
so you can see exactly what's happening.
ps. the pascal grammar link I've provided to you has a problem with one specific token, but I bet you can workaround this ;-)
ps2. update - antlrworks screenshot to help #Jack
You don't have to have a semi-colon after an end. Simple as that.
Semi-colon is used to separate statements. So you only need to have a semi-colon after an end if it is not the last statement. If it is the last statement you should instead have a full stop.
Now, there could also be some error in the BNF that means that according to the BNF you don't have to have a semi-colon where you actually need it, but the only way to figure that out is to analyze the whole BFN in detail, which I don't feel is constructive. :-)
But in this case I think what you have missed is that a procedure or function declaration must end with a semi-colon.
Procedure and functions do not need to be terminated with a semi-colon, but they must be separated by one:
From the Pascal BNF
proc-and-func-declaration:
proc-or-func
proc-and-func-declaration ; proc-or-func
declare
l_tot number := 0;
begin
for i in 1..apex_application.g_f08.count loop
l_tot := l_tot + nvl(to_number(apex_application.g_f08(i)),0);
end loop;
if l_tot = nvl(to_number(:P21_TOTAL_PRICE),0) then
return true;
else
return false;
end if;
end;
Got below error with above code
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
Error occurred with :P21_TOTAL_PRICE. What is the wrong ? How can i correct this ?
Rather than using REPLACE you should use the more powerful REGEXP_REPLACE function. http://www.orafaq.com/wiki/REGEXP_REPLACE
You can then remove any non-numeric character from the string before then using the TO_NUMBER function.
In your case it would be something like:
REGEXP_REPLACE(:P21_TOTAL_PRICE, '[^0-9]+', '');
See my answer to almost the exact same question here: Oracle To_Char function How to handle if it's already a string
The error rises because the number that you're representing is actually a character string involving commas etc. When you put a to_number to that, Oracle cannot replace the commas.
You might want to use replace function to strip off the commas
Change
if l_tot = nvl(to_number(:P21_TOTAL_PRICE),0) then
to
if l_tot = nvl(to_number(replace(:P21_TOTAL_PRICE,',','')),0) then
I am working with a pl/sql procedure. I have an initialized variable myvar and I want to check its value : if it does not contain 'Z', I want it to contain 'P'.
I am currently doing it this way:
myvar := decode(myvar,'Z','Z','P');
I was just wondering if there was a simplier way to do this. I mean, decode is already simple, but I feel it's weird to specify the content of the variable while it is already in it !
If such a function would exist, it would look like this:
Function myfunction(a In Varchar2, b In Varchar2, c In Varchar2)
Return Varchar2
Is
Begin
if a <> b
then
return c;
end if;
return a;
End myfunction;
Any help would be appreciated !
There is no built-in function that does exactly what you want.
You could use CASE rather than DECODE:
CASE myvar WHEN 'Z' THEN 'Z' ELSE 'P' END
It doesn't make it any shorter though!
Put that function of yours to the program's declaration section and use it!
I agree the best option is to use CASE expression:
CASE myvar WHEN 'Z' THEN 'Z' ELSE 'P' END
Another approach if you feel happy with DECODE is to run this query:
SELECT decode(myvar,'Z','Z','P')
INTO myvar
FROM DUAL;
To answer your original question of whether there is a simpler way, there is also this:
if myvar <> 'Z' then
myvar := 'P'
end if;