Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read from a string into 8-byte integer?

Status
Not open for further replies.

MBFerguson

Programmer
Dec 3, 2021
3
US
I have the following 8-byte integer defined as follows:

integer*8 :: strINT = 0

And an allocatable string defined using

character:)), allocatable :: str
str = "9876543210"


I'm trying to set the strINT variable using the read function

read(str,*) strINT

However, this gives the following error

Fortran runtime error: Bad integer for item 1 in list input


The read function seems like it does not work with my 8-byte integer. How can I get the strINT variable to be set with the value represented by str?
 
Hi MBFerguson,

I tried this:

mbferguson.f95
Code:
[COLOR=#a020f0]program[/color] mbferguson
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color][COLOR=#a52a2a][b]*[/b][/color][COLOR=#ff00ff]8[/color] :: strint [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#2e8b57][b]character[/b][/color](:), [COLOR=#2e8b57][b]allocatable[/b][/color] :: str
  
  str [COLOR=#a52a2a][b]=[/b][/color] [COLOR=#ff00ff]"9876543210"[/color]
  [COLOR=#a52a2a][b]read[/b][/color](str,[COLOR=#a52a2a][b]*[/b][/color]) strint

  [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#a52a2a][b]*[/b][/color], [COLOR=#a52a2a][b]*[/b][/color]) [COLOR=#ff00ff]"str = "[/color], str
  [COLOR=#a52a2a][b]write[/b][/color]([COLOR=#a52a2a][b]*[/b][/color], [COLOR=#a52a2a][b]*[/b][/color]) [COLOR=#ff00ff]"strint = "[/color], strint
[COLOR=#a020f0]end program[/color] mbferguson
and it works:
Code:
$ gfortran mbferguson.f95 -o mbferguson 
$ ./mbferguson 
 str = 9876543210
 strint =            9876543210

I have this compiler version:
Code:
$ gfortran --version
GNU Fortran (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

What compiler are you using ?
 
Hello mikrom,

I am using the following compiler version:

$ gfortran --version
GNU Fortran (GCC) 11.2.0


I am not necessarily tied down to this specific compiler, although I don't understand why a more recent version would have problems a previous version doesn't have.

Here is my entire source code for the program I'm writing:

program Euler43

implicit none
integer :: time_start, time_finish, clock_finish
integer*8 :: solution = 0

character:)), allocatable :: str
character(3) :: s234, s345, s456, s567, s678, s789, s8910
integer :: i234, i345, i456, i567, i678, i789, i8910
integer*8 :: max_ = 4200000000, strINT = 1402356789
integer, dimension(10) :: reverse, numbers = (/ 1,4,0,2,3,5,6,7,8,9 /)
integer :: i, j, u, x, y, z

call SYSTEM_CLOCK(time_start)

str = "1402356789"
do while(strINT .LT. max_)

do i = 8,0,-1
if(numbers(i+1) .LT. numbers(i+2)) then
x = i
EXIT
end if
end do

do i = 9,0,-1
if (numbers(x+1) .LT. numbers(i+1)) then
y = i
EXIT
end if
end do

z = numbers(x+1)
numbers(x+1) = numbers(y+1)
numbers(y+1) = z

do i = 9,(x+1),-1
reverse(9-i+1) = numbers(i+1)
end do

do i = 0,(9-x)
j = x + 1 !mbf
numbers(j+1) = reverse(i+1)
j = j + 1
end do

str = ""
do u = 0,9
str = str // CHAR(numbers(u+1)+48)
end do

s234 = str(1:3)
s345 = str(2:4)
s456 = str(3:5)
s567 = str(4:6)
s678 = str(5:7)
s789 = str(6:8)
s8910 = str(7:9)

read(s234,*) i234
read(s345,*) i345
read(s456,*) i456
read(s567,*) i567
read(s678,*) i678
read(s789,*) i789
read(s8910,*) i8910
read(str,*) strINT
if( (modulo(i234,10)==0) .and. (modulo(i345,10)==0) .and. (modulo(i456,10)==0) .and. &
(modulo(i567,10)==0) .and. (modulo(i678,10)==0) .and. (modulo(i789,10)==0) .and. &
(modulo(i8910,10)==0) ) then

solution = solution + strINT
end if

end do
call SYSTEM_CLOCK(time_finish)
clock_finish = time_finish - time_start
write (*,"(A10,I10,//,A21,I5,A14)") "Solution: ",solution,"Program completed in ",clock_finish," milliseconds."

end program Euler43


This program is for problem 43 on the website projecteuler.net. You can see at the top of the file is where I'm declaring the str & strINT variables, and towards the end of the file is where the read() function throws the error I mentioned.

I'm wondering if the problem might have something to do with the fact that the compiler prompts me to use the -fno-range-check

$gfortran Euler43.f90 -o Euler43 -fno-range-check
 
Another update -- Just realized now that my error was not being caused by anything related to the integer*8 declaration or the read() function itself.

I had a minor indexing problem on the do i = 0,(9-x) within the lexicographic sorting portion of the code which simply needed to have a -1 added in the index like so do i = 0,(9-x-1).

What was occurring was the substrings were being assigned invalid ASCII codes, which ended up breaking the strINT variable when concatenated.
 
I didn't get a bug. I got the result
Code:
$ gfortran euler43.f90 -o euler43 -fno-range-check
$ ./euler43 
 Solution: 10900000000

Program completed in     0 milliseconds.
 
It looks like your answer was faster than mine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top