fortran - Pass arguments and take sum -


i passing 2 values fortran program, need sum of arguments , print result:

i have program reading arguments follows:

program argtest    implicit none   integer*4 nargs,i   character arg*80   nargs = iargc()   = 0,nargs    call getarg(i, arg)    print '(a)', arg end end  

i passing values 10 , 20. tried this:

program argtest    implicit none   integer:: nargs,i   character:: arg   integer:: num1   integer:: num2   integer:: result  nargs = iargc()   = 1,nargs    call getarg(i, arg)    !print *, arg   if( == 1)    num1 = ichar(arg)   else if(i == 2)       num2  = ichar(arg)   else    end if end result = num1+num2 print *, num1 print*,num2 end 

i need print answer 30. getting values 49 , 50 instead of getting 10 , 30. please me.

here simple version: reads arguments strings, converts them ints 1 after other, , adds them up.

program argtest    implicit none   integer*4 nargs,i   character arg*80   integer :: total, int_arg  nargs = iargc()  total = 0  = 1,nargs   call getarg(i, arg)   read(arg, *) int_arg   total = total + int_arg end  print *, "total ", total  end 

note starting argument 1, not 0 (as program name, , can't converted number).

you have updated question: ichar converts single character integer corresponds character's ascii code. need use read(ch_num, '(i)') int_num convert string "10" integer number 10.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -