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 e
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.
*
) 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.
/
)?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.
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.
Ok then the correct source code is
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 (*,"(A1)") 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