Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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!

like image 458
jamesh625 Avatar asked Sep 07 '25 04:09

jamesh625


1 Answers

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
like image 186
Vladimir F Героям слава Avatar answered Sep 10 '25 14:09

Vladimir F Героям слава



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!