Dividing by half in ruby to create an effective calculator

47 views Asked by At

For the past while I've been working on a calculator, but have run into problems when needing to divide by a half. I'll add the offending bit of code along with a loop to keep it open below.

on = true
while on == true do
    half = 1.0 / 2.0
    puts ("FUNCTION IN TESTING MODE, DO NOT EXPECT IT TO FUNCTION PROPERLY")
    puts ("Area of a triangle")
    print("What is the legnth of the base? ").to_i
    base = gets.chomp("base")
    print("\nWhat is the height? ")
    height = gets.chomp("height").to_i
    PreAreaT = base * height
    AreaT = PreAreaT * half
    puts("The area of the triangle is #{AreaT}")
end

So essentially, how on Earth do I get the program to display an answer, rather than outputting nothing for the answer?

EDIT:As it would turn out the code above is improperly done. I've spent nearly two weeks asking myself why it wouldn't work only to find I had .to_i after a print statement rather than the input.

1

There are 1 answers

3
Makoto On BEST ANSWER

Your to_i call is switched around here.

print("What is the legnth of the base? ").to_i
base = gets.chomp("base")

Should be the other way 'round.

print("What is the length of the base? ")
base = gets.chomp("base").to_i

Further, chomp will attempt to remove any occurrences of base or height from the string. Be sure that you're intention is to remove those occurrences; if you want to remove whitespace, you'll have to take a different approach.