5

I seem to be having a queer problem while getting user input within a for loop in go. Here is my code

package main import "fmt" func main() { var num int for i := 0; i < 10; i++ { fmt.Printf("Debug: i : %d ", i) fmt.Scanf("%d", &num) fmt.Println(num) } } 

What happens when I run this code is this :

Debug: i : 0 Enter next number 1 1 Debug: i : 1 Enter next number 1 Debug: i : 2 Enter next number 2 2 Debug: i : 3 Enter next number 2 Debug: i : 4 Enter next number 3 3 Debug: i : 5 Enter next number 3 Debug: i : 6 Enter next number 4 4 Debug: i : 7 Enter next number 4 Debug: i : 8 Enter next number 5 5 Debug: i : 9 Enter next number 5 

What I notice is that each iteration of the loop happens twice, Could this be because Go is using parallelism by default and causing both processors to run the code within a for loop?

6
  • 2
    As far as I can see, each iteration is done only once, Debug: i: n where n goes from 0 to 9 suggests exactly that. Or do you mean something else? Here is my session in the terminal: gist.github.com/4357860 It works exactly as expected. Commented Dec 22, 2012 at 7:13
  • 2
    How come your output doesn't match your code? Commented Dec 22, 2012 at 7:36
  • @VladimirMatveev If you try to run it on your machine, i think youll see what I mean . Do you see the scanf in the loop? It should read a value from the keyboard 10 times. But for every input I give in, the loop block executes twice. So i increments twice. Commented Dec 22, 2012 at 8:20
  • @jdi Im confused about that too Commented Dec 22, 2012 at 8:26
  • 2
    @gprasant, the link I gave holds the copy of terminal session on my computer with your program. It went exactly as your code suggests: print "Debug: i: $n " line -> awaiting number -> I enter the number -> it is printed -> repeat again. However, the output you have given cannot be produced by you program in principle; it can be produced by the program that peterSO has written in his answer. Commented Dec 22, 2012 at 15:16

4 Answers 4

6

What OS are you using? Windows?

Try this:

package main import "fmt" func main() { var num int for i := 0; i < 10; i++ { fmt.Printf("Debug: i : %d\n", i) fmt.Println("Enter next number") n, err := fmt.Scanf("%d\n", &num) if err != nil { fmt.Println(n, err) } fmt.Println(num) } } 

Output:

Debug: i : 0 Enter next number 1 1 Debug: i : 1 Enter next number 2 2 Debug: i : 2 Enter next number 3 3 Debug: i : 3 Enter next number 4 4 Debug: i : 4 Enter next number 5 5 Debug: i : 5 Enter next number 6 6 Debug: i : 6 Enter next number 7 7 Debug: i : 7 Enter next number 8 8 Debug: i : 8 Enter next number 9 9 Debug: i : 9 Enter next number 10 10 
Sign up to request clarification or add additional context in comments.

2 Comments

Im using windows. I tried your suggestion. Im still having the same problem
Im sorry, I midded out the '\n' in fmt.Scanf("%d\n", &num) . I tried the scanfwith \n and it worked
2

The above answer is a good suggestion. the code

 if err != nil { fmt.Println(n, err) } 

will output the reason of this problem.

 10 unexpected newline 

So I change the code to this, and it works.

package main import "fmt" func main() { var num int for i := 0; i < 10; i++ { fmt.Printf("Debug: i : %d ", i) fmt.Scanf("%d\n", &num) // add "\n" fmt.Println(num) } } 

this is because of the different line endings. the windows uses carriage return and line feed(\r\n) as a line ending. the Unix uses the line feed(\n).

you can use notepad2 to create a file (a.txt) with \r line feed. and do this:

 go run s.go < input.txt 

this will work correctly.

2 Comments

it was a problem with the absense '\n' in the scanf. Is this a windows line ending problem? because I ran the same code above on a friend's mac and it worked perfectly
this is because of the different line endings. the windows uses carriage return and line feed('\r\n') as a line ending. the Unix uses the line feed('\n').
1

Just to point out fmt.Scanln(&num) would probably work the same as fmt.Scanf("%d\n",&num), since fmt.Scanln(&num) also check the type of "num".

In other words, if

var num float32 fmt.Scanln(&num) 

you can input floating number from the console.

Comments

0

I have the same problem, i resolved adding "\n" in a scanf format string: fmt.Scanf("%d\n", &num).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.