Fortran 读取语句的 iostat 参数返回代码 5001



我正在fortran中编写一个计算BMI的小代码,但我对read语句的iostat参数有一个问题。整个代码如下:

program calculate_bmi
! Simple BMI calculator
implicit none
character :: unit_system
character :: height_unit*15, weight_unit*10
real :: height, weight, bmi
integer :: ierror

print *, 'Would you like to use imperial or metric system?'
systemloop: do
print *, 'Press [I] for imperal system or press [M] for metric system.'
read *, unit_system
if (unit_system == 'i' .or. unit_system == 'I' .or. &
unit_system == 'm' .or. unit_system == 'M') exit systemloop
end do systemloop
if (unit_system == 'i' .or. unit_system == 'I') then
height_unit='inches'
weight_unit = 'pounds'
else
height_unit = 'centimeters'
weight_unit= 'kilograms'
end if
print *, 'Type in your height in ', height_unit
read (*, iostat = ierror) height
if (ierror /= 0) then
print *, 'Invalid input for height!'
call exit
end if
print *, 'Type in your weight in ', weight_unit
read (*, iostat = ierror) weight
if (ierror /= 0) then
print *, 'Invalid input for weight!'
call exit
end if
if (unit_system == 'i' .or. unit_system == 'I') then
bmi = 703 * weight / (height**2)
else
bmi = weight / ((height/100)**2)
end if
print *, 'Your BMI is: '
write (*, 1) NINT(bmi*10)/10.0
1 format(f6.1)
end program calculate_bmi

问题发生在以下代码块:

print *, 'Type in your height in ', height_unit
read (*, iostat = ierror) height
if (ierror /= 0) then
print *, 'Invalid input for height!'
call exit
end if
print *, 'Type in your weight in ', weight_unit
read (*, iostat = ierror) weight
if (ierror /= 0) then
print *, 'Invalid input for weight!'
call exit
end if

iostat总是以代码的形式返回5001,read语句不等待输入,只是终止程序。我做了一些谷歌搜索,但无法真正弄清楚到底是什么问题。如果我从read语句中删除iostat,代码就可以正常工作,但在这种情况下,我没有任何错误处理和防止错误输入的方法。

我对fortran有点陌生,所以请让我知道我在哪里犯了错误。此外,我使用Gfortran作为我的编译器。

read语句通常需要两个位置参数,一个用于输入单元(或*以声明其为标准输入(,另一个用于格式(或者*自行计算(。

无括号,即有

read *, some_var

该命令自动默认为标准输入流,因此单个*仅适用于该格式。但是,一旦使用括号,就需要声明输入单位和格式。

一段时间以来,read语句以及openclosewrite都支持iomsg参数,该参数是一个字符串,为错误提供了人类可读的解释。

试着这样修改:

character(len=100) :: ioerrmsg
...
read (*, *, iostat=ioerror, iomsg=ioerrmsg) weight
if (ioerror /= 0) then
print *, "Encountered Error:"
print *, ioerrmsg
call exit
end if

这应该会给你一些关于正在发生的事情的提示

干杯

PS:如果有人知道iomsg字符串默认有多长,请在评论中告诉我。但长度100通常对我有用。

最新更新