0

I am trying to count the number of lines in a file in Fortran 77 (My file has two columns, both with numbers)

I am getting an unclassifiable statement and unexpected data declaration and I am not sure why. This is my code: (new to fortran 77):

    PROGRAM Exercise

C
C John Smith
C

C
C PARAMETERS
C
      REAL PRECISION UNUM
      PARAMETER (UNUM=15)
C
C LOCAL VARIABLES
C

C
C FUNCTION DECLARATIONS
C

      INTEGER*8 PRECISION NUMLIN

C
C COMMON VARIABLES
C

C
C DATA STATEMENTS
C
      DATA FILEN /'linecounttester.txt'/
C
C MAIN PROGRAM MODULE
C

      OPEN(UNIT=UNUM, FILE = FILEN, STATUS='OLD')

C
C function counts the lines in the file

C

      FUNCTION NUMLIN
      INTEGER*8 PRECISION NUMLIN
      CHARACTER*256 LINE
      100 READ(UNUM,*,END=200) LINE
      NUMLIN=NUMLIN+1
      GOTO 100
      200 CONTINUE

      RETURN
      END

      REWIND(UNUM)

      CLOSE(UNUM)

This is my data file: (just a tester so I can count the lines to 8): 1 100 2 200 3 300 4 400 5 500 6 600 7 700 8 800

These are my errors:

Exercise.for:58.6:

  FUNCTION NUMLIN                                                   
  1

Error: Unclassifiable statement at (1) Exercise.for:59.32:

  INTEGER*8 PRECISION NUMLIN                                        
                            1

Error: Symbol 'precisionnumlin' at (1) already has basic type of INTEGER Exercise.for:60.72:

  CHARACTER*256 LINE                                                
                                                                    1

Error: Unexpected data declaration statement at (1) Exercise.for:61.7:

  100 READ(UNUM,*,END=200)                                          
   1

Error: Invalid character in name at (1) Exercise.for:62.6:

  LINE                                                              
  1

Error: Unclassifiable statement at (1) Exercise.for:65.7:

  200 CONTINUE                                                      
   1

Error: Invalid character in name at (1) Exercise.for:41.10:

  DATA FILEN /'linecounttester.txt'/                                
      1

Error: Incompatible types in DATA statement at (1); attempted conversion of CHARACTER(1) to REAL(4) Exercise.for:46.16:

  OPEN(UNIT=UNUM, FILE = FILEN, STATUS='OLD')                       
            1

Error: UNIT tag at (1) must be of type INTEGER Exercise.for:64.72:

  GOTO 100                                                          
                                                                    1

Error: Label 100 referenced at (1) is never defined Exercise.for:1.72:

  PROGRAM EXERCISE                                    
                                                                    1

Exercise.for:70.72:

  REWIND(UNUM)                                                      
                                                                    2

Error: Two main PROGRAMs at (1) and (2)

Can anyone help me out?

Surz
  • 984
  • 3
  • 11
  • 36
  • 1
    Personally I no longer help people write FORTRAN77. It's rather like helping people amputate limbs quickly without anaesthetic. And for much the same reason: there are more modern approaches which are much less painful for all concerned. If I were trying to help, my first step would be to question why you report errors in lines (*eg* `PROGRAM NUMBERCOUNTINGEXERCSIE`) which do not appear in thecode you have posted. – High Performance Mark Apr 16 '14 at 16:34
  • 1
    I'll be pedantic and say that this source is not FORTRAN 77. INTEGER*8 is non-standard. Also, "REAL PRECISION UNUM" possibly doesn't mean what you think it is. I agree with HPM that the messages don't seem to match the source. – Steve Lionel Apr 16 '14 at 17:20
  • is that `PRECISION` maybe some legacy extension? Certainly not f77. – agentp Apr 16 '14 at 18:52
  • REAL PRECISION the data type? – Surz Apr 16 '14 at 19:17
  • right there is no such data type in standard fortran (of any vintage). Your compiler thinks you are declaring a `REAL`variable named `PRECISIONUNUM` (note spaces are not significant in fortran). – agentp Apr 16 '14 at 20:17
  • Having `function` inside a main program without a contains is confusing the compiler. The order of your statements is wrong. For an example of how to arrange a function or subroutine and a main program in Fortran >=90, see http://stackoverflow.com/questions/6511711/computing-the-cross-product-of-two-vectors-in-fortran-90 – M. S. B. Apr 16 '14 at 20:27
  • This is Fortran77; I assume this is still applicable? – Surz Apr 16 '14 at 20:29
  • Why are you writing Fortran 77 in 2013? The statement order is a problem in any version of Fortran. The example layout is Fortran >=90. – M. S. B. Apr 16 '14 at 22:06

1 Answers1

1

You are mixing your namespaces.

you have a

PROGRAM

statement, which needs a corresponding

END

statement before you start declaring your functions.

that should fix the compilation errors you see. however, i do not see where you actually call the function you create. in other words, it appears that you are defining a function without actually using it. I would suggest that you first write the code without the function, just to make sure it works. then separate it out, until you get the hang of this :)

EDIT: I also second the comments under your question, about the strange syntax you are using. though I will not dissuade you from using FORTRAN77 but i hope you have a very good reason to do so ;)

physphun
  • 376
  • 3
  • 6