Skip to content

Instantly share code, notes, and snippets.

@Najaf
Created June 4, 2016 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Najaf/60ddc39c41befd442ca633bfc73ae966 to your computer and use it in GitHub Desktop.
Save Najaf/60ddc39c41befd442ca633bfc73ae966 to your computer and use it in GitHub Desktop.
use std::io;
use std::io::Write;
use std::str::FromStr;
fn prompt<F>(question: &str) -> Result<F, F::Err>
where F: FromStr
{
let mut answer = String::new();
print!("{} ", question);
let _ = io::stdout().flush();
let _ = io::stdin().read_line(&mut answer);
answer.trim().to_string().parse::<F>()
}
fn main() {
let people: u8 = prompt("How many people?").unwrap();
let pizzas: u8 = prompt("How many pizzas?").unwrap();
let slices_per_pizza = 8;
let total_slices = pizzas * slices_per_pizza;
let slices_per_person = total_slices / people;
let leftovers = total_slices % people;
println!("{} people with {} pizzas", people, pizzas);
println!("Each person gets {} slices of pizza", slices_per_person);
println!("There are {} leftover pieces", leftovers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment