index : temp_converter
Convert temperature degres
about summary refs log tree commit diff
author Justin Zelikoff <justinzelikoff@gmail.com> 2026-08-02 19:19:59 -0400
committer Justin Zelikoff <justinzelikoff@gmail.com> 2026-08-02 19:19:59 -0400
commit 060a723aae0142b8f6c6d952d5a4d1fccfe66549 [patch]
tree 486d5ff2949a445a20ee45c54c12df4a8fdf0240
parent e24bbbaecfe83e9b756ab294baa2bdd8c1513531
download 060a723aae0142b8f6c6d952d5a4d1fccfe66549.tar.gz

refactor - added helper function for parsing start temp

Diffstat

-rw-r--r-- src/main.rs 35
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index fb91a21..687d7e3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,43 +8,42 @@
        println!("Which way do you need to convert?");
        println!("Fahrenheit ---> Celsius: Press 1");
        println!("Celsius ---> Fahrenheit: Press 2");
        println!("(Press q to quit)");

        let mut direction = String::new();
        let mut start_temp = String::new();

        io::stdin()
            .read_line(&mut direction)
            .expect("Failed to read line.");

        let direction: u32 = direction.trim().parse().expect("Please type a number!");

        let start_temp = read_temp();

        if direction == 1 {
            println!("What is starting temp in Farenheit?");
            io::stdin()
                .read_line(&mut start_temp)
                .expect("Failed to read line.");
            let start_temp: f64 = start_temp.trim().parse().expect("Please type a number!");
            println!("{start_temp}F is equal to {:?}C",farenheit_to_celsius(start_temp));
            println!("{start_temp}F is equal to {}C",farenheit_to_celsius(start_temp));
        } else if direction == 2 {
            println!("What is starting temp in Celsius?");
            io::stdin()
                .read_line(&mut start_temp)
                .expect("Failed to read line.");
            let start_temp: f64 = start_temp.trim().parse().expect("Please type a number!");
            println!("{start_temp}C is equal to {:?}F", celsius_to_farenheit(start_temp));
            println!("{start_temp}C is equal to {}F", celsius_to_farenheit(start_temp));
        } else {
            println!("Please choose 1 or 2!");
            continue;
        }
    }
}

fn read_temp() -> f64 {
    println!("What is starting temperature?");
    let mut temp = String::new();
    io::stdin()
        .read_line(&mut temp)
        .expect("Failed to read line.");
    let parsed_temp: f64 = temp.trim().parse().expect("Please type a number!");
    parsed_temp
}

fn farenheit_to_celsius(temp_to_convert: f64) -> f64 {
    let converted_temp: f64 = (temp_to_convert - 32.0) / 1.8;
    converted_temp
    (temp_to_convert - 32.0) / 1.8
}

fn celsius_to_farenheit(temp_to_convert: f64) -> f64 {
    let converted_temp: f64 = (temp_to_convert * 1.8) + 32.0;
    converted_temp
    (temp_to_convert * 1.8) + 32.0
}