Syntax error, found .and. and .or. - fortran

I just wanted to modify a small part of a very old program and I can't for the life of me figure out what I've done to anger the Fortran gods.
The original code has the following line:
if (r.gt.rstep) xappad = xappad*fakm
which I have modified to:
if (r.gt.0.58*rstep .and. r.lt.1.42*rstep) .or. (r.gt.2.08*rstep
: .and. r.lt.2.92*rstep) xappad = xappad*fakm
Which gives me the errors:
sp-co-2-MODIFIED.for(785): error #5082: Syntax error, found '.OR.'
when expecting one of: BLOCK BLOCKDATA PROGRAM MODULE TYPE COMPLEX
BYTE CHARACTER DOUBLE DOUBLECOMPLEX ...
if (r.gt.0.58*rstep .and. r.lt.1.42*rstep) .or. (r.gt.2.08*rstep
-------------------------------------------------------------------------^
sp-co-2-MODIFIED.for(786): error #6090: An array-valued operand is required in this context.
: .and. r.lt.2.92*rstep) xappad = xappad*fakm
------^
sp-co-2-MODIFIED.for(786): error #6087: An array assignment statement
is required in this context.
: .and. r.lt.2.92*rstep) xappad = xappad*fakm
-------------------------------------------^

I really don't know much FORTRAN, but it looks to me like you're missing a pair of parentheses around the conditional:
if ((r.gt.0.58*rstep .and. r.lt.1.42*rstep) .or. (r.gt.2.08*rstep .and. r.lt.2.92*rstep)) xappad = xappad*fakm

Related

Error: Unclassifiable statement at (1) in Fortran for calculations [duplicate]

This program:
C This program calculates cos(x**2)
PROGRAM COSX_SQUARE
IMPLICIT NONE
INTEGER a
REAL y, r
PRINT*, 'INPUT THE DEGREE'
PRINT*, 'BETWEEN 0 AND 360'
READ*, a
a*(3.141592/180) = y
C This part determines minus sign and calculates the function
IF (a .GT. 90) THEN
r = -(1-(y**4)/2+(y**8)/24-(y**12)/720+(y**16)/40320)
ELSEIF (a .GE. 270) THEN
r = 1-(y**4)/2+(y**8)/24-(y**12)/720+(y**16)/40320
ELSEIF (a .GT. 360) THEN
PRINT*, 'INVALID DEGREE'
PRINT*, 'DEGREE MUST BE BETWEEN 0 AND 360'
ELSEIF (a .LT. 0) THEN
PRINT*, 'INVALID DEGREE'
PRINT*, 'DEGREE MUST BE BETWEEN 0 AND 360'
END IF
PRINT*, 'THE RESULT OF COS', a, 'SQUARE IS = ', r
STOP
END
Gives this error:
a*(3.141592/180)=y
1
Error: Unclassifiable statement at (1)
I already defined a as INTEGER. Why this error keeps coming?
Yep. It is an expression which begins a statement. Maybe change it to
y = a*(3.141592/180)
if that is what you really meant.

Fortran 90 : return error

Hello I am a new user in fortran.
I try to use a subroutine for my project. The code is quite simple and below is the code
subroutine initial_data(f, x, y, z)
implicit none
REAL :: f, x, y, z
! Adding function
#include "func_expr.h"
return
end subroutine
func_expr.h is separate file that I created because expression of f is really complicated. And I got the following error messages:
error #5082: Syntax error, found IDENTIFIER 'RETURN' when expecting one of: * ) :: , ; . (/ + - : ] /) ' ** / // > .LT. < ...
return
----^
and
error #6404: This name does not have a type, and must have an explicit type. [RETURN]
return
----^
compilation aborted for initial.f90.f (code 1)
I am not sure which part is wrong and why return has the problem of it.
Any help is really appreciated!

I am trying to compile following code wrote in fortran but has many errors

I am trying to compile this following code in gfortran compiler:
do_cmd = .not. is_group
do while (.not. EOF(unit=100) .and. .not. flag_stop)
read (100, '(A)', iostat = ios) buf
if (buf(1:1)=="#") then
! comment line
elseif ((buf(1:2)=="v ") .and. do_cmd) then ! vertex
read(buf(2:), *, iostat=ios) p(1), p(2), p(3)
if (ios.ne.0) then
print "(/,2x,'Error of reading vertex position from obj-file (line #'(I3)');')", line_index
stop
end if
But I get these errors when the code is compiling:
do while (.not. EOF(unit=100) .and. .not. flag_stop)
1
Error: Keyword argument requires explicit interface for procedure ‘eof’ at (1)
and;
do while (.not. EOF(unit=100) .and. .not. flag_stop)
1
Error: Function ‘eof’ at (1) has no IMPLICIT type
I will be so grateful if anyone could help me.
edit:
I'm very new in fortran and couldn't find out how eof function works in gfortran.
What does it mean using unit=100 in argument?
And also Thought it is a logical function by default. How should I implicit its type?
As far as I know, EOF is not part of the Fortran Standard. At least I can't find it anywhere.
In most programming languages, EOF stands for "End of File", and it is fairly obvious to me that the EOF in your example is supposed to be a logical function that returns .TRUE. if and only if the read pointer associated with unit (in your case 100) points to the end of the file.
You can use the ios variable to check whether an EOF was encountered during the read. In that case, it will be -1. You can then use the exit statement to immediately quit the loop.
If you don't like the -1, and you use a Fortran 2003 compatible compiler, you might even use the module ISO_FORTRAN_ENV, which provides (amongst other things) the parameter IOSTAT_END.
Here's a very simple example:
program iso
use ISO_FORTRAN_ENV
implicit none
integer, parameter :: u = 100
character(len=80) :: buf
integer :: ios
open(unit=u, action='READ', status='OLD', file='data.dat')
read_loop : do
read(u, '(A)', iostat=ios) buf
if (ios == IOSTAT_END) exit read_loop ! EOF
if (buf(1:1) == '#') cycle read_loop ! This line is a comment
write(*, *) buf
end do read_loop
close(u)
end program iso
One note though: This code is meant as an example. It only exits the loop if it encounters an EOF -- not if the read fails for any other reason. (There are some other reasons that the the read would fail.) If that other error is persistent, then this will lead to an infinite loop.
It may be useful to try a user-defined EOF function (as shown below) to minimize the amount of code modifications; but because this function is rather inefficient, I guess it is probably better to rewrite the original code so as to utilize iostat in read statements directly (as suggested above)...
module eofmod !<-- please save this in a separate file, compile, and link with other programs
implicit none
contains
logical function EOF( unit )
integer, optional :: unit
integer :: ios
if ( .not. present( unit ) ) stop "no unit given"
read( unit, *, iostat=ios )
if ( ios == -1 ) then
EOF = .true.
else
EOF = .false. ; backspace( unit )
endif
end function
end module
program test
use eofmod !<-- needs to be inserted at the top of routines (or modules) where "EOF" is used
implicit none
character(200) :: buf
open(10, file="test.dat", status="old")
do while ( .not. EOF( unit=10 ) )
read( 10, "(a)" ) buf
print "(a)", trim( buf )
enddo
close(10)
end program

An error combining DO and IF in Fortran

This is a part of code that I am writing in Visual FORTRAN 6.6A:
.
.
.
DO 24 I=1,80
24 IF ((NODNUM(1) .EQ. I) .AND. (CAUCHY(3,2) .LT. CTI(I+12))) THEN
ARRAY(1)=C1*0.99
END IF
.
.
.
and this error occurs when I compile it:
*--------------------Configuration: ovl30u_moon1 - Win32 Debug--------------------
Compiling Fortran...
C:\Documents and Settings\XPMUser\Desktop\ovl30u_moon1.f
C:\Documents and Settings\XPMUser\Desktop\ovl30u_moon1.f(567) : Error: This is not a valid termination statement for a DO construct.
24 IF ((NODNUM(1) .EQ. I) .AND. (CAUCHY(3,2) .LT. CTI(I+12))) THEN
---^
C:\Documents and Settings\XPMUser\Desktop\ovl30u_moon1.f(569) : Error: An ENDIF occurred without a corresponding IF THEN or ELSE statement.
END IF
-----------^
Error executing df.exe.
ovl30u_moon1.obj - 2 error(s), 0 warning(s)*
I also tried this but error exists yet:
DO 24 I=1,80
24 IF ((NODNUM(1) .EQ. I) .AND. (CAUCHY(3,2) .LT. CTI(I+12))) THEN
ARRAY(1)=C1*0.99
END IF
END DO
Any advice appreciated.
The termination of a do loop using a label is not valid the way you specify it. For FORTRAN 77 use:
DO 24 I=1,80
IF ((NODNUM(1) .EQ. I) .AND. (CAUCHY(3,2) .LT. CTI(I+12))) THEN
ARRAY(1)=C1*0.99
END IF
24 CONTINUE
In Fortran 90+ you could simply leave out the label:
DO I=1,80
IF ((NODNUM(1) .EQ. I) .AND. (CAUCHY(3,2) .LT. CTI(I+12))) THEN
ARRAY(1)=C1*0.99
END IF
END DO
Or, if you depend on the label, you could use
label: DO I=1,80
IF ((NODNUM(1) .EQ. I) .AND. (CAUCHY(3,2) .LT. CTI(I+12))) THEN
ARRAY(1)=C1*0.99
END IF
END DO label
To add to Alexander Vogt's answer there is another way to restate your particular case.
While it is not allowed to use an if construct as the termination of the non-block (labelled) do it is possible to use an if statement:
DO 24 I=1,80
24 IF ((NODNUM(1) .EQ. I) .AND. (CAUCHY(3,2) .LT. CTI(I+12))) ARRAY(1)=C1*0.99
You can, but you really shouldn't: use the block construct as in the other anwer.
For more detail, see Fortran 2008 8.1.6.3 and R214.

FORTRAN 77 "Error: Unclassifiable statement at (1)"

This program:
C This program calculates cos(x**2)
PROGRAM COSX_SQUARE
IMPLICIT NONE
INTEGER a
REAL y, r
PRINT*, 'INPUT THE DEGREE'
PRINT*, 'BETWEEN 0 AND 360'
READ*, a
a*(3.141592/180) = y
C This part determines minus sign and calculates the function
IF (a .GT. 90) THEN
r = -(1-(y**4)/2+(y**8)/24-(y**12)/720+(y**16)/40320)
ELSEIF (a .GE. 270) THEN
r = 1-(y**4)/2+(y**8)/24-(y**12)/720+(y**16)/40320
ELSEIF (a .GT. 360) THEN
PRINT*, 'INVALID DEGREE'
PRINT*, 'DEGREE MUST BE BETWEEN 0 AND 360'
ELSEIF (a .LT. 0) THEN
PRINT*, 'INVALID DEGREE'
PRINT*, 'DEGREE MUST BE BETWEEN 0 AND 360'
END IF
PRINT*, 'THE RESULT OF COS', a, 'SQUARE IS = ', r
STOP
END
Gives this error:
a*(3.141592/180)=y
1
Error: Unclassifiable statement at (1)
I already defined a as INTEGER. Why this error keeps coming?
Yep. It is an expression which begins a statement. Maybe change it to
y = a*(3.141592/180)
if that is what you really meant.