防止FORTRAN在输入字符而不是数字时关闭 [英] Prevent FORTRAN from closing when a character is inputed instead of a number

查看:158
本文介绍了防止FORTRAN在输入字符而不是数字时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 程序测试
整型变量var
read(*,*)var
end

事情是我通常会因分心而输入一串字符(即: yes )。如何防止我的代码完全停止,而是显示类型的错误消息您输入的值不正确。再试一次



我在想这样的事情:

 <$ c (*,*)var 
if(var.not.a.number)then
write(*,*)'You我输入了一个不正确的值。再试一次'
goto 10
endif
end

var.not.a.number 条件看起来像?
我使用 gfortran 在Ubuntu下编译。






编辑:谢谢大家!我最终实现了HPM的第三个选项,因为它是最简单的:

 程序测试
整数变量var,iostat,ios
读取(*,*,iostat = ios)var
if(ios.ne.0)然后
write(*,*)'您输入的值不正确。再试一次'
goto 10
endif
end

特别感谢至 User7391 谁花了所有的代码编写!

解决方案

您正在使用列表导向输入。语句 read(*,*)中的第二个 * 实际上告诉编译器/运行时系统,在运行时为它提供一些可以解释为 integer 的东西。如果你想给自己输入错误的自由,你至少有3种选择。你可以像@ User7391的答案已经说过,将输入读入字符变量并自己解析。这种用户甚至愿意为您编写代码!

  • 您可以将读取命令修改为 read(*,*,err = 1234)其中 1234 是错误处理语句的标签。这种方法现在被认为是相当老式的,并且可能会被忽视。
  • 您可以将读命令修改为像 read(*,*,iostat = ios )其中 ios 是一个整数变量,它捕获 iostat (I / O状态标志)由读取语句报告。如果(iostat / = 0)... 为错误处理,那么你可以写下这一行。这被认为是更新的方法。


  • I have a read statement that expects a number, very simple example code:

    program test
    integer var
    read(*,*) var
    end
    

    The thing is that I usually input a string of characters (ie: yes) on account of being distracted. How can I prevent my code from halting entirely and instead display an error message of the type You've entered an incorrect value. Try again?

    I'm thinking something like:

        program test
        integer var
    10  read(*,*) var
        if (var.not.a.number) then
          write(*,*)'You've entered an incorrect value. Try again'
          goto 10
        endif
        end
    

    What would that var.not.a.number condition look like? I use gfortran to compile under Ubuntu.


    Edit: Thank you all! I ended up implementing HPM's 3rd option since it was the simplest one:

        program test
        integer var,iostat,ios
    10  read(*,*,iostat=ios) var
        if (ios.ne.0) then
          write(*,*)'You've entered an incorrect value. Try again'
          goto 10
        endif
        end
    

    Special thanks to User7391 who took the effort to write all that code!

    解决方案

    You're using list-directed input. The second * in the statement read(*,*) essentially tells the compiler/run-time system that you will provide it with something at run time that can be interpreted as an integer. If you want to give yourself the freedom to make mistakes on input you have (at least) 3 choices.

    1. You can, as @User7391's answer already says, read the input into a character variable and parse it yourself. That kind user has even offered to write the code for you !
    2. You could modify the read command to something like read(*,*,err=1234) where 1234 is the label of your error-handling statement(s). This approach is now considered rather old-fashioned and might be frowned upon.
    3. You could modify the read command to something like read(*,*,iostat=ios) where ios is an integer variable which catches the iostat (i/o status flag) reported by the read statement. You might then write the line if (iostat/=0) ... for error handling. This is considered to be the more up to date approach.

    这篇关于防止FORTRAN在输入字符而不是数字时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆