What function do we use in Julia in place of cin>> in C++? how to input number in Julia from keyboard? Can I create vector from keyboard in Julia?

function F(a=parse(Float64,readline()))
@show a
end

F()

You already found readline, which answers your first question.

It returns a string, which you have to process.

Look at ?parse and ?tryparse for parsing numbers.

To parse a vector, the best strategy is to split it to components (you could use Julia’s code parser, but that allows arbitrary code, so it is not recommended). Use split or regular expressions.

1 Like

Why not? It’s not like it actually evaluates the code.

julia> function parsevector(T, str)
           expr = Meta.parse(str)
           if expr.head == :vect
             Vector{T}(expr.args)
           else
             error("could not parse")
           end
         end

julia> parsevector(Int, "[1, 2, 3]")
3-element Array{Int64,1}:
 1
 2
 3
3 Likes

The way you did it is OK I think (because nothing else should be able to get through); I was thinking of converting with eval.

2 Likes