100-days-of-rust/Week-07/Day-43_Area-Of-A-Triangle/day43/src/main.rs

29 lines
785 B
Rust
Raw Normal View History

2024-09-06 07:54:25 +00:00
use std::{
io::{self, Write},
num::ParseFloatError,
};
use day43::tri_area;
fn read_f64(request: &str) -> Result<f64, ParseFloatError> {
let mut input = String::new();
print!("{}", request);
io::stdout().flush().expect("Failed to flush stdout");
std::io::stdin()
.read_line(&mut input)
.expect("Failed to read input");
input.trim().parse()
}
fn main() {
let base = read_f64("Enter the base of the triangle: ");
let height = read_f64("Enter the height of the triangle: ");
match base {
Ok(base) => match height {
Ok(height) => println!("The area of the triangle is: {}", tri_area(base, height)),
Err(e) => println!("Error: {}", e),
},
Err(e) => println!("Error: {}", e),
}
}