complete temperature converter
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Welcome to the temperature converter program!");
|
println!("Welcome to the temperature converter program!");
|
||||||
|
|
||||||
@@ -12,16 +14,61 @@ fn main() {
|
|||||||
|
|
||||||
// then the input should be sanitized and validated / parsed int.
|
// then the input should be sanitized and validated / parsed int.
|
||||||
|
|
||||||
|
loop {
|
||||||
|
|
||||||
|
println!("Type 1 for converting Celcius to Fahrenheit");
|
||||||
|
println!("Type 2 for converting Fahrenheit to Celcius");
|
||||||
|
|
||||||
|
let mut temp_type = String::new();
|
||||||
|
let mut degrees = String::new();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut temp_type)
|
||||||
|
.expect("failed to read line");
|
||||||
|
|
||||||
|
let temp_type: u32 = match temp_type.trim().parse() {
|
||||||
|
Ok(num) if num == 1 || num == 2 => num,
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Please type 1 or 2");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
println!("Please type 1 or 2");
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// TODO USER INPUT 2
|
// TODO USER INPUT 2
|
||||||
// next up, we need to get a new user input, as float. This must also be sanitized and parsed.
|
// next up, we need to get a new user input, as float. This must also be sanitized and parsed.
|
||||||
// Here we can perhaps come into some difficulties, what if the user inputs 22? not 22.0? also,
|
// Here we can perhaps come into some difficulties, what if the user inputs 22? not 22.0? also,
|
||||||
// we need to let the user input again, if the input is invalid, like text or whatever.
|
// we need to let the user input again, if the input is invalid, like text or whatever.
|
||||||
|
println!("Please enter the temperature: ");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut degrees)
|
||||||
|
.expect("failed to read line");
|
||||||
|
|
||||||
|
let degrees: f64 = match degrees.trim().parse() {
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
|
||||||
// TODO CALCULATOR
|
// TODO CALCULATOR
|
||||||
// The control flow starts here. if user input 1 = 1 then convert from c to f. if user input =
|
// The control flow starts here. if user input 1 = 1 then convert from c to f. if user input =
|
||||||
// 2, then f to c. We could have a else to catch something unexpected. Say something like, an
|
// 2, then f to c. We could have a else to catch something unexpected. Say something like, an
|
||||||
// error occured when trying to convert temperature, please try again...
|
// error occured when trying to convert temperature, please try again...
|
||||||
|
if temp_type == 1 {
|
||||||
|
let result = (degrees * 9.0/5.0) + 32.0;
|
||||||
|
println!("{degrees} celcius is {result:.2} fahrenheit");
|
||||||
|
} else if temp_type == 2 {
|
||||||
|
let result = (degrees - 32.0) * 5.0/9.0;
|
||||||
|
println!("{degrees} fahrenheit is {result:.2} celsius");
|
||||||
|
} else {
|
||||||
|
println!("something went wrong, please try again");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// PRINT RESULT AND LOOP
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user