I am trying to make a program that takes a Float number inputted by the user via keyboard and does stuff with it.
However every time I try to parse the inputted String into a Float I keep getting errors. Every single method I've tried has failed to allow me to take user inputted data and turn it into a Float, which is what I need.
My practice program (not the actual problem I'm trying to solve) is:
main = do
putStrLn "Please input a number."
inputjar <- getLine
read :: read a => String -> a
putStrLn( read inputjar :: Int)
Edit
A further question.
How do I take the inputted string and turn it into something I can use in a calculation?
For example, how do I take the inputted string so that I can do something like:
(var + var) / 2
You say
(read inputjar :: Int)
in your code, but you're telling us that you want to read aFloat
. You are not definingread
, take that line out. You useputStrLn
so that you can print out the float, butputStrLn
takes aString
, so you need toshow
the value.This is in a way nicer since it immediately fixes what we're reading the string as:
but it requires
{-#LANGUAGE ScopedTypeVariables#-}
putStrLn
prints outString
s, notInt
s, so there are two solutions:Use
print
, which prints anything that implementsShow
:.. or call
putStrLn
on the originalString
that you read inIn the latter example n is never used, but presumably you would write some code after that which would actually use it.
Well I guess the
is there just by mistake?
Anyway, under ghci,
:t putStrLn
shows:So
putStrLn
only accepts String as its first parameter, so if you really want to:BTW,
read inputjar :: int
is correct, only your ouput is wrong.This worked for me