fortrangfortran

"Cannot assign to named constant" (reassigning a variable)


I'm using a flag f for some error checking. Fortran (or maybe gfortran) won't let me reassign its value when I want to perform another check.

integer, dimension(:,:), allocatable :: A
integer :: f, n        

write (*, *) "Give an integer n > 0. n = "

   read (*, IOSTAT=f) n

   do while(f /= 0)
      print *, "Error with input. Please try again."
      read (*, IOSTAT=f) n
   end do

   write (*, "(a, i5)") "You have entered n = ", n

   allocate(A(n), STAT=f)
   if (f /= 0) 
      print *, "Error: not enough memory for A."
   end if

NB: I think copy-pasting may mess up my spacing.

f has been declared as an integer (and not as a parameter integer): integer :: f.

I'm very much a beginner with Fortran, so it's very possible I've made some unthinkable mistake!


Solution

  • This error message is confusing, but the problem is that

       if (f /= 0) 
          print *, "Error: not enough memory for A."
       end if
    

    should be

       if (f /= 0) then
          print *, "Error: not enough memory for A."
       end if