hi i want to run this code but it show me several errors please any one can help
Program Climat;
var clim: integer;
begin
Writeln('entrez degré');
Readln(clim);
if (clim < 0) then
Writeln('c est glacial ');
else
if (clim < 30) then
Writeln('le temps est doux');
else
Writeln('trés chaud'):
end.
Pascal expects one statement between then and else, and a statement does not ends with a ;. ; is used as a separator of statements in compound statment.
To get rid of your errors do one of:
Remove ; at the end of the Writeln('c est glacial ') and Writeln('le temps est doux')
or use compound statements: write then begin and end else instead of just then and else.
Also after the last Writeln there should be ; instead of :.
Related
The problem is to remove enter and join in one line for ': 86:', which lasts until ': 61:'
http://wklejto.pl/840401
:86:770 PRZELEW KRAJOWY SP; Z RACH.: 78105000861000309030383012; OD:
DOZ SPÓŁKA AKCYJNA DIRECT SPÓŁKA KOMANDYTOWA KINGA C.GILLETTE 11
94-406 ŁÓD¬; TYT.:
/VAT/10655,40/IDC/6252315798/INV/FV/334/PL/2001/TXT/FAKTURA;
DATA STEMPLA: 30.04.2020; TNR: 191181084638228.280001
:61:2005040504DN12655,40NTRFNONREF//EL011880-016890
368-PODATEK VAT
My program should allow the user to choose ice cream from menu then add the price for the ice cream to show total price of the ice cream.However my output for total price is wrong. For example if i choose 1 and 2 the answer should be 1.5+1.7=3.2 but I get 3.4. Also if i choose 2 I'll only get error. Please help me.
program iceCream;
var
count,i: integer;
price:array[1..50]of real;
totalPrice: real;
choice: integer;
begin
count:= 0;
writeln ( ' ICE CREAM FLAVOUR');
write ( ' 1.Vanilla: RM 1.50 | 3.Chocolate: RM 2.00');
writeln;
write ( ' 2. Strawberry: RM 1.70 | 0. Exit ');
writeln;
repeat
write ( ' Enter your choice(number): ');
readln ( choice);
if choice <= 3 then
count:= count+1
else
writeln ( 'Invalid choice');
case choice of
1: begin
price[i]:= 1.50;
end;
2: begin
price[i]:= 1.70 ;
end;
3: begin
price[i]:= 2.00;
end;
end;
for i:= 1 to count do
begin
totalPrice:= totalPrice+price[i];
end;
until choice = 0;
writeln ( ' Total ice-cream: ', count);
readln;
writeln ( ' Total price: RM ', totalPrice:2:2);
readln;
end.
I won't answer your question here. It's more important to find out what you're doing wrong and how you can solve the problem on your own.
How to find out what's going wrong?
The compiler shows two warnings when it compiles your code:
Warnings: 2
project1.lpr(29,17) Warning: Variable "i" does not seem to be initialized
project1.lpr(44,25) Warning: Variable "totalPrice" does not seem to be initialized
This kind of warnings appears when you read a variable without having a value assigned to it. The compiler leads you to the location of the unsafe code. The first warning is located in line 29 in column 17:
1: begin
price[i]:= 1.50; // i has not been set before. So its value is undefined or 0
end;
Is your intention to use set Price[i] here? So why hasn't it been assigned to a value before?
Or do you have a different variable that holds the number of ice creams? Why don't you use that?
Let's take a look at the second warning in line 44 in column 25. It's this line:
totalPrice:= totalPrice+price[i];
// ^
Are you sure totalPrice has been initialized correctly?
Even though you initialize it correctly the program may still doesn't work properly. In that case it helps to debug the application.
Open your application in the Lazarus IDE and press F8 to step through your program. You go from line to during the execution of your program. You will see what's wrong then.
My goal is to write a string to a file where the size of the string will vary. At the moment I have made the string very large so that there is no overflow but is there a way to make it so that the size of the string is the exact number of characters I'm placing into it? I've tried something like the code below but it gives me an error unknown identifier "address count" I think it is because address count is a variable declared in a process and address count is constantly changing. Is there any way around this?
signal address_map :string (1 to address_count);
many thanks
leo
"My goal is to write a string to a file." Hence, lets just focus on that.
Step 1: reference the file IO packages (recommended to turn on VHDL-2008):
use std.textio.all ;
-- use ieee.std_logic_textio.all ; -- include if not using VHDL-2008
Step 2: Declare your file
file MyFile : TEXT open WRITE_MODE is "MyFile.txt";
Step 3: Create a buffer:
TestProc : process
variable WriteBuf : line ;
begin
write ... -- see step 4
writeline ... -- see step 5
Step 4: Use write to write into the buffer (in the process TestProc):
write(WriteBuf, string'("State = ") ) ; -- Any VHDL version
write(WriteBuf, StateType'image(State)) ;
swrite(WriteBuf, " at time = " ); -- VHDL-2008 simplification
write(WriteBuf, NOW, RIGHT, 12) ;
Step 5: Write the buffer to the file (in the process TestProc):
writeline(MyFile, WriteBuf) ;
Alternate Steps 3-5: Use built-in VHDL Write with to_string:
Write(MyFile, "State = " & to_string(State) &
", Data = " & to_hstring(Data) &
" at time " & to_string(NOW, 1 ns) ) ;
Alternate Steps 1-5: Use OSVVM (see http://osvvm.org) (requires VHDL-2008):
library osvvm ;
use osvvm.transcriptpkg.all ; -- all printing goes to same file
. . .
TestProc : process
begin
TranscriptOpen("./results/test1.txt") ;
Print("State = " & to_string(State) &
", Data = " & to_hstring(Data) &
" at time " & to_string(NOW, 1 ns) ) ;
One hard but flexible solution is to use dynamic allocation features of VHDL (copied from ADA).
You have to use an access of string (it is roughly like a "pointer to a string" in C)
type line is access string;
you event don't have to do it because line is already declared in std.textio package.
Ok, the problem next is that you can't use an access type for a signal, so you have to use a shared variable:
shared variable address_map: line;
And finally you have to allocate, read and write to this line:
--Example in a function/procedure/process:
--free a previously allocated string:
if address_map /= NULL then
deallocate(address_map);
end if;
--allocate a new string:
address_map:=new string (1 to address_count);
address_map(1 to 3):="xyz";
--we have here:
-- address_map(1)='y'
-- address_map(2 to 3)="yz"
-- address_map.all = "xyz"
Notice the use of new/deallocate (like malloc/free in C or free/delete in C++).
It is not easy to handle this kind of code, I recommend you to read the documentation of VHDL keywords "new", "deallocate" and "access" (easily found with your favorite search engine) or feel free to ask more questions.
You can also use the READ (read the whole line into a string) and WRITE (append a string to the line) functions from std.textio package.
I have recently learned how to work with basic files in Fortran
and I assumed it was as simple as:
open(unit=10,file="data.dat")
read(10,*) some_variable, somevar2
close(10)
So I can't understand why this function I wrote is not working.
It compiles fine but when I run it it prints:
fortran runtime error:end of file
Code:
Function Load_Names()
character(len=30) :: Staff_Name(65)
integer :: i = 1
open(unit=10, file="Staff_Names.txt")
do while(i < 65)
read(10,*) Staff_Name(i)
print*, Staff_Name(i)
i = i + 1
end do
close(10)
end Function Load_Names
I am using Fortran 2008 with gfortran.
A common reason for the error you report is that the program doesn't find the file it is trying to open. Sometimes your assumptions about the directory in which the program looks for files at run-time will be wrong.
Try:
using the err= option in the open statement to write code to deal gracefully with a missing file; without this the program crashes, as you have observed;
or
using the inquire statement to figure out whether the file exists where your program is looking for it.
You can check when a file has ended. It is done with the option IOSTAT for read statement.
Try:
Function Load_Names()
character(len=30) :: Staff_Name(65)
integer :: i = 1
integer :: iostat
open(unit=10, file="Staff_Names.txt")
do while(i < 65)
read(10,*, IOSTAT=iostat) Staff_Name(i)
if( iostat < 0 )then
write(6,'(A)') 'Warning: File containts less than 65 entries'
exit
else if( iostat > 0 )then
write(6,'(A)') 'Error: error reading file'
stop
end if
print*, Staff_Name(i)
i = i + 1
end do
close(10)
end Function Load_Names
Using Fortran 2003 standard, one can do the following to check if the end of file is reached:
use :: iso_fortran_env
character(len=1024) :: line
integer :: u1,stat
open (newunit=u1,action='read',file='input.dat',status='old')
ef: do
read(u1,'A',iostat=stat) line
if (stat == iostat_end) exit ef ! end of file
...
end do ef
close(u1)
Thanks for all your help i did fix the code:
Function Load_Names(Staff_Name(65))!Loads Staff Names
character(len=30) :: Staff_Name(65)
integer :: i = 1
open(unit=10, file="Staff_Names.txt", status='old', action='read')!opens file for reading
do while(i < 66)!Sets Set_Name() equal to the file one string at a time
read(10,*,end=100) Staff_Name(i)
i = i + 1
end do
100 close(10)!closes file
return!returns Value
end Function Load_Names
I needed to change read(10,*) to read(10,*,END=100)
so it knew what to do when it came to the end the file
as it was in a loop I assume.
Then your problem was that your file was a row vector, and it was likely
giving you this error immediately after reading the first element, as #M.S.B. was suggesting.
If you have a file with a NxM matrix and you read it in this way (F77):
DO i=1,N
DO j=1,M
READ(UNIT,*) Matrix(i,j)
ENDDO
ENDDO
it will load the first column of your file in the first row of your matrix and will give you an error as soon as it reaches the end of the file's first column, because the loop enforces it to read further lines and there are no more lines (if N<M when j=N+1 for example). To read the different columns you should use an implicit loop, which is why your solution worked:
DO i=1,N
READ(UNIT,*) (Matrix(i,j), j=1,M)
ENDDO
I am using GNU Fortran 5.4.0 on the Ubuntu system 16.04. Please check your file if it is the right one you are looking for, because sometimes files of the same name are confusing, and maybe one of them is blank. As you may check the file path if it is in the same working directory.
uses crt;
var
i: integer;
stav: integer;
prsten: boolean;
begin
clrscr();
stav:=0;
prsten:=false;
repeat
case stav of
0: begin //Zacatek hry//
writeln('Toto je hra, jsi Princ a jsi v lese.');
writeln('Na krizovatce muzes jit doleva = 1, nebo doprava = 2');
readln(stav);
end;
1: begin
writeln('Potkas draka, ktery vezni krasnou princeznu. ');
writeln('3 = Prepadnout draka, 4 = Promluvit s nim');
readln(stav);
end;
2: begin
writeln('Potkas pocestneho. ');
writeln('5 = Pokracujes dal lesem, 6 = Promluvit s pocestnym');
readln(stav);
end;
3: begin
writeln('Drak je silnejsi nez Ty a tak te rozprasil na popel.');
writeln('Zacni znovu stisknutim klavesy 0.');
readln(stav);
end;
4: begin
writeln('Drak Te vyzve na souboj, ale Ty na nej nejsi jeste pripraven.');
writeln('Musis pokracovat dal lesem. Stiskni 5.');
readln(stav);
end;
5: begin
writeln('Po dlouhe a namahave ceste jsi dorazil do mistni knajpy.');
writeln('Najis a napijes se a pokracujes dal. Kousek od knajpy potkas pocestneho');
writeln('Promluvis s nim. Stiskni 6.');
readln(stav);
end;
6: begin
writeln('Povis mu, ze se pokousis zachranit princeznu pred drakem.');
writeln('On se ti rozhodne pomoci a daruje Ti kouzelny prsten.');
writeln('Nasadit prsten na ruku a pokracovat k drakovi 7 / Strcit prsten do kapsy 8 a pokracovat k drakovi.');
readln(stav);
end;
7: begin
writeln('Prijdes k drakovi a das se s nim do boje.');
writeln('Draka zabijes a muzes pokracovat k princezne.');
writeln('Pokracovat k princezne, stiskni 9.');
prsten:=true;
readln(stav);
end;
8: begin
writeln('Prijdes k drakovi a das se s nim do boje.');
writeln('Draka zabijes a muzes pokracovat k princezne.');
writeln('Pokracovat k princezne, stiskni 9.');
readln(stav);
prsten:=false;
end;
9: begin
if prsten then
writeln('Princeznu jsi uchvatil a muzes si ji odvest do hradu.');
else
wrtieln('Princezna je rada, ze jsi ji zachranil, ale opovrhuje Tebou.');
readln(stav);
end;
end;
until stav<0;
writeln('KONEC');
readln;
end.
What is causing the fatal error ";" expected but else founded message?
Unlike C, in Pascal a semicolon ; separates statements, it does not terminate them, and the then clause requires a single statement. then WriteLn(...); else is two statements; you want then WriteLn(...) else.
Let's take this opportunity to learn how to read and use error messages to your advantage.
The compiler tells you exactly what the error is (it's a ; before an else, because both of those are mentioned in the error message). It also gives you the exact line number where it's reporting the error; that's the number (usually in parentheses right before the error message, like (from Delphi):
[DCC Error] Project2.dpr(14): E2153 ';' not allowed before 'ELSE'
So the error is happening on line 14 (in my code - your number will be different). Let's look at that line and a few before and after:
if prsten then
writeln('Princeznu jsi uchvatil a muzes si ji odvest do hradu.');
else
wrtieln('Princezna je rada, ze jsi ji zachranil, ale opovrhuje Tebou.');
So look at the error message:
';' not allowed before 'ELSE'
That clearly tells you that the ; in the line before the else is the problem (that's very clear, because it says not allowed), so remove it.
BTW, now you're going to get another error:
[DCC Error] Project2.dpr(15): E2003 Undeclared identifier: 'wrtieln'
I think you should be able to figure that one out; again, the compiler gives you the exact line number.
You're going to get another one, if you've posted your entire code:
[DCC Error] Project2.dpr(18): E2029 Statement expected but end of file found
This is because you've left out the end. that marks the end of a program file in Pascal. If you've not posted your entire code, you may not get it.
It's important to learn to actually read the words when you get an error message from the compiler. In most languages, the messages are clearly worded, and they all have information you can use to try to figure out (or at least narrow down) the problems in your code.