writing a program that prompts the user to enter a degree in Celsius and converts it to Fahrenheit

1.2k views Asked by At

New to coding and not sure what I'm doing wrong here. this is what I have so far:

put "Enter degrees in Celsius:"
Celsius = gets.chomp

def celsius_to_fahrenheit (c)

fahrenheit = (c * 9/5)+32
end

puts "The temperature is #{celsius_to_farenheit (Celsius)} in Farenheit"
2

There are 2 answers

0
Veridian Dynamics On

Here it is fixed:

puts "Enter degrees in Celsius:"
celsius = gets.chomp

def celsius_to_fahrenheit(c)

        fahrenheit = (c.to_f * 9/5)+32
end

puts "The temperature is #{celsius_to_fahrenheit(celsius)} in Farenheit"
  1. Incorrectly called function celsius_to_farenheit
  2. Incorrectly called function put (should be puts)
  3. c was a String, not an int / float. Can't do math on a String
  4. Celsius was capitalized. That's bad form. Starting a variable with a capital letter usually indicates a class. It's not "wrong", but it's wrong.

Overall, your biggest mistake is not reading your error logs. When you run the program, it'll output errors indicating your mistakes. One by one, you should fix the errors until your program compiles/runs. Don't just randomly fix the errors. Read the message, think about what you did and what you're trying to do, then fix the error.

Traceback (most recent call last):
test.rb:1:in `<main>': undefined method `put' for 
main:Object (NoMethodError)
Did you mean?  puts
               putc

That means you're attempting to call a function (the erroneous put function) at the top of the file.

Traceback (most recent call last):
test.rb:9:in `<main>': undefined method 
`celsius_to_farenheit' for main:Object (NoMethodError)
Did you mean?  celsius_to_fahrenheit

Same symptom, different disease.

Traceback (most recent call last):
    1: from test.rb:9:in `<main>'
test.rb:6:in `celsius_to_fahrenheit': 
undefined method `/' for "666666666":String (NoMethodError)

See how it tells me everything I need to know?

I converted that "c" value to a float. By default, a user's input should be interpreted as a String. You have to "cast" (convert) the variable to a float if you want the user's input to be interpreted as a float and not a string.

The reason you see 666666666 is because Ruby tries to be fancy. If you multiply a string by an int, N, you get that string repeated N times.

eg. "hello world" * 2 # hello worldhello world

0
Phanindra On

This is how the code should be.

puts "Enter degrees in Celsius:"
Celsius = gets.chomp.to_f

def celsius_to_farenheit (c)
fahrenheit = (c * 9/5)+32
end

puts "The temperature is #{celsius_to_farenheit (Celsius)} in Farenheit"

There are a certain mistakes you are doing

  1. The function name when you are defining it and then calling it are different. They should be exactly the same. You defined it as "celsius_to_fahrenheit" but called it as "celsius_to_farenheit"
  2. The run time input which you actually give (gets.chomp) is a string. So convert it to float using ".to_f". You can do it when you are assigning it to a variable (Celsius) like I did or even when you are passing the argument to the function
  3. You incorrectly called put instead of puts in the first line