hey – great coders and haskellers,
i’m a haskell freshman and have a problem with a program
it boils down to the following situaition
main :: IO ()
main = do
putStrLn "\nplease give me some input"
input1 <- getLine
putStrLn "\nplease give me another input"
input2 <-getLine
putStrLn ("\nyour inputs were "++show(input1)++" and "++ show(input2)")
putStrLn "restart ?? yY or nN"
c <- getChar
restart c
where
restart c
|elem c "yY" = do
main
|elem c "nN" = putStrLn "\nExample Over"
|otherwise = do
putStrLn "\nyou must type one of Yy to confirm or nN to abort"
c'<- getChar
restart c'
on any but the first execution of main
input1 <- getLine
is skipped and i can find no reason for it, as the following
input2 <- getLine
is executed as expected, i’m open for any suggestions and help
thanks in advance ε/2
The fix: set
NoBufferingat the start of your program:Why does this fix the issue? Look at what you’re typing when you don’t using NoBuffering! You type, and
getLineconsumes:Then you type, and
getLine#2 consumes:Then you type:
But
getCharonly consumed theyand leaves the[enter]buffered, which your firstgetLinecall reads! Why did you type[enter]? Because you had to, just hitting ‘y’ didn’t causemainto loop because the terminal was line buffered.