Beginner looking for help with Fahrenheit to Celsius code

Hey guys. I'm new here :slight_smile:

I've been going through the Rust handbook and I got to the point where the book recommends that I do the Fahrenheit to Celsius convertor exercise. Well I did it and it compiles, and this would be my first real Rust code I've written independently. Converting Celsius to Fahrenheit works fine, but I can't get converting Fahrenheit to Celsius to work.

Specifically, I'm stuck on calculating different data types. I know something is wrong with my formula and the way I phrase the calculation from Fahrenheit to Celsius, but I have literally no clue how to fix it. I know one of the number there needs to be a floating point but I am lost on what I need to change. I would really appreciate any feedback on how to fix it, and a general review of my code would be welcome :smiley:

Code pasted below. The line in question is line 65 in the second function. Thanks in advance!

// My solution to the Celsius/Fahrenheit convertor exercise

use std::io;

fn main() {
println!("What would you like to convert to?");
println!("Input 'C' to convert Fahrenheit to Celsius or 'F' to convert Celsius to Fahrenheit.");
    let mut convert_to = String::new();

    io::stdin().read_line(&mut convert_to).expect("Failed to read line.");
    let convert_to = convert_to.trim();

    if convert_to == "f" {
        cel_to_fh() // calls the function to convert Celsius to Fahrenheit
    } else if convert_to == "c" {
        fh_to_cel() // calls the function to convert Fahrenheit to Celsius
    } else {
        println!("Command not understood. You can only input 'C' or 'F'");
    }
}


// Function to convert Celsius to Fahrenheit
fn cel_to_fh(){
    println!("Converting Celsius to Fahrenheit. Please input a number");

loop {
let  mut cel = String::new();

io::stdin().read_line(&mut cel)
.expect("That's not a number!");

let cel: i32 = match cel.trim().parse() {
    Ok(cel) => cel,
    Err(_E) => {
        -1
    }
};

// string with formula to convert Celsius to Fahrenheit and assign value to fh 
let fh = cel * 9 / 5 + 32;
println!("In Fahrenheit, That's {}", fh);
    }
}


// Function to convert Fahrenheit to Celsius
fn fh_to_cel(){
    println!("Converting Fahrenheit to Celsius. Please input a number");

loop {
let mut fh = String::new();

io::stdin().read_line(&mut fh)
.expect("That's not a number!");

let fh: i32 = match fh.trim().parse() {
    Ok(fh) => fh,
    Err(_E) => {
        -1
    }
};

// string with formula to convert Fahrenheit to Celsius and assign value to cel 
let cel = fh - 32 * (5 / 9);
println!("In Celsius, That's {}", cel);
    }
}

You're using integers, so 5/9 evaluates to 0, then 32*0 is 0 [1], then fh - 0 is going to give you fh back again.

I agree you should use floating point for this problem. [2] But if we walk through it again with floats, we would get

  • 5.0/9.0 is .555555...
  • Times 32.0 is 17.7777....
  • Then you're always going to get back fh - 17.77777...

Division and multiplication binding tighter than subtraction and addition is the key part here. What you actually meant was

let cel = (fh - 32.0) * 5.0 / 9.0;

  1. multiplication binds tighter than subtraction, so we do this first ↩ī¸Ž

  2. If you change your i32s to f32s and then add decimal points to all your literal numbers, I think that would do it. ↩ī¸Ž

1 Like

Thank you for replying. I assigned fh as f32 and implemented your solution. It now works.
Now when I convert Fahrenheit to Celsius I see 5 fractional numbers, example: "100 Fahrenheit is 37.77778 Celsius." so I guess my next project is to figure out how to trim the solution to only show 1 fraction, 37.7. Thanks a lot for your help though!
Would you recommend I change the function converting celsius to Fahrenheit to also use floating point numbers? Or it is fine like this since the formula works and compiles anyway?
Thanks a ton again!

It's verbose documentation, but here's a reference.

I would, for all of consistency, accuracy, and the ability to round-trip (within the limits of floating point math). [1] Though in the context of a self-driven exercise, I think the ultimate answer is "if you feel you would benefit from it".

Speaking of, if you're interested, the main things I'd suggest adding is a way for the user to quit the program, and maybe a way to go back to the "main menu". [2]


  1. I guess that last one is just consistency in another guise. ↩ī¸Ž

  2. And error handling, but that's not covered until chapter 9. ↩ī¸Ž

1 Like

Thanks for the link and advice. Yeah I was planning to add a quit and return to menu option. Time to get back to coding :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.