functions, loops and control flow

This commit is contained in:
2025-12-20 18:13:04 +00:00
parent 83fc3600b8
commit 04c6e7e66a
12 changed files with 300 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
use std::io;
fn main() {
println!("welcome to tempverter!");
println!("please enter your temp:");
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("error storing user input");
let input: i32 = input
.trim()
.parse()
.expect("error storing temperature - did you enter a number?");
println!("DEBUG: you entered {input}");
println!("is this in C or F?");
let mut input_unit = String::new();
io::stdin()
.read_line(&mut input_unit)
.expect("error storing user input");
let input_unit: char = input_unit
.trim()
.parse()
//NOTE: it won't panic when you enter a number
//because char is an unsigned 32-bit integer
//so it will akshually panic when you enter a negative number
.expect("error storing temperature unit - did you enter a char?");
if input_unit == 'c' || input_unit == 'C' {
println!("{input}C is {}F", converter(input, 1));
} else if input_unit == 'f' || input_unit == 'F' {
println!("{input}F is {}C", converter(input, 2));
}
}
fn converter(input: i32, choice: i32) -> i32 {
if choice == 1 {
return 32 + ((9 * input) / 5);
} else {
return ((input - 32) / 9) * 5;
}
}