fmt.Scanf(%d%s%d)

2,138 views
Skip to first unread message

Bardia Jedi

unread,
Feb 4, 2013, 10:09:24 AM2/4/13
to golan...@googlegroups.com
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?
  

Dave Cheney

unread,
Feb 4, 2013, 3:21:25 PM2/4/13
to Bardia Jedi, golan...@googlegroups.com
fmt.Scanf and friends return an error. What does is say when you run your program. 
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Bardia Jedi

unread,
Feb 4, 2013, 3:28:52 PM2/4/13
to golan...@googlegroups.com
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

Kyle Lemons

unread,
Feb 5, 2013, 4:28:30 PM2/5/13
to Bardia Jedi, golang-nuts
Scanf, like its C counterpart, expects space separated tokens.


For expression parsing you'll probably need your own tokenizer.


  

--

Bardia Jedi

unread,
Feb 5, 2013, 5:44:22 PM2/5/13
to golan...@googlegroups.com
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'


 

On Monday, February 4, 2013 4:09:24 PM UTC+1, Bardia Jedi wrote:

Kyle Lemons

unread,
Feb 5, 2013, 7:40:48 PM2/5/13
to Bardia Jedi, golang-nuts
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


--

minux

unread,
Feb 5, 2013, 7:41:35 PM2/5/13
to Bardia Jedi, golan...@googlegroups.com
On Wed, Feb 6, 2013 at 6:44 AM, Bardia Jedi <bardi...@gmail.com> wrote:
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!?
what do you mean? fmt.Scanln(&s) should work as documented.
2- fmt.FscanF doesn't accepts the string 's'
use fmt.Sscanf for strings, Fscanf is for file like objects (io.Reader).

Bardia Jedi

unread,
Feb 7, 2013, 3:41:03 AM2/7/13
to golan...@googlegroups.com, 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!

////////////////////////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 

minux

unread,
Feb 7, 2013, 5:12:50 AM2/7/13
to Bardia Jedi, golan...@googlegroups.com

On Thursday, February 7, 2013, Bardia Jedi wrote:
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
Reply all
Reply to author
Forward
0 new messages