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:
parent
47d305243a
commit
41f0d4a1f8
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue