diff --git a/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/Cargo.toml b/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/Cargo.toml index 5266e2a..287ce86 100644 --- a/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/Cargo.toml +++ b/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/Cargo.toml @@ -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] diff --git a/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/src/main.rs b/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/src/main.rs index 543888d..ba3a954 100644 --- a/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/src/main.rs +++ b/Week-01/Day-01_Convert-Ages-To-Days/convert_ages_to_days/src/main.rs @@ -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); }