Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem reading from a file and storing in array

Using the g96 compiler, I got an error saying:

INTENT(OUT) at variable 'SIZE' is never set.

Below is my subroutine. Do you know how I can fix this error? Thanks so much!

SUBROUTINE getFileItems(size,itemarray,pricearray,quantityarray)

INTEGER:: iost=0, i=0
INTEGER, INTENT(OUT):: quantityarray(50)
INTEGER, INTENT(OUT):: size
REAL, INTENT(OUT):: pricearray(50)
CHARACTER(20),INTENT(OUT)::itemarray(50)
CHARACTER(20)::namefiletoread

PRINT*,"Enter the name of file you would like to read: "
READ*,namefiletoread

OPEN(UNIT=44,FILE = namefiletoread, ACTION = "READ", !POSITION="REWIND",IOSTAT=iost)
IF(iost>0)STOP "Problem opening the file!"

DO i=1, size
READ(44,'(A,F6.2,I8)',IOSTAT=iost), itemarray(i), pricearray(i),quantityarray(i)
IF(iost<0)STOP
END DO


END SUBROUTINE
like image 793
EuropaDust Avatar asked Jul 07 '26 20:07

EuropaDust


1 Answers

You need to initialize the value of "size" somehow. Several possible methods: 1) If the size is known externally to the subroutine, make size intent(in) and set the value in the calling routine, 2) Prompt the user for the value, 3) Have the length of the array on the first line of the file and read it. 4) If the file may have a variable number of items, read it until you hit EOF, counting the number of items. Use an infinite loop and exit when you hit the EOF, setting size to the number of items read.

like image 88
M. S. B. Avatar answered Jul 10 '26 16:07

M. S. B.



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!