From 41f0d4a1f8486542b02caa3fe0ce2d55f201da6f Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 24 Jan 2024 06:56:48 +0100 Subject: [PATCH] Added ability to input names with spaces I finally figured out how to use the bufio library but I also had to get acquainted with another library for strings to trim bufio's appended newlines --- golangr/between-1-10-checker/compare.go | 2 ++ golangr/between-1-10-checker/nameget.go | 20 ++++++++++++++------ golangr/between-1-10-checker/startup.go | 9 +++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/golangr/between-1-10-checker/compare.go b/golangr/between-1-10-checker/compare.go index 24ffa3b..971e200 100644 --- a/golangr/between-1-10-checker/compare.go +++ b/golangr/between-1-10-checker/compare.go @@ -2,6 +2,8 @@ package main import "fmt" +// TODO: Figure out error handling and prevent people from breaking this + func compare(number float64) { if number >= 1 && number <= 10 { fmt.Printf("%v is between 1 and 10\n", number) diff --git a/golangr/between-1-10-checker/nameget.go b/golangr/between-1-10-checker/nameget.go index 0c2e223..6656530 100644 --- a/golangr/between-1-10-checker/nameget.go +++ b/golangr/between-1-10-checker/nameget.go @@ -1,20 +1,28 @@ package main import ( + "bufio" "fmt" - "log" + //"log" + "os" ) func nameget() { - fmt.Println("Welcome to this simple program, may I know your name?\n(Please don't use spaces, I still haven't figured out how to parse them all together into a string)") + fmt.Println("Welcome to this simple program, may I know your name?\n(You can use spaces now, I figured out how to use the bufio library)") fmt.Printf("Name: ") + reader := bufio.NewReader(os.Stdin) + name, _ = reader.ReadString('\n') + + // Hold that thought, I'm pretty sure bufio might be infinitely better, scan keeps bothering me by not + // accepting spaced out words + // It has come to my attention that scan actually spits out two values, default and err - _, err := fmt.Scanln(&name) + // _, err := fmt.Scanln(&name) // nil is literally the equivalent of null, we're just checking here if err is not null, // and if it isn't, it means an error has ocurred so we're just gonna panic and kill the program - if err != nil { - log.Fatal(err) - } + // if err != nil { + // log.Fatal(err) + // } } diff --git a/golangr/between-1-10-checker/startup.go b/golangr/between-1-10-checker/startup.go index 444887b..00bd0fd 100644 --- a/golangr/between-1-10-checker/startup.go +++ b/golangr/between-1-10-checker/startup.go @@ -1,9 +1,14 @@ package main -import "fmt" +import ( + "fmt" + "strings" +) func startup(number float64) { - fmt.Printf("Alright %s, give me a number, I will tell you if it's between 1 and 10\n", name) + // Oh man I gotta use another library, strings lets me trim pretty easily any string and since bufio + // seems to append newlines to everything I'm gonna have to make use of this + fmt.Printf("Alright %s, give me a number, I will tell you if it's between 1 and 10\n", strings.TrimRight(name, "\n")) fmt.Printf("Number: ") fmt.Scan(&num) return