I downloaded and executed utop as guided here, and I ran the following code:
Scanf.scanf "%d %d" (fun a b -> Printf.printf "%d\n" (a - b));;
On the first time I input 3 1, it worked fine, giving 2 - : unit = (),
but after the second try with the same input, it keeps on giving the message:
Exception:
Stdlib.Scanf.Scan_failure
"scanf: bad input at char number 3: character '\\n' is not a decimal digit".
Scanfconsumes as little input as possible.If you evaluate
and send to the standard input
Scanfreads and consumes the3 1prefix and leaves the newline character\nin the input buffer. Then the next call towill be stuck on this remaining character and fail with
In this situation, you can consume this
\ncharacter with eitheror
However, a better solution is probably to add a newline to your input format: