I am trying to make a program that reads a number given by a user and then prints it. the number has to be an integer when I print it, but this code gives me a parse error:
main = do
{
putStrLn "Please enter the number"
number <- getLine
putStrLn "The num is:" ++ show (read number:: Int)
}
If you use brackets in your do
statement, you have to use semicolons. Also, the last line should be putStrLn $ "The num is:" ++ show (read number :: Int)
So you have two options:
main = do
{
putStrLn "Please enter the number";
number <- getLine;
putStrLn $ "The num is:" ++ show (read number:: Int)
}
or:
main = do
putStrLn "Please enter the number"
number <- getLine
putStrLn $ "The num is:" ++ show (read number:: Int)
Almost all of the code I've seen uses the second version, but they are both valid. Note that in the second version, whitespace becomes significant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With