#Typing 5 for input but returning Failed why?

63 messages ยท Page 1 of 1 (latest)

split wren
sharp zinc
# split wren

add fmt.Printf("%q\n", answer) in CheckAnswer and see what is the output

#

Also the global here makes little sense, I guess you are begining that fine, just don't go think this is an acceptable usage of global variables ๐Ÿ˜„

#

(this is bad because if there are multiple pieces of code using this, they will colide on the global which is really confusing, you should lookup return)

split wren
#

so should i check answer inAskQuestion?

sharp zinc
#

that would work

#

if you don't know about it, lookup return (really ๐Ÿ˜‰)

split wren
split wren
#

isn't that?

sharp zinc
# split wren

I don't see fmt.Printf("%q\n", answer) anywhere here

split wren
sharp zinc
split wren
#

am i getting wrong something?

#

it should be "5"

sharp zinc
split wren
#

i have no idea

#

i started today ๐Ÿ˜„

sharp zinc
#

Yeah np

#

so you look at your code, you have ReadString('\n')

#

you can also google "golang trailing \r\n" ๐Ÿ™‚

split wren
#

uh should i remove it when i get input everytime?

sharp zinc
#

||(this represent the ENTER key in your terminal)||

#

you can use strings.TrimSpace

split wren
#

ups i got "" ๐Ÿ˜„

sharp zinc
#

if you remove all whitespace from \r\n you indeed get nothing

#

what are you trying to do ? ๐Ÿ™‚

split wren
#

get just "5"

#

๐Ÿ˜„

sharp zinc
split wren
#

nah nah

#

i mean just input

#

without \r\n

sharp zinc
#

hum hum ^^ ?

#

it's the input that you want to cleanup, not \r\n

split wren
#

yes

#

i suck

sharp zinc
split wren
#

sheesh

split wren
sharp zinc
#

@split wren to recap.
ReadString('\n') is inclusive, it will give you all data until it find the thing you pass it in, right now you read until \n which is a magic character representing a new line.

5\r\n is really the key sequence 5 + ENTER (you have an extra \r too because you are on windows)
So what you did is passed your string into strings.TrimSpace which will remove all the whitespaces left and right of your data.
This includes the ENTER key.
Which leaves you with just 5 (or whatever was written in).

split wren
#

I totally understand

#

thank you so much

#

Also do i have anyway for convert string to int?

sharp zinc
#

@split wren you can also lookup fmt.Scan and fmt.Scanf

split wren
#

uh okay

#

also

#

i want to check it in CheckAnswer()

#

it is looking like that

#

now, the output of the fmt.Printf("%q\n", value) is "5\r\n"

#

all good

#

i typed that value = strings.TrimSpace("\r\n")