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
This commit is contained in:
raul 2024-01-24 06:56:48 +01:00
parent 47d305243a
commit 41f0d4a1f8
3 changed files with 23 additions and 8 deletions

View File

@ -2,6 +2,8 @@ package main
import "fmt" import "fmt"
// TODO: Figure out error handling and prevent people from breaking this
func compare(number float64) { func compare(number float64) {
if number >= 1 && number <= 10 { if number >= 1 && number <= 10 {
fmt.Printf("%v is between 1 and 10\n", number) fmt.Printf("%v is between 1 and 10\n", number)

View File

@ -1,20 +1,28 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"log" //"log"
"os"
) )
func nameget() { 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: ") 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 // 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, // 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 // and if it isn't, it means an error has ocurred so we're just gonna panic and kill the program
if err != nil { // if err != nil {
log.Fatal(err) // log.Fatal(err)
} // }
} }

View File

@ -1,9 +1,14 @@
package main package main
import "fmt" import (
"fmt"
"strings"
)
func startup(number float64) { 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.Printf("Number: ")
fmt.Scan(&num) fmt.Scan(&num)
return return