From c7df2bd546d127bb12491a3b604f434c695a4f28 Mon Sep 17 00:00:00 2001 From: Musselman Date: Thu, 14 Mar 2024 14:47:48 -0500 Subject: [PATCH] Add basic Input validation for if user enters digit or 'q' as a choice --- .gitignore | 5 +++++ Cargo.toml | 8 ++++++++ src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 3ca43ae..193d30e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,8 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3088a6a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "math-tool" +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/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7ff2901 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,51 @@ +/* +Written by: James Musselman +On: 3/14/2024 +Updated: Never +Description: + This tool was writen as part of my 100DaysOfCode commitment for the 2024 year to help establish a + habit of programming daily and fuel my continious drive to learn. + + The tool below should allow the user to run basic, intermediate, and advanced math computations. + The user will select the equation or type of math they want to be done, enter the data, and + then recive the result of the computation. + + Future work could include showing how the answer was calculated, showing graphs, and exporting to + a text file. + +*/ +use std::io; + + +fn main() { + // TODO: Greet and explain tool + + // Get user choice on what they want to use + loop { + let mut user_input = String::new(); + + println!("Please select a function below or use 'q' to quit:"); + + io::stdin().read_line(&mut user_input) + .expect("Failed to read line"); + + if user_input.trim().eq_ignore_ascii_case("q") { + break; + } + + let num: Result = user_input.trim().parse(); + + // Match statement to run choice + match num { + Ok(num) => { + run_function(num); + } + Err(_) => println!("Invalid input. Please try again."), + } + } +} + +fn run_function(num: i32) { +// run choice, then ask again or quit if the user selects that option +println!("{}",num) +} \ No newline at end of file