Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

fortran - '*' and '/' not recognized on input by a read statement

I start learning Fortran and I'm doing a little case test program where the user types two real numbers and selects an arithmetic operators (from + - * /). The following error appears when the user selects "*"

F6502 : read <con> - positive integer expected in repeat field

and if the user selects "/" the compiler executes the default case, and displays this message

invalid operator, thanks
the result is 0.000000E+00

The program is as follows.

program operateur
implicit none

   CHARACTER(LEN=1) :: oper 
   real::a,b,res
   print*,'Give the first number a :'
   read*,a
   print*,'Give the second number b :'
   read*,b
   print*,'which operation ?'
   read*,oper
   !print*,'donnez a,b,oper :'
  ! read(*,*)a,b,oper

               select case (oper)

                  case ('+')      
                  res=a+b

                  case ('-')       
                  res=a-b

                  case ('*')       
                  res=a*b


                  case ('/')       
                  res=a/b           

                  case default
                  print*, "Invalid Operator, thanks" 

               end select

   print*,'the result is ',res 

end program operateur
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

FORTRAN input and output formatting rules are rather involved. Each input and ouptut statement has two arguments that have special meaning. For example

  READ (10,"(2I10)") m,n

The first argument is a file descriptor. Here it is 10. The second argument "(2I10)" is the format specifier. If you give an asterisk (*) as a format specifier you switch on the list-directed formatting mode.

List directed input as the name suggests is controlled by the argument list of the input operator.

1. Why asterisk (*) is special in list-directed input mode?

The input list is split into one or more input records. Each input record is of the form c, k*c or k* where c is a literal constant, and k is an integer literal constant. For example,

  5*1.01

as an instance of k*c scheme is interpreted as 5 copies of number 1.01

   5*

is interpreted as 5 copies of null input record.

The symbol asterisk (*) has a special meaning in list-directed input mode. Some compiler runtimes would report a runtime error when they encounter asterisk without an integer constant in list-directed input, other compilers would read an asterisk. For instance GNU Fortran compiler is known for standards compliance, so its runtime would accept *. Other compiler runtimes might fail.

2. What's up with slash (/)?

A comma (,), a slash (/) and a sequence of one or more blanks ( ) are considered record separators in list-directed input mode.

There is no simple way to input a slash on its own in this mode.

3. Possible solution: specify format explicitly

What you can do to make the runtime accept a single slash or an asterisk is to leave the list-directed input mode by specifying the format explicitly:

read (*,"(A1)") oper

should let you input any single character.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...