FAQ
The code below prints two prompts every time if the number isn't 5:
(Windows XP, 32bit)

package main

import "fmt"

func main() {
var i int
for {
fmt.Print("Please enter a number: ")
fmt.Scanf("%d", &i)
  if i == 5 {
fmt.Println("You win! ")
break;
} else if i > 5 {
fmt.Println("Too large! Try again.")
} else {
fmt.Println("Too small! Try again. ")
}
}
}

But it's OK when I use fmt.Scanf("%d\n", &i). Is it a bug?

--
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@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • David Symonds at Jul 3, 2013 at 1:28 am
    You aren't checking for an error returned by fmt.Scanf.

    --
    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@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • peterGo at Jul 3, 2013 at 4:49 am
    Yes, it's a bug.

    Issue 5391 <http://code.google.com/p/go/issues/detail?id=5391>: fmt: Scanf
    rejects \r\n at end of line on Windows
    http://code.google.com/p/go/issues/detail?id=5391

    Peter
    On Tuesday, July 2, 2013 1:56:43 AM UTC-4, angan...@gmail.com wrote:

    The code below prints two prompts every time if the number isn't 5:
    (Windows XP, 32bit)

    package main

    import "fmt"

    func main() {
    var i int
    for {
    fmt.Print("Please enter a number: ")
    fmt.Scanf("%d", &i)
    if i == 5 {
    fmt.Println("You win! ")
    break;
    } else if i > 5 {
    fmt.Println("Too large! Try again.")
    } else {
    fmt.Println("Too small! Try again. ")
    }
    }
    }

    But it's OK when I use fmt.Scanf("%d\n", &i). Is it a bug?
    --
    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@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 3, '13 at 1:27a
activeJul 3, '13 at 4:49a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase