Initial commit

This commit is contained in:
2022-10-06 13:29:41 -07:00
commit c496d12aa2
5 changed files with 69 additions and 0 deletions

7
bmi-calc/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bmi-calc"
version = "0.1.0"

8
bmi-calc/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "bmi-calc"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

29
bmi-calc/src/main.rs Normal file
View File

@ -0,0 +1,29 @@
use std::io::{self, Write};
fn read_float(prompt: &str) -> f32 {
print!("{}", prompt);
let _ = io::stdout().flush();
let mut line = String::new();
let _ = io::stdin().read_line(&mut line);
let flt = line[..line.len() - 1].parse::<f32>();
if flt.is_err() {
println!("Invalid number: '{}'", line);
return read_float(prompt);
}
return flt.unwrap();
}
fn calculate_bmi(kg_weight: f32, cm_height: f32) -> f32 {
let m_height = cm_height / 100.0;
return kg_weight / (m_height * m_height);
}
fn main() {
let in_height = read_float("Enter height (inches): ");
let lb_weight = read_float("Enter weight (pounds): ");
let cm_height = in_height * 2.54;
let kg_weight = lb_weight / 2.20462;
println!("Your metric height: {}cm", cm_height);
println!("Your metric weight: {}kg", kg_weight);
println!("Your BMI: {}", calculate_bmi(kg_weight, cm_height));
}