Joyce
Joyce

Reputation: 1

Unknown operator n in Fortran

Program is meant to calculate sqrt of real numbers entered by its user, but rejects calculating complex numbers (in an if statement). User should be able to exit program when he wants. There's an error on if(dataType.n.real)then, the build message alert is "Unknown operator n". What am I doing wrong?

!square of a number
  real:: x,n
  integer::a
  write (*,*) "1 to continue, or any key to exit: "
  read(*,*)a
  do while(a==1)
    write(*,*)"Type the value of n"
    read(*,*)n
    if(dataType.n.real) then
        x=sqrt(n)
        write(*,*)"x = "
        write(*,*)x
        else
            write(*,*)"Please enter a real number: "
            end if
            write (*,*) "Press 1 to continue, or any key to exit: "
            read (*,*)a
            end do
            stop
            end

Upvotes: 0

Views: 138

Answers (1)

if(dataType.n.real is not a Fortran way to compare datatypes and there is no reason to use something like that here. n is declared real so it is always real even if it happens to contain an integer value.

(High Performance Mark explained in his comment what the compiler sees in dataType.n.real, it sees an operator .n. applied to two variables dataType and real. And it complains that it does not know the .n. operator and later may complain that it does not know the dataType variable either.)

Fortran is a statically typed language and if you declare your variable real :: a it is just of type real forever. It differs from dynamic languages like Python. If the user tries to input something illegal (for real) the read statement will fail. That can be controlled by the iostat= or the err= argument).

integer :: ier

read(*,*,iostat=ier) n
if (ier/=0) then
  !invalid input, do what is necessary
end if

Upvotes: 1

Related Questions