Created
April 10, 2018 10:15
-
-
Save newton-migosi/8910ec93ea6d2ca2224e1b7111d94c94 to your computer and use it in GitHub Desktop.
Converting between Fahrenheit and Celsius using Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
println!("Hello there user,"); | |
loop{ | |
println!("Which units will you be converting from F or C? (type q to quit):"); | |
let mut unit = String::new(); | |
io::stdin().read_line(&mut unit) | |
.expect("Could not read input."); | |
//println!("You chose {}", unit); | |
if unit.trim() == String::from("q"){ | |
break; | |
}; | |
println!("Enter the number of degrees you want to convert from in numbers:"); | |
let mut val = String::new(); | |
io::stdin().read_line(&mut val) | |
.expect("Could not read input."); | |
let val_int: isize = match val.trim().parse() { | |
Ok(num) => num, | |
Err(_) => continue | |
}; | |
let f = String::from("F"); | |
let c = String::from("C"); | |
let output = if unit.trim() == f { | |
f_to_c(val_int) | |
}else if unit.trim() == c{ | |
c_to_f(val_int) | |
} else { | |
5 | |
}; | |
println!("{:?}", output); | |
} | |
println!("See you later!") | |
} | |
fn f_to_c(deg_val: isize) -> isize { | |
let converted = deg_val - 32; | |
let converted = converted * 5; | |
let converted = converted / 9; | |
converted | |
} | |
fn c_to_f(deg_val: isize) -> isize{ | |
let converted = deg_val * 9; | |
let converted = converted / 5; | |
let converted = converted + 32; | |
converted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment