How to convert character to integer in fortran? [d

2019-07-29 23:51发布

This question already has an answer here:

How to manipulate the command line argument? for example


te.f90

   program print_
   integer :: i
   character(len = 32) :: arg
   i = 1   
   Do 
      call get_command_argument(i, arg)
      if ( len_trim(arg) == 0) exit
          write(*,*) trim(arg)
          write(*,*) trim(arg)**2
          i = i + 1
   end do
   end program print_


te.sh

#!/bin/bash

for (( x = 1; x <=3; x++ ))
do
   ./te $x
done

I pass $x as arg which has type character, but I want to manipulate arg as a number, when I execute ./te.sh, I got the error promotion Operands of binary numeric operator '**' at (1) are CHARACTER(1)/INTEGER(4).

what to do?

1条回答
贼婆χ
2楼-- · 2019-07-30 00:30

You'll need to convert the string (arg) into an integer.

program print_
   integer :: i, iarg
   character(len = 32) :: arg
   i = 1   
   Do 
      call get_command_argument(i, arg)
      if ( len_trim(arg) == 0) exit
          write(*,*) trim(arg)
          read(arg,"(I)") iarg
          write(*,*) iarg**2
          i = i + 1
   end do
end program print_
查看更多
登录 后发表回答