It gives an error at the first else, saying it was not expected
Also if some one is willing to add me in Skype so I can ask these basic questions, it would help a lot.
Program Game ;
var answer: string;
Begin
writeln('(======= MENU =======)');
writeln('-------- PLAY --------');
writeln('-------- HELP --------');
writeln('-------- EXIT --------');
repeat
writeln('Pick PLAY, HELP or EXIT');
readln(answer);
if answer = 'EXIT' then
writeln();
writeln('Write EXIT again!')
else
if answer = 'HELP' then
writeln();
writeln('Simple commands like observe, look, take.')
else
if answer = 'PLAY' then
writeln();
writeln('You are in a cave!');
until answer = 'EXIT';
End.
Pascal expects one statement between if and else. It understands your code incorrectly:
if answer = 'EXIT' then
writeln(); (* one statement *)
writeln('Write EXIT again!') (* unrelated to the "if" above *)
else (* error - no preceding "if" *)
The solution is, use begin and end to enclose 2 statements in one:
if answer = 'EXIT' then
begin
writeln();
writeln('Write EXIT again!')
end
else
...
Related
There is a code
program roots;
var a, b, c, d,x1, x2,x:real;
begin
writeln('Введите коэффиценты квадратного уравнения');
write('a='); readln(a);
write('b=');readln(b);
write('c=');readln(c);
begin
while a=0 then
writeln('');
repeat
else
until
if a=0
then
if b=0
then
if c=0
then writeln('Любое x - решение')
else writeln('Нет решений')
else
begin
x:=-c/b;
writeln('x=',x)
end
else
begin
d:=b*b-4*a*c;
if d<0
then writeln('Нет вещенственный корней')
else
begin
x1:=(-b+sqrt(d))/2/a;
x2:=(-b-sqrt(d))/2/a;
writeln('x1=',x1);
writeln('x2=',x2)
end
end
end.
you need to remake it so that when you enter a = 0, the program writes that the value is incorrect and asks you to enter a again, by repeat
it is desirable to make it faster
A repeat .. until loop has the following form:
repeat
<code to perform in the loop>
until <condition to terminate the loop>
So note that the code you need to repeat is between repeat and until and the termination condition is after until.
In your code you have these lines:
...
repeat
else
until
...
There is no else as part of the repeat .. until. However, the code block within the loop might of course contain conditional statements with an else.
I am trying to get Dep_Code to read as a string after choosing the options given (1, 2 or 3). I first had it set to integer in my first program (I think) and was able to get it to read out the options given as words (Accounts ACC or the others). However, it was accidentally deleted. I've tried various ways to get it even setting Dep_Code as a string but its not working and I keep getting a variety of errors. Btw, I'm not familiar with programming so I'm aware that the following code is quite incorrect... but I hope you all can help. Thank you!
REPEAT
writeln ('Please enter the Department Code:- ');
writeln;
writeln ('1. Accounts (ACC)');
writeln ('2. Human Resources (HR)');
writeln ('3. Operations (OP)');
writeln;
readln (Dep_Code);
IF Dep_Code = 1 THEN
Dep_Code := ('Accounts (ACC)')
ELSE IF Dep_Code = 2 THEN
Dep_Code := ('Human Resources(HR)')
ELSE IF Dep_Code = 3 THEN
Dep_Code := ('Operations (OP)');
UNTIL ((Dep_Code >= 1) AND (Dep_Code <= 3));
This is impossible. Pascal is a strictly typed language, and something cannot be an Integer and a string at the same time, and variables cannot change type either:
IF Dep_Code = 1 THEN
Dep_Code := ('Accounts (ACC)')
But you don't need a string at all. Keep it an integer. The functions that handle the various depts can write or define such strings, if necessary. Your logic for the menu does not need a string variable.
Do something like:
procedure HandleAccounts(var Error: Boolean);
begin
...
end;
// Skipped the other functions to keep this answer short ...
var
Dep_Code: Integer;
AllFine: Boolean;
// Skip the rest of the necessary code ...
repeat
// Skipped the Writelns to keep this answer short ...
Readln(Dep_Code);
Error := False;
case Dep_Code of
1: HandleAccounts(Error);
2: HandleHumanResources(Error);
3: HandleOperations(Error);
else
Error := True;
end;
until not Error;
Above, I skipped some of the code. You can fill in the blanks, I guess.
I have a procedure, that in theory, should be skipping whitespace using a look_ahead loop. Problem is, it's not working, if there's any whitespace in the input file, it is adding it to the array of records. I think my logic is correct, but could use another pair of eyes to let me know what I'm missing, and why it's not working.
PROCEDURE Read(Calc: OUT Calculation) IS
EOL: Boolean;
C: Character;
I: Integer := 1;
BEGIN
LOOP
LOOP
Look_Ahead(C, EOL);
EXIT WHEN EOL or C /= ' ';
Get(C);
END LOOP;
EXIT WHEN ADA.Text_IO.END_OF_FILE;
Look_Ahead(C, EOL);
IF Is_Digit(C) THEN
Calc.Element(I).Kind := Number;
Get(Calc.Element(I).Int_Value);
ELSE
Calc.Element(I).Kind := Symbol;
Get(Calc.Element(I).Char_Value);
END IF;
Calc.Len := Calc.Len+1;
IF Calc.Element(I).Char_Value = '=' THEN
EXIT;
END IF;
I := I+1;
END LOOP;
END Read;
EDIT: If any of the other procedures, the code for the record etc is needed for an answer, let me know and I will post it.
For Ada.Text_IO.Look_Ahead, ARM A.10.7(8) says
Sets End_Of_Line to True if at end of line, including if at end of page or at end of file; in each of these cases the value of Item is not specified. Otherwise, End_Of_Line is set to False and Item is set to the next character (without consuming it) from the file.
(my emphasis) and I think the "without consuming it" is key. Once Look_Ahead has found an interesting character, you need to call Get to retrieve that character.
I hacked this little demo together: I left end-of-file to exception handling, and I called Skip_Line once end-of-line’s been seen because just Get wasn’t right (sorry not to be more precise!).
with Ada.Text_IO;
with Ada.IO_Exceptions;
procedure Justiciar is
procedure Read is
Eol: Boolean;
C: Character;
begin
-- Instead of useful processing, echo the input to the output
-- replacing spaces with periods.
Outer:
loop
Inner:
loop
Ada.Text_IO.Look_Ahead (C, Eol);
exit Outer when Eol; -- C is undefined
exit Inner when C /= ' ';
Ada.Text_IO.Get (C); -- consume the space
Ada.Text_IO.Put ('.'); -- instead of the space for visibility
end loop Inner;
Ada.Text_IO.Get (C); -- consume the character which isnt a space
Ada.Text_IO.Put (C); -- print it (or other processing!)
end loop Outer;
Ada.Text_IO.Skip_Line; -- consume the newline
Ada.Text_IO.New_Line; -- clear for next call
end Read;
begin
loop
Ada.Text_IO.Put ("reading: ");
Read;
end loop;
exception
when Ada.IO_Exceptions.End_Error =>
null;
end Justiciar;
Usually it's better to read an entire line and parse it than to try to parse character by character. The latter is usually more complex, harder to understand, and more error prone. So I'd suggest something like
function De_Space (Source : String) return String is
Line : Unbounded_String := To_Unbounded_String (Source);
begin -- De_Space
Remove : for I in reverse 1 .. Length (Line) loop
if Element (Line, I) = ' ' then
Delete (Source => Line, From => I, Through => I);
end if;
end loop Remove;
return To_String (Line);
end De_Space;
Line : constant String := De_Space (Get_Line);
You can then loop over Line'range and parse it. Since I'm not clear if
Get(C);
Get(Calc.Element(I).Int_Value);
Get(Calc.Element(I).Char_Value);
represent 1, 2, or 3 different procedures, I can't really help with that part.
Please tell me where am i wrong, i couldn't fine my mistake in 2 programs. I try to use recursive in pascal.
This one is running but it gives me wrong resuts
program fatorial;
var
n: integer;
function f(n: longint): longint;
begin
if((n=0) or (n=1)) then
f:=1
else
*f:= n*f(n-1);*
read(f);
end;
begin
write('n:='); read(n);
f(n);
write('result:', f(n));
readln;
end.
This one told me "Error: illegal expression" but i don't know how to fix it
program Greatest_common_divisor;
var
gcd,p,q: integer;
r:=real;
begin
write('p:'); read(p);
write('q:'); read(q);
r:= p mod q;
if r <> o then
begin
p:=q;
q:=r
*gcv:= gcv(q,r);*
end;
write('Greatest common divisor:', gcv(p.q));
readln;
end.
You should not read f in the function.
You should write a function rather than use the internal function gcv()
First question:
I think reading f in the function isn't correct.
But the second question:
Don't use := in the command: r:=real; , only :
o and gcv are what kind of variables? You didn't identify o and gcv after var .
Put ; after q:=r
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".