Dividing by half in ruby to create an effective calculator -
for past while i've been working on calculator, have run problems when needing divide half. i'll add offending bit of code along loop keep open below.
on = true while on == true half = 1.0 / 2.0 puts ("function in testing mode, not expect function properly") puts ("area of triangle") print("what legnth of base? ").to_i base = gets.chomp("base") print("\nwhat height? ") height = gets.chomp("height").to_i preareat = base * height areat = preareat * half puts("the area of triangle #{areat}") end
so essentially, how on earth program display answer, rather outputting nothing answer?
edit:as turn out code above improperly done. i've spent 2 weeks asking myself why wouldn't work find had .to_i after print statement rather input.
your to_i
call switched around here.
print("what legnth of base? ").to_i base = gets.chomp("base")
should other way 'round.
print("what length of base? ") base = gets.chomp("base").to_i
further, chomp
attempt remove occurrences of base
or height
string. sure you're intention remove occurrences; if want remove whitespace, you'll have take different approach.
Comments
Post a Comment