Hi !,
Basically, in the following code, we print a line first and the read the line from stdin. This behavior works perfectly fine when using “runhaskell guess.hs”. But when i say “./guess”, i had to type in a number directly ( i.e. first prompt is skipped ).
btw.. i am using mac yosomite command terminal.
Any help greatly appreciated.. Thanks a lot in advance..
import System.Random
import Control.Monad(when)
main = do
ranGen <- getStdGen
let (rand,_) = randomR (1,10) ranGen :: (Int,StdGen)
putStr "Guess a number between 1 and 10 : "
numStr <- getLine
when(not $ null numStr) $ do
let num = read numStr
if rand == num
then putStrLn "Yuhaa!! you are right baby!"
else putStrLn $ "Sorry dude!! it was " ++ show rand
newStdGen
main