I am currently working on a small program for school for standard input/output. I am having trouble with read_word (or read_line) and then last_string. Sometimes it uses the most current last_string instead of the one to which it was assigned. Can anyone please help me with this? Thanks --Great_Jasmini (not-so-great-at-eiffel-programming)
PS. I think the root of my problem is linked with another little sample problem that I dont understand. Why is it that if you do: local my_array:ARRAY[STRING] do create my_array(1,2) from i:=1 until i=10 loop io.read_word my_array.force(last_string, i) i:=i+1 end
end
when i run code similar to this and then type in different words for the array, when i print off the array it is all the last word. Any help? Please? Thanks in advance--Jasmine


Typo?
Hello Jasmine,
Firstly, it would be easier to read your code if you included it within eiffel XML tags (see MediaWiki syntax when posting).
I can see a few problems with your code.
Firstly, it should be
io.last_string(you omitted the io.). Secondly, I think you need to callio.last_string.twin, which will copy the last string. The problem is thatio.last_stringis re-used, so all the entries in your array point to the same object.Thirdly, since you know the size of the array, it would be better to size it correctly, and use put, not force.
Also, avoid loops when you can (not always possible). Agents are better.
So something like this:
test is
-- Test reading multiple words from standard input
local
my_array: ARRAY [STRING]
i: INTEGER
do
create my_array.make (1, 10)
from
i := 1
until
i > 10
loop
io.read_word
my_array.put (io.last_string.twin, i)
i := i + 1
end
my_array.do_all (agent print_line)
end
print_line (a_string: STRING) is
-- Print `a_string' on standard output followed by a new line.
require
a_string_not_void: a_string /= Void
do
print (a_string + "%N")
end