Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console input giving spurious values

After trying this simple console input with 5, the result is shown as 53

printfn "Enter no. of blocks: "
let nBlock = System.Console.Read()
printfn "entered value is %O" nBlock

Tried it on the interactive, still getting wrong results. Any solutions please?

like image 323
AruniRC Avatar asked May 17 '26 07:05

AruniRC


1 Answers

You should try something like:

printfn "Enter no. of blocks: "
let nBlock = System.Console.ReadLine() |> System.Int32.Parse
printfn "entered value is %d" nBlock

Explanation:

you code only reads one character - as Lee mentioned with this you will read a line (ending after you press return) and parse that string into a int.

Remark: maybe you will want to check for a number, you can do this with TryParse:

printfn "Enter no. of blocks: "
let nBlock = 
   match System.Console.ReadLine() |> System.Int32.TryParse with
   | true, n -> n
   | false, _ -> -1
printfn "entered value is %d" nBlock

Of course you will have to check for the error case (-1) or change it into a option or something.

like image 125
Random Dev Avatar answered May 19 '26 21:05

Random Dev