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

前端 未结 2 2041
Happy的楠姐
Happy的楠姐 2020-11-28 17:06

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

相关标签:
2条回答
  • 2020-11-28 17:31

    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.

    0 讨论(0)
  • 2020-11-28 17:33

    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
    
    0 讨论(0)
提交回复
热议问题