Wrote program for Day 1

This commit is contained in:
Mariano Riefolo 2024-07-26 16:03:54 +02:00
parent 06b1e0657c
commit 4f0b2678c2
2 changed files with 16 additions and 23 deletions

View File

@ -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]

View File

@ -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 {
let mut buffer = String::new();
io::stdin()
.read_line(&mut buffer)
.expect("Error while trying to read from stdin.");
println!("Please input your age in years!");
let mut guess = String::new();
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 age: u16 = buffer.trim().parse()
.expect("The input is not an integer between 0 and 65535");
let days = calc_age(age);
println!("Your age is about {} days", days);
}