Reputation: 1760
import Control.Monad
import Data.Char
main = forever $ do
putStr "Give me some input: "
l <- getLine
putStrLn $ map toUpper l
I have been learning Haskell from learn you haskell. When i try to run this code it does not behave like it should.
l
Give me some input: L
abc
Give me some input: ABC
When i run this it doesn't print the string first and i can input. After providing some input, in this case l
it returns me like this : Give me some input: L
.
What it should do is, ask for input by printing Give me some input:
and after putting the input it shall return the input in uppercase.
How can i fix this?
Upvotes: 1
Views: 166
Reputation: 34378
As Michail points out, it is a buffering issue, which can be solved with hSetBuffering
for stdout
:
import Control.Monad
import Data.Char
import System.IO
main = do
hSetBuffering stdout NoBuffering
forever $ do
putStr "Give me some input: "
l <- getLine
putStrLn $ map toUpper l
If for whatever reason you don't want to change the buffer mode for the whole program, you can use hFlush
to do an extra flushing of the buffer exactly where you need it:
import Control.Monad
import Data.Char
import System.IO
main = do
forever $ do
putStr "Give me some input: "
hFlush stdout
l <- getLine
putStrLn $ map toUpper l
Upvotes: 4
Reputation: 21690
Try this:
import Control.Monad
import Data.Char
main = forever $ do
putStrLn "Give me some input: "
l <- getLine
putStrLn $ map toUpper l
The issue here is that putStr
doesn't write a newline. And the output buffer only gets flushed on a newline.
Upvotes: 1