PechaScripts/temperature/src/main.rs

45 lines
1.5 KiB
Rust
Executable File

use std::io;
fn main() {
println!("Welcome to Pecha's Temperature Converter! 0u0!");
loop {
let mut scale = String::new();
let mut temperature = String::new();
let mut o_scale = String::new();
println!("Please enter a scale to convert from- Farenheit (F) or Celsius (C)");
io::stdin().read_line(&mut scale)
.expect("ALERTA ALERTA ANTIFASCISTA");
scale = scale.trim().to_string();
if scale != "F" && scale != "C" {
println!("No recognised scale, please try again");
continue;
}
o_scale = o_scale.trim().to_string();
if scale == "F" {o_scale = "C".to_string();}
if scale == "C" {o_scale = "F".to_string();}
println!("Please enter a temperature");
io::stdin().read_line(&mut temperature)
.expect("ALERTA ALERTA ANTIFASCISTA");
let mut temperature: f32 = match temperature.trim().parse() {
Ok(x) => x,
Err(_) => {
println!("Error: not recognised as a number, please try again.");
continue;
}
};
// [°C] = ([°F] - 32) x 5/9
//[°F] = [°C] x 9/5 + 32
if scale == "F" {
temperature = (temperature - 32.0)*5.0/9.0;
} else {
temperature = (temperature*9.0/5.0)+32.0;
}
println!("Your new temperature is {} degrees °{}",temperature,o_scale)
}
}