Converting temperature help please

I'm new to coding and cant figure out why I'm getting the error for both my (&mut temp) lines 32 and 54 of 82. any assistance would be appreciated.

The problem im seeing says," mismatched types rustc(E0308) expected mutable reference &mut String' found mutable reference &mut i32

that's the issue for both locations

 use std::io;



fn main() {

    let mut temp = String::new();

    let temp: i32 = temp.trim().parse().expect("msg");

    let mut ans = String::new();

    let fvar = 5/9;
 
    let fah = temp + 32;

    let fah = temp * fvar;
    

    println!("would you like to conver from Celsius to Fahrenheit?");

    io::stdin()
        .read_line(&mut ans)
        .expect("input yes or no");

    
    if ans.trim() == "yes" {

        println!("Input a temperature");

        io::stdin()
            .read_line(&mut temp)// having an issue here
            .expect("input a number");

        println!("{temp} converted to Fahrenheit is {fah}");



    } else if ans.trim() == "no" {

        println!("would you like to convert from fahrenheit to Celsius?");

        io::stdin()
            .read_line(&mut ans)
            .expect("input yes or no");



        if ans.trim() == "yes" {

            println!("Input a temperature");

            io::stdin()
                .read_line(&mut temp)//having an issue here
                .expect("msg");
            
                let cel = temp - 32;
                                            
                let cel = cel / fvar;
            

            println!("{temp} converted to Celsius is {cel}");



        } else {
            
            println!("Im not sure why you are here then")
        }


        
    } else {
        
        println!(" I didnt understand what you typed");

        println!(" maybe try again when you have had less to drink")


    }
   
}

You should read the documentation for functions you are using, but the compiler also tells you exactly what the issue is. Reading from stdin results in a string (since one can type in arbitrary characters, not just numbers). So you can't read into a number directly.

The error message

error[E0308]: mismatched types
  --> src/main.rs:24:24
   |
24 |             .read_line(&mut temp) // having an issue here
   |                        ^^^^^^^^^ expected struct `String`, found `i32`
   |
   = note: expected mutable reference `&mut String`
              found mutable reference `&mut i32`

Has a little more detail. Notice the part where it points at the variable. And there are lines that say what was expected and what was found.

On line 4 you create a String called temp. Then on line 6 you create a i32 called temp. Down when you read the file you try to put it into "temp". But "temp" is not a String anymore because line 6 made "temp" meaning the i32.
That is called "shadowing" the variable name.

Also there are quite a few other things in your code that your might want to be aware of.

If you get it to compile it will panic at this line.

    let temp: i32 = temp.trim().parse().expect("msg");

This line

    let fvar = 5 / 9;

will put 0 into fvar because if you 9 goes into 5 zero times. If you want a fraction you need to use floating point.

    let fvar = 5.0 / 9.0;

and that leads to this

    let fah = temp + 32;

    let fah = temp * fvar;

You have "fah" and it is an integer i32, you can not multiply integer and float. The "fvar" will be now be a float because of "5.0/9.0"

Another pointer. If you are very fresh with rust and looks like you have maybe written c before, I suggest just adding just a couple of lines of code and get it to compile. Then add more.
Comment out everything after line 6.
Read the panic message. The remove line 6 because I think you just have it there to play with the panic and "expect"

The part with the math.

    let fah = temp + 32;

    let fah = temp * fvar;

is broken in a couple of ways.
Your math should be after you read the numbers from input. Put that code after the read_line code.
Also broken because you set fah to "temp + 32" on one line and the next line you reset fah to "temp * fvar".

That is all for now. Good luck

2 Likes

thanks for your detailed response. I took your advice and made the changes you recommended. im still having trouble with those two lines however.

I added two lines of code above and below the read lines

if ans.trim() == "yes" {

            println!("Input a temperature");

            let temp:String = temp.to_string();//added this line

            io::stdin()
                .read_line(&mut temp)
                .expect("msg");

                let temp:f64 = temp.trim().parse().expect("msg");//added this line
            
                let cel = temp - 32.0;
                                            
                let cel = cel / fvar;
            

            println!("{temp} converted to Celsius is {cel}");

and it seems to have fixed that issue. however when i try to do the same thing when reading the initial file at line 32 not only does it not work. it actually creates mor problems.

any advise on another way to approach this?

You should change tempt to be an empty string before calling read_line.

let mut temp = String::new();
io::stdin()
    .read_line(&mut temp)
    .expect("msg");

let temp: f64 = temp.trim().parse().expect("failed to parse");
1 Like

thanks for the reply. I took all the advice and started over keeping what you and everyone else said in mind. I finally got it to work thanks to you and everyone's guidance. thank you!

this is what i came up with

use std::io;


fn main() {

    println!("Would you like to convert from Fahrenheit to Celsius?");

    let mut ans = String::new();

    io::stdin()
        .read_line(&mut ans)
        .expect("msg");

    if ans.trim() == "yes" {

        println!("Please enter a number ex. 30.0");

        let mut val = String::new();

        io::stdin()
            .read_line(&mut val)
            .expect("msg");

        let val:f64 = val.trim().parse().expect("msg");

        let fah = (val - 32.0) * (5.0 / 9.0);

        println!("{val} degrees Fahrenheit converts to {fah} degrees Celsius");

    } else if ans.trim() == "no" {

        println!("Would you like to convert from Celsius to Fahrenheit?");

        let mut second_ans = String::new();

        io::stdin()
            .read_line(&mut second_ans)
            .expect("msg");

        if second_ans.trim() == "yes" {

            println!("Please enter a number ex. 30.0");

            let mut val = String::new();

            io::stdin()
                .read_line(&mut val)
                .expect("msg");

            let val:f64 = val.trim().parse().expect("msg");

            let car = (val * 1.8) + 32.0;

            println!("{} degrees Celsius converts to {} degrees Fahrenheit", val, car);

            
        } else if second_ans.trim() == "no" {

            println!("why are we even talking then")

        }else {
            
            println!(" Wow someone seems a little tipsy");

            println!(" why dont we try this tomorrow when your sober");
        }
    }else {

        println!(" Wow someone seems a little tipsy");

        println!(" why dont we try this tomorrow when your sober");
        
    }
        
}

If there is a way that anyone thinks i could have improved the code i welcome the advice. Thanks again

1 Like