Question

I've got a program that accepts three numbers and calculates whether or not the three numbers form a triangle. If it does, it prints the three sides and the perimeter. For some reason, however, when printing the perimeter, instead of saying Perimeter= 3 (for example), it says Perimeter= @@. Any idea what would cause this? This is my first attempt using Fortran, so I'm a little lost.

PROGRAM G6P1
   IMPLICIT NONE
   REAL :: a, b, c, perimeter
   CHARACTER(LEN=30) :: Format, PerimeterFormat
   Format = "(3(2(A,X),F6.1,A,2X))"
   PerimeterFormat = "(' ',10A,X,F7.1)"
   PRINT *,'Please enter 3 numbers to form a triangle'
   READ *, a, b, c
   IF (a+b.gt.c) THEN
      IF (b+c.gt.a) THEN
         IF (a+c.gt.b) THEN
            PRINT *,'It is a triangle!'
            WRITE (*,Format) 'A','=',a,';','B','=',b,';','C','=',c,';'
            perimeter = A+B+C
            WRITE (*,PerimeterFormat) '','Perimeter=',perimeter

         ELSE
            PRINT *,'The sides do not form a triangle'
            WRITE (*,Format) 'A','=',a,';','B','=',b,';','C','=',c,';'
         ENDIF
      ELSE
         PRINT *,'The sides do not form a triangle'
         WRITE (*,Format) 'A','=',a,';','B','=',b,';','C','=',c,';'
      ENDIF
   ELSE
      PRINT *,'The sides do not form a triangle'
      WRITE (*,Format) 'A','=',a,';','B','=',b,';','C','=',c,';'
   ENDIF
END PROGRAM G6P1
Was it helpful?

Solution

It's your format statement:

PerimeterFormat = "(' ',10A,X,F7.1)"

When it comes to characters with Fortran output, you need to put the length after the specifier A, i.e.:

PerimeterFormat = "(' ',A10,X,F7.1)"

Changing that should fix it (it did for me with gfortran 4.6.3).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top