主题:infinite format loop
dingsheng1206
[专家分:0] 发布于 2010-09-18 16:06:00
我程序编译,链接都没问题,可是运行时输入数字后却出错了,我检查了循环没问题啊,students 是从链盘输入的,怎么不行,请高手赐教哦,谢谢
module typedef
type student
integer Chinese,English,Math
end type
end module
program ex0906
use typedef
implicit none
integer::students
type(student),allocatable::s(:)
character(len=80)::filename="data.txt"
integer,parameter::fileid=10
integer::i
write(*,*)"how many students are there in the class?"
read(*,*)students
allocate(s(students),stat=i)
if(i/=0)then
write(*,*)"allocate buffer fail."
stop
endif
open(fileid,file=filename)
do i=1,students
write(*,"('please input Chinese,English,Math for student i2')") i
read(*,*)s(i)%Chinese,s(i)%English,s(i)%Math
write(fileid,"('number:'i2',Chinese:'i3'English:'i3'Math:'i3)")i,s(i)
enddo
close(fileid)
stop
end
回复列表 (共6个回复)
沙发
dongyuanxun [专家分:7180] 发布于 2010-09-18 17:11:00
gfortran 4.5
ivf 11.1
均无问题
板凳
dingsheng1206 [专家分:0] 发布于 2010-09-18 17:52:00
[quote] gfortran 4.5
ivf 11.1
均无问题[/quote]
请问你执行时,有没有输入数据呢,
读数据能成功吗。
我一输数据就显示有问题啊,
3 楼
dongyuanxun [专家分:7180] 发布于 2010-09-18 18:40:00
[quote][quote] gfortran 4.5
ivf 11.1
均无问题[/quote]
请问你执行时,有没有输入数据呢,
读数据能成功吗。
我一输数据就显示有问题啊,[/quote]
能
就是输入成绩吧
没有问题
4 楼
asymptotic [专家分:16630] 发布于 2010-09-18 23:07:00
Your source code does not conform to Fortran 2003 standard in I/O Editing, the following
procedure correct it.
module typedef
implicit none
type, public:: student
integer:: Chinese, English, Math
end type
end module
program ex0906
use typedef
implicit none
integer:: students
type(student), allocatable:: s(:)
character(len = 80):: filename = "data.txt"
integer, parameter:: fileid = 10
integer:: i
write(*, *) "how many students are there in the class?"
read(*, *) students
allocate(s(students), stat = i)
if (i /= 0) then
write(*,*)"allocate buffer fail."
stop
endif
open(fileid, file = filename)
do i = 1, students, 1
write(*, *) 'please input Chinese,English,Math for student'
write(*,"(i2)") i
read(*, *) s(i) % Chinese, s(i) % English, s(i) % Math
write(*, "(3(A, 2X, I3))") &
'number: ', i, &
'Chinese: ', s(i) % Chinese, &
'English: ', s(i) % English, &
'Math: ', s(i) % Math
end do ! i
close(unit = fileid)
stop
end program
5 楼
dingsheng1206 [专家分:0] 发布于 2010-09-19 09:18:00
[quote]Your source code does not conform to Fortran 2003 standard in I/O Editing, the following
procedure correct it.
module typedef
implicit none
type, public:: student
integer:: Chinese, English, Math
end type
end module
program ex0906
use typedef
implicit none
integer:: students
type(student), allocatable:: s(:)
character(len = 80):: filename = "data.txt"
integer, parameter:: fileid = 10
integer:: i
write(*, *) "how many students are there in the class?"
read(*, *) students
allocate(s(students), stat = i)
if (i /= 0) then
write(*,*)"allocate buffer fail."
stop
endif
open(fileid, file = filename)
do i = 1, students, 1
write(*, *) 'please input Chinese,English,Math for student'
write(*,"(i2)") i
read(*, *) s(i) % Chinese, s(i) % English, s(i) % Math
write(*, "(3(A, 2X, I3))") &
'number: ', i, &
'Chinese: ', s(i) % Chinese, &
'English: ', s(i) % English, &
'Math: ', s(i) % Math
end do ! i
close(unit = fileid)
stop
end program[/quote]
谢谢你,不过我没想过用FORTRAN2003的标准啊,我这不是2003的标准,我是90或者95的格式吧,而且你觉得他们有很大的不同吗
6 楼
dingsheng1206 [专家分:0] 发布于 2010-09-19 09:23:00
[quote]Your source code does not conform to Fortran 2003 standard in I/O Editing, the following
procedure correct it.
module typedef
implicit none
type, public:: student
integer:: Chinese, English, Math
end type
end module
program ex0906
use typedef
implicit none
integer:: students
type(student), allocatable:: s(:)
character(len = 80):: filename = "data.txt"
integer, parameter:: fileid = 10
integer:: i
write(*, *) "how many students are there in the class?"
read(*, *) students
allocate(s(students), stat = i)
if (i /= 0) then
write(*,*)"allocate buffer fail."
stop
endif
open(fileid, file = filename)
do i = 1, students, 1
write(*, *) 'please input Chinese,English,Math for student'
write(*,"(i2)") i
read(*, *) s(i) % Chinese, s(i) % English, s(i) % Math
write(*, "(3(A, 2X, I3))") &
'number: ', i, &
'Chinese: ', s(i) % Chinese, &
'English: ', s(i) % English, &
'Math: ', s(i) % Math
end do ! i
close(unit = fileid)
stop
end program[/quote]
说实话,我是在学习FORTRAN95彭国伦 这本书,有练习书上的程序的时候发现有问题,后事我找到他书上配套的电子版的源码,一试却可以,可是是他的和我很接近啊,比你还接近我的,麻烦你看看吧,谢谢我哦,
module typedef
type student
integer Chinese,English,Math
end type
end module
program ex0906
use typedef
implicit none
integer :: students
type(student), allocatable :: s(:)
character(len=80) :: filename = "data.txt"
integer, parameter :: fileid = 10
integer :: i
write(*,*) "班上有多少学生?"
read (*,*) students
allocate( s(students), stat=i )
if ( i/=0 ) then
write(*,*) "Allocate buffer fail."
stop
end if
open(fileid, file=filename)
do i=1,students
write(*,"('请输入'I2'号同学的中文、英文及数学成绩')") i
read(*,*) s(i)%Chinese, s(i)%English, s(i)%Math
write(fileid,"('座号:'I2/'中文:'I3' 英文:'I3' 数学:'I3)") i,s(i)
end do
close(fileid)
stop
end
我来回复