Add basic Input validation for if user enters digit or 'q' as a choice

This commit is contained in:
James Musselman 2024-03-14 14:47:48 -05:00
parent 61edfcdeb9
commit c7df2bd546
Signed by: Musselman
GPG key ID: 1DAEFF35ECB5D6DB
3 changed files with 64 additions and 0 deletions

5
.gitignore vendored
View file

@ -14,3 +14,8 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Added by cargo
/target

8
Cargo.toml Normal file
View file

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

51
src/main.rs Normal file
View file

@ -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<i32, _> = 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)
}