Home > Archive > Fortran > March 2004 > Odd behaviour (beginner)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Odd behaviour (beginner)
|
|
| Oodini 2004-03-27, 12:17 am |
| Hello,
Reading my Fortran book, I typed this little program:
-------------------------------------------------------
! Test the number of opening and closing parenthesis in a line
PARAMETER (LINESIZE = 80)
CHARACTER (LINESIZE)::LIGNE
PRINT *, "Enter expression on 1 line:"
READ *, LIGNE
LEVEL=0
DO I=1, LINESIZE
! IF (LIGNE(I:I)/=' ') THEN
! PRINT *,"Caractere",I,":", &
! LIGNE(I:I)," LEVEL=",LEVEL
! ENDIF
SELECT CASE (LIGNE(I:I))
CASE ('('); LEVEL=LEVEL+1
CASE (')')
LEVEL=LEVEL-1
IF (LEVEL<0) THEN
PRINT *, &
"Bad closing parenthesis",I
STOP
ENDIF
END SELECT
END DO
IF (LEVEL>0) THEN
PRINT *,"Missing closing parenthesis."
ELSE
PRINT *,"Expression is correct."
ENDIF
END PROGRAM
-------------------------------------------------------
The problem is that whatever the expression I type, the program takes as
the first character of the line the first character after the last '*'.
Ie, in the expression: (a*(x2+y2)+b*y*zdx*z)
The program reacts as if I typed: z)
If I type only a*(x2+y2), the program react as if I typed only (x2+y2)
You can constat that if you remove the commented code.
Thanks for help.
| |
| James Giles 2004-03-27, 12:17 am |
| Oodini wrote:
....
> PARAMETER (LINESIZE = 80)
> CHARACTER (LINESIZE)::LIGNE
>
> PRINT *, "Enter expression on 1 line:"
> READ *, LIGNE
....
> The problem is that whatever the expression I type, the program takes as
> the first character of the line the first character after the last '*'.
>
> Ie, in the expression: (a*(x2+y2)+b*y*zdx*z)
> The program reacts as if I typed: z)
>
> If I type only a*(x2+y2), the program react as if I typed only (x2+y2)
>
Reading undelimited character data with a list-directed read is
not a particularly good idea if the data contains (or might contain)
spaces, commas, slashes, or asterisks. You probably should use
an explicit format:
READ(*,'(A)') LIGNE
On standard conforming implementations, this will read the record
(or, up to 80 characters of it, in this case) without any other processing
of its content.
--
J. Giles
| |
| Oodini 2004-03-27, 12:17 am |
| > Reading undelimited character data with a list-directed read is
> not a particularly good idea if the data contains (or might contain)
> spaces, commas, slashes, or asterisks. You probably should use
> an explicit format:
>
> READ(*,'(A)') LIGNE
>
> On standard conforming implementations, this will read the record
> (or, up to 80 characters of it, in this case) without any other processing
> of its content.
OK. It does work now !
The info you gave me is at thje end of the book...
Thanks for your help !
|
|
|
|
|