Topic: Can't convert Fixnum into string?

I'm writing a basic program and I don't understand what this error message means. Can somebody help me? Here is my program:

def pixel_problem
print "How tall is the box?"
height_of_box = gets.chomp
print "How tall is the shape?"
height_of_shape = gets.chomp
print "How many pixels does the shape increase by each second?"
rate = gets.chomp
print "How many seconds has the shape been increasing (it can be 0)?"
seconds = gets.chomp
total_1 = height_of_box + 1 - height_of_shape
total_2 = total_1/rate
answer = total_2+seconds
if answer == 1
puts "It took 1 second for the shape to escape the box!"
else
puts "It took #{answer} seconds for the shape to escape the box!"
end
end

Whenever I run it it shows that error message. I've determined the error is on the line where I define the variable total_1. Can somebody please help?

Last edited by maxitola2009 (2013-01-13 17:14:04)

Re: Can't convert Fixnum into string?

Just add

.to_i

to every gets.chomp call (as everything you get from the input is String):

def pixel_problem
    print "How tall is the box?"
    height_of_box = gets.chomp.to_i
    print "How tall is the shape?"
    height_of_shape = gets.chomp.to_i
    print "How many pixels does the shape increase by each second?"
    rate = gets.chomp.to_i
    print "How many seconds has the shape been increasing (it can be 0)?"
    seconds = gets.chomp.to_i
    total_1 = height_of_box + 1 - height_of_shape
    total_2 = total_1/rate
    answer = total_2+seconds
    if answer == 1
      puts "It took 1 second for the shape to escape the box!"
    else
      puts "It took #{answer} seconds for the shape to escape the box!"
    end
  end