So I am trying to do something simple:
printfn "Enter a number:"
try
let x = System.Console.ReadLine();
Some(int32(x))
with
| :? System.FormatException -> printfn "Invalid number!"
Some(0)
I want to print the message, then get the user to input a number, and try to convert it to an int and return it.
If I just compile the code (by typing fsc a3.fs on the command line), it works fine. It pauses, waits for input, then returns Some(int).
If I copy and paste the code into the FSI on the command line, it works great.
But when I am in visual studio, and I run the code in the FSI (highlight + alt+enter), it just goes right over the input part and the exception is thrown (and caught).
Here is the output when I run in the FSI (in visual studio):
Enter a number:
Invalid number!
0
As you can see, It doesnt ever actually pause and wait for me to enter input.
Anyone know how to make this work?
Thanks!
The F# Interactive console in Visual Studio does not support reading input, so there is no way to ask for an input from the console. If you’re running code interactively, you can always enter the input in the editor, so the best workaround is to have
letbinding at the beginning where you enter the input before running your code. You can use#ifto support both scenarios: