Print_string not flushing?

I’m trying to collect user input by use of the following function:

let promptString ?(msg=None) s =
  let prompt = match msg with
    | Some msg -> msg
    | None     -> String.capitalize_ascii s
  in
  print_string @@ prompt ^ ": ";
  flush stdout;
  let value = read_line () in
  Val (s, String value)

However, when running the program, the prompt never gets printed before attempting to read a line. I call this function 3 times, and it accepts 3 line inputs before finally flushing the prompt text. Any idea how to fix this?

On Ubuntu Linux with ocaml 4.10.0, I ran your code (with minor change to remove “Val”):

# promptString ~msg:(Some "f") "a" ;;
f: zzzz;;
- : string * string = ("a", "zzzz;;")
# 

Perhaps you have an environmental problem?

Maybe try this:

Printf.print "%s\n%!" your_string