Wrote program for Day 1
This commit is contained in:
parent
06b1e0657c
commit
4f0b2678c2
@ -3,6 +3,4 @@ name = "convert_ages_to_days"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
@ -1,27 +1,22 @@
|
||||
use std::io;
|
||||
use std::io::{self, Write};
|
||||
|
||||
fn calc_age(age: u16) -> u16 {
|
||||
const DAYS_IN_YEAR: u16 = 365;
|
||||
return age * DAYS_IN_YEAR;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
print!("Write your age: ");
|
||||
io::stdout().flush().expect("Failed to flush stdout.");
|
||||
|
||||
loop {
|
||||
|
||||
println!("Please input your age in years!");
|
||||
|
||||
let mut guess = String::new();
|
||||
let mut buffer = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut buffer)
|
||||
.expect("Error while trying to read from stdin.");
|
||||
|
||||
let age: u16 = buffer.trim().parse()
|
||||
.expect("The input is not an integer between 0 and 65535");
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut guess)
|
||||
.expect("Could not read age. Make you an integer is used.");
|
||||
|
||||
let guess: u32 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
let age: u32 = guess * 365;
|
||||
|
||||
println!("You are roughly {age} days old!")
|
||||
|
||||
}
|
||||
|
||||
let days = calc_age(age);
|
||||
println!("Your age is about {} days", days);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user