- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I seem to get a wrong result on using nested select type. Can you tell me if this code is incorrect or if it is a problem with the compiler (ifort version 12.1.0).
[fortran]module comparator
implicit none
contains
function compareLess(A,B) result(comp)
class(*),pointer, intent(in) :: A
class(*),pointer, intent(in) :: B
logical :: comp
comp=.false.
select type(C=>A)
! Check if Type of A is integer
type is(integer)
select type(D=>B)
type is(integer)
comp = C.lt.D
end select
print*,'Error :: A is type int, B is not type int'
stop
! Check if Type of A is real
type is(real)
print*,'Type of A is real',C
select type(D=>B)
type is(real)
comp = C.lt.D
end select
print*,'Error :: A is type real, B is not type real'
stop
! Check if Type of A is character
type is(character(len=*))
select type(D=>B)
type is(character(len=*))
comp = C.lt.D
end select
print*,'Error :: A is type character, B is not type character'
stop
end select
end function compareLess
end module comparator
program TestComp
use comparator
class(*), pointer :: A
class(*), pointer :: B
! enable to test integer
integer, pointer :: C
integer, pointer :: D
! enable to test reals
!real, pointer :: C
!real, pointer :: D
logical :: comp
allocate(C,D)
! enable to test reals
!call random_number(C)
!call random_number(D)
C=5
D=8
A => C
B => D
! Method 1
!!---
! Code compiles, and runs but the result is,
! Error :: A is type int, B is not type int
!!---
comp = compareLess(A,B)
print*,'A is',C,'and B is',D,'A is less than B :',comp
! Method 2
!!---
! Code compiles, and throws error,
! severe (40): recursive I/O operation, unit -1, file unknown
! This may be understanable, I tried method 2 first before i tried method 1.
! How does on deal with a situation like this, where one wants to print the result
! of compare but also let the compare print an error messge in case of error in compare.
! Test for sucess of compare and then print?
!!---
! Enable to test method 2
!print*,'A is',C,'and B is',D,'A is less than B :',compareLess(A,B)
end program TestComp[/fortran]
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You need to add a return after line 14, line 22, and line 31. Because there is no return of break statement, the program continues and hits the error code after it does the comparison.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks. I should have paid more attention :)
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page