diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f2ffb58 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,54 @@ +use std::io; + +fn main() { + let mut unit = String::new(); + let mut temperature = String::new(); + + println!("Would you like to convert from Celcius to Fahrenheit (F) or from Fahrenheit to Celcius (C)? "); + io::stdin() + .read_line(&mut unit) + .expect("Failed to read line"); + + let unit = unit + .trim() + .get(..1) + .unwrap(); + + println!("What temperature are you converting? "); + io::stdin() + .read_line(&mut temperature) + .expect("Failed to read line"); + + + let temperature: f64 = temperature + .trim() + .parse() + .expect("Please type a number!"); + + println!("Converting {temperature} degrees {unit}."); + println!("{temperature} degrees {unit} = {} degrees {}", + if unit == "F" { + to_celcius(temperature) + } else if unit == "C" { + to_fahrenheit(temperature) + } else { 0.0 }, + if unit == "F" { "C" } + else if unit == "C" { "F" } + else {"X"} + ); + + // println!("If the temperature is {temperature} degrees Celcius, then it would be {} degrees Fahrenheit.", to_fahrenheit(temperature)); + +// println!("If the temperature is {temperature} degrees Fahrenheit, then it would be {} degrees Celcius.", to_celcius(temperature)); + +} + +// from Celcius +fn to_fahrenheit (temperature: f64) -> f64 { + (temperature * (9.0/5.0)) + 32.0 +} + +// from Fahrenheit +fn to_celcius (temperature: f64) -> f64 { + (temperature - 32.0) * (5.0/9.0) +}