I am currently trying to write a program in Fortran 77 that calculates the number of words in a text file. The text files looks like this:
Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world. Hello world.
My program currently looks like this:
program COUNT
implicit none
character text*100000
integer i, a, nw
nw=1 !number of words
open(9, FILE='file.txt', STATUS='old')
read(9, '(A)') text
a=0
10 do i=1, LEN_TRIM(text)
if (text(i:i) .ne. " ") then
if (a .eq. 0) then
goto 10
else
a=0
nw=nw+1
goto 10
endif
else
if (a .eq. 0) then
a=a+1
goto 10
else
goto 10
endif
endif
enddo
print *, "Number of words: ", nw
end
I did this on paper and it should work, however, my program is getting stuck in the do-loop. I think that this might have something to do with this statement:
if (text(i:i) .ne. " ") then
Am I allowed to write the logical expression this way? If not, does anyone have any hints on how I could rewrite this code? I'm sorry if my program is a bit messy; I'm relatively new at this. I'm trying to improve my coding skills to do computational chemistry. Thanks so much for the help!
All your if branches go to goto 10
which points at the beginning of the loop. There is no way to end because this will restart the loop from the beginning. Do not use goto to start new iteration end do
s meant for that.