Console 2: Mr Bargain Tout

This commit is contained in:
thatscringebro 2024-09-26 20:17:10 -04:00
parent 8f8a8346e5
commit f8fb4a4821
2 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[package]
name = "mrbargaintout"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,57 @@
use std::io::{self, Write};
fn main() {
let mut object_name: String = String::new();
let mut posted_price: f32 = 0.0;
let mut paid_price: f32 = 0.0;
print!("Enter the object's name: ");
let _ = io::stdout().flush();
io::stdin()
.read_line(&mut object_name)
.expect("failed to read from stdin");
object_name = object_name.trim().to_string();
let mut postedprice_isint: bool = false;
while !postedprice_isint {
print!("Enter the posted price: ");
_ = io::stdout().flush();
let mut input_text = String::new();
io::stdin()
.read_line(&mut input_text)
.expect("failed to read from stdin");
match input_text.trim().parse::<f32>() {
Ok(i) => {
posted_price = i;
postedprice_isint = true;
}
Err(..) => println!("Input must be an integer!"),
}
}
let mut paidprice_isint: bool = false;
while !paidprice_isint {
print!("Enter the paid price: ");
_ = io::stdout().flush();
let mut input_text = String::new();
io::stdin()
.read_line(&mut input_text)
.expect("failed to read from stdin");
match input_text.trim().parse::<f32>() {
Ok(i) => {
paid_price = i;
paidprice_isint = true;
}
Err(..) => println!("Input must be an integer!"),
}
}
println!("The object's name is: {}", object_name);
println!("The posted price is: {:.2} $", posted_price);
println!("The paid price is: {:.2} $", paid_price);
let diff: f32 = posted_price - paid_price;
println!("Difference between the prices: {:.2} $", diff);
println!("Rebate percentage: {:.2} %", diff / posted_price * 100.0);
}