index : temp_converter
Convert temperature degres
about summary refs log tree commit diff
author Justin Zelikoff <justinzelikoff@gmail.com> 2026-08-01 15:50:14 -0400
committer Justin Zelikoff <justinzelikoff@gmail.com> 2026-08-01 15:50:14 -0400
commit e24bbbaecfe83e9b756ab294baa2bdd8c1513531 [patch]
tree 5d340973828b490d139312ecb9b93fdad9d81d4f
parent d92ac96bf2ae95c556cdaf6e21c9a7ab05881fe8
download e24bbbaecfe83e9b756ab294baa2bdd8c1513531.tar.gz

refactor: created functions for temp conversions

Diffstat

-rw-r--r-- src/main.rs 74
 1 file changed, 43 insertions(+), 31 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index d31aff6..fb91a21 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,36 +3,48 @@
fn main() {
    println!("Welcome to the temperature converter!");

    println!("Which way do you need to convert?");

    println!("Fahrenheit ---> Celsius: Press 1");

    println!("Celsius ---> Fahrenheit: Press 2");

    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!");
    println!("You chose {direction}");

    println!("What is the starting temperature?");

    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!("You entered {start_temp}");

    if direction == 1 {
        let converted_temp: f64 = (start_temp - 32.0) / 1.8;
        println!("{start_temp}F is {converted_temp}C");
    } else if direction == 2 {
        let converted_temp: f64 = (start_temp * 1.8) + 32.0;
        println!("{start_temp}C is {converted_temp}F");
    loop {

        println!("Which way do you need to convert?");
        println!("Fahrenheit ---> Celsius: Press 1");
        println!("Celsius ---> Fahrenheit: Press 2");

        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!");

        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));
        } 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));
        } else {
            println!("Please choose 1 or 2!");
            continue;
        }
    }
}

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

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