Discussion:
fmt.Scanf(%d%s%d)
Bardia Jedi
2013-02-04 15:09:24 UTC
Permalink
Hi I'm trying to read an basic equation and output result! here my code

package main

import "fmt"

func main (){
fmt.Print("input ekvation: ")
var a, b int
var s rune
fmt.Scanf("%d %5v %d", &a, &s, &b)
fmt.Println (a, string(s), b)
}

the idea is that the user input an equation and the program separtas the
int and the operations and acts accordingly
e.g.

1+2
set 1 to int a
+ is a char (rune) so it stors it as rune

2 is an integer

so throw some logic is output 3
----------

now the problem is that scanf reads evrything after the %s!

anyideas?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Dave Cheney
2013-02-04 20:21:25 UTC
Permalink
fmt.Scanf and friends return an error. What does is say when you run your program.
Post by Bardia Jedi
Hi I'm trying to read an basic equation and output result! here my code
package main
import "fmt"
func main (){
fmt.Print("input ekvation: ")
var a, b int
var s rune
fmt.Scanf("%d %5v %d", &a, &s, &b)
fmt.Println (a, string(s), b)
}
the idea is that the user input an equation and the program separtas the int and the operations and acts accordingly
e.g.
1+2
set 1 to int a
+ is a char (rune) so it stors it as rune
2 is an integer
so throw some logic is output 3
----------
now the problem is that scanf reads evrything after the %s!
anyideas?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Bardia Jedi
2013-02-04 20:28:52 UTC
Permalink
it able to compile and it output
1 +2 0
were +2 is an string
I have try it w/ runes and bytes now when i do that is gives me value 0
witch means that it's not reading anything
Post by Bardia Jedi
Hi I'm trying to read an basic equation and output result! here my code
package main
import "fmt"
func main (){
fmt.Print("input ekvation: ")
var a, b int
var s rune
fmt.Scanf("%d %5v %d", &a, &s, &b)
fmt.Println (a, string(s), b)
}
the idea is that the user input an equation and the program separtas the
int and the operations and acts accordingly
e.g.
1+2
set 1 to int a
+ is a char (rune) so it stors it as rune
2 is an integer
so throw some logic is output 3
----------
now the problem is that scanf reads evrything after the %s!
anyideas?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Kyle Lemons
2013-02-05 21:28:30 UTC
Permalink
Scanf, like its C counterpart, expects space separated tokens.

http://play.golang.org/p/Jmw7JGyu56

For expression parsing you'll probably need your own tokenizer.
Post by Bardia Jedi
Hi I'm trying to read an basic equation and output result! here my code
package main
import "fmt"
func main (){
fmt.Print("input ekvation: ")
var a, b int
var s rune
fmt.Scanf("%d %5v %d", &a, &s, &b)
fmt.Println (a, string(s), b)
}
the idea is that the user input an equation and the program separtas the
int and the operations and acts accordingly
e.g.
1+2
set 1 to int a
+ is a char (rune) so it stors it as rune
2 is an integer
so throw some logic is output 3
----------
now the problem is that scanf reads evrything after the %s!
anyideas?
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Bardia Jedi
2013-02-05 22:44:22 UTC
Permalink
ok that work but Go Playground doesn’t support keyboard input and i want
the user to input a equation, so I started coding and got this:

package main

import "fmt"

func main () {

var a , b int
var op, s string
fmt.Scanln( &s)
fmt.Fscanf(s ,"%d %s %d", &a ,&op ,&b)
fmt.Println(a, b , op)
fmt.Println(s)
}

and here is the problem I'm having
1- fmt.Scanln dose not read the line it only reads till the \n -> this
most be a bug right!?
2- fmt.FscanF doesn't accepts the string 's'
Post by Bardia Jedi
Hi I'm trying to read an basic equation and output result! here my code
package main
import "fmt"
func main (){
fmt.Print("input ekvation: ")
var a, b int
var s rune
fmt.Scanf("%d %5v %d", &a, &s, &b)
fmt.Println (a, string(s), b)
}
the idea is that the user input an equation and the program separtas the
int and the operations and acts accordingly
e.g.
1+2
set 1 to int a
+ is a char (rune) so it stors it as rune
2 is an integer
so throw some logic is output 3
----------
now the problem is that scanf reads evrything after the %s!
anyideas?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Kyle Lemons
2013-02-06 00:40:48 UTC
Permalink
fscanf was just there so that you wouldn't think you had to read it in all
at once and use sscanf. Use Fscanf(os.Stdin or Scanf
Post by Bardia Jedi
ok that work but Go Playground doesn’t support keyboard input and i want
package main
import "fmt"
func main () {
var a , b int
var op, s string
fmt.Scanln( &s)
fmt.Fscanf(s ,"%d %s %d", &a ,&op ,&b)
fmt.Println(a, b , op)
fmt.Println(s)
}
and here is the problem I'm having
1- fmt.Scanln dose not read the line it only reads till the \n -> this
most be a bug right!?
2- fmt.FscanF doesn't accepts the string 's'
Post by Bardia Jedi
Hi I'm trying to read an basic equation and output result! here my code
package main
import "fmt"
func main (){
fmt.Print("input ekvation: ")
var a, b int
var s rune
fmt.Scanf("%d %5v %d", &a, &s, &b)
fmt.Println (a, string(s), b)
}
the idea is that the user input an equation and the program separtas the
int and the operations and acts accordingly
e.g.
1+2
set 1 to int a
+ is a char (rune) so it stors it as rune
2 is an integer
so throw some logic is output 3
----------
now the problem is that scanf reads evrything after the %s!
anyideas?
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
minux
2013-02-06 00:41:35 UTC
Permalink
Post by Bardia Jedi
ok that work but Go Playground doesn’t support keyboard input and i want
package main
import "fmt"
func main () {
var a , b int
var op, s string
fmt.Scanln( &s)
fmt.Fscanf(s ,"%d %s %d", &a ,&op ,&b)
fmt.Println(a, b , op)
fmt.Println(s)
}
and here is the problem I'm having
1- fmt.Scanln dose not read the line it only reads till the \n -> this
most be a bug right!?
what do you mean? fmt.Scanln(&s) should work as documented.
Post by Bardia Jedi
2- fmt.FscanF doesn't accepts the string 's'
use fmt.Sscanf for strings, Fscanf is for file like objects (io.Reader).
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Bardia Jedi
2013-02-07 08:41:03 UTC
Permalink
minux! -> sorry wrong char what I should have said was " "
e.g.
var s string
fmt.Print("input: ")
fmt.Scanln(&s)
fmt.Println(s)
---------------------------
input: *hello world
*hello

do u see am I doing something wrong!

////////////////////////7
kyle!
thak for the help I understand it better now.
so i gess there is on built in command to let the user not worry about
space between integer and operator

/thx Bardia Jedi
Post by minux
Post by Bardia Jedi
ok that work but Go Playground doesn’t support keyboard input and i want
package main
import "fmt"
func main () {
var a , b int
var op, s string
fmt.Scanln( &s)
fmt.Fscanf(s ,"%d %s %d", &a ,&op ,&b)
fmt.Println(a, b , op)
fmt.Println(s)
}
and here is the problem I'm having
1- fmt.Scanln dose not read the line it only reads till the \n -> this
most be a bug right!?
what do you mean? fmt.Scanln(&s) should work as documented.
Post by Bardia Jedi
2- fmt.FscanF doesn't accepts the string 's'
use fmt.Sscanf for strings, Fscanf is for file like objects (io.Reader).
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
minux
2013-02-07 10:12:50 UTC
Permalink
Post by Bardia Jedi
minux! -> sorry wrong char what I should have said was " "
e.g.
var s string
fmt.Print("input: ")
fmt.Scanln(&s)
fmt.Println(s)
---------------------------
input: *hello world
*hello
do u see am I doing something wrong!
please read docs for fmt.Scan and you will understand Scanln's behavior:
http://golang.org/pkg/fmt/#Scan
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...