1 file changed, 17 insertions(+), 18 deletions(-)
@@ -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
}