yasindce1998
yasindce1998

Reputation: 35

How to solve "value: ParseIntError" in rust?

I am trying to build a Fahrenheit to Celsius converter in Rust.

I compiled it successfully, but I don't know what went wrong in at runtime. Is this because of the conversion?

Here is my code:

use std::io;

fn main(){
    println!("Please select\n 1.CELSIUS to FAHRENHEIT\n 2.FAHRENHEIT to CELSIUS");

    const CEL_FAH: f64 = 32.00;
    const FAH_CEL: f64 = -17.22;

    let mut select = String::new();

    io::stdin().read_line(&mut select)
    .expect("Please select the appropriate options");

    let select: i32 = select.parse().unwrap();

    //control flow

    if select == 1 {
        println!("1. CELSIUS - FAHRENHEIT: ");

        let cels = String::new();

        let cels: f64 = cels.parse().unwrap();

        let ans1 = cels * CEL_FAH;

        println!("Conversions: {}", ans1);
    } else if select == 2 {

        println!("2. FAHRENHEIT - CELSIUS: ");

        let fahs = String::new();

        let fahs: f64 = fahs.parse().unwrap();

        let ans2 = fahs * FAH_CEL;

        println! ("Conversions: {}", ans2);
    }else {

        println!("Select the options please.");
    }


}

Here is my output and error:

   Compiling converter v0.1.0 (D:\Program Files\Rust Projects\converter)
    Finished dev [unoptimized + debuginfo] target(s) in 2.46s
     Running `target\debug\converter.exe`
Please select
 1.CELSIUS to FAHRENHEIT
 2.FAHRENHEIT to CELSIUS
2
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }', src\main.rs:19:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\converter.exe` (exit code: 101)```

Upvotes: 0

Views: 3126

Answers (2)

Jawwad Turabi
Jawwad Turabi

Reputation: 342

In your code, there are three mistakes:

  1. When you're taking input using stdin() method and want to parse it into another type you have to put .trim() because stdin() adds whitespaces with your input so you need to trim those unwanted whitespaces.
  2. Each time you are taking input you have to use io::stdin() method along with the read_line method so that the compiler hold for the user input.
  3. You have used the wrong formula for conversion.

I have made the corrections in your code and it is working fine now, here is the snippet:

use std::io;

fn main() {
    println!("Please select\n 1.CELSIUS to FAHRENHEIT\n 2.FAHRENHEIT to CELSIUS");

    const CEL_FAH: f64 = 32.00;
    const FAH_CEL: f64 = 1.8; //changed

    let mut select = String::new();

    io::stdin()
        .read_line(&mut select)
        .expect("Please select the appropriate options");

    let select: i32 = select.trim().parse().unwrap(); // .trim() method requires when taking input

    //control flow

    if select == 1 {
        println!("1. CELSIUS - FAHRENHEIT: ");

        let mut cels = String::new();
        io::stdin()
            .read_line(&mut cels)
            .expect("Please input a temperature in degrees"); // when you're taking input from user you have to use stdin()

        let cels: f64 = cels.trim().parse().unwrap();

        let ans1 = (cels * FAH_CEL) + CEL_FAH; //this the correct formula to convert from celsius to fahrenheit

        println!("Conversions: {}", ans1);
    } else if select == 2 {
        println!("2. FAHRENHEIT - CELSIUS: ");

        let mut fahs = String::new();
        io::stdin()
            .read_line(&mut fahs)
            .expect("Please input a temperature in degrees"); //when you're taking input from user you have to use stdin()

        let fahs: f64 = fahs.trim().parse().unwrap();

        let ans2 = (fahs - CEL_FAH) * 5. / 9.; //this the correct formula to convert from fahrenheit to celsius

        println!("Conversions: {}", ans2);
    } else {
        println!("Select the options please.");
    }
}

You can also refer to this repository. Temperature converter rust

Upvotes: 1

weary
weary

Reputation: 135

Your read_line also yielded a newline (the enter you pressed when selecting something). You first have to remove that from the input, for example like this:

select.truncate(select.len()-1);

Also, you have to specify the target-type that the string must be parsed to:

let select = select.parse::<i32>().unwrap();

(the rest of your snippet seems unfinished, so not responding to errors down there)

Upvotes: 0

Related Questions