Alexandr Krivosheev
Alexandr Krivosheev

Reputation: 73

Scanf value is executed as command in terminal

I have simple go program that converts miles to kilometers:

const kmInMile = 1.609344

func main() {
    var miles float64

    fmt.Print("Enter miles: ")
    fmt.Scanf("%f", &miles)

    fmt.Println(miles)

    km := kmInMile * miles

    fmt.Println(miles, "miles =", km, "km")
}

If I pass "lls" as input to scanf:

Enter miles: lls

Output is:

0
0 miles = 0 km
alexandrkrivosheev$ ls
hello   main.go

so the first char of input was taken and all other were executed as command. Why does it happened and how can i prevent this?

Full terminal session:

alexandrkrivosheev$ ./hello 
Enter miles: lls
0
0 miles = 0 km
alexandrkrivosheev$ ls
hello   main.go
alexandrkrivosheev$ 

Upvotes: 1

Views: 64

Answers (1)

ain
ain

Reputation: 22759

When using "plain fmt.Scanf" the input must match the expected format, ie in your case it must be valid float. If it isn't then the scanning is aborted and rest of the input remains in console's input buffer where it is executed as next command after your program exits.

To fix this you wrap the stdin into an bufio.Reader or bufio.Scanner:

func main() {
    var miles float64

    fmt.Print("Enter miles: ")
    //
    reader := bufio.NewReader(os.Stdin)
    val, err := reader.ReadString('\n')
    if err != nil {
        fmt.Println(err)
        return
    }
    if _, err = fmt.Sscanf(val, "%f", &miles); err != nil {
        fmt.Println(val, err)
        return
    }

    fmt.Println(miles)

    km := kmInMile * miles

    fmt.Println(miles, "miles =", km, "km")
}

This way you consume whole line from input and process it separately.

Upvotes: 4

Related Questions