Why doesn't this work

It should ask lastName, but the program skip that step and go to the last putStrLn. I don’t understand.

import Data.Char

main = do
putStrLn “What’s your first name?”
firstName <- getLine
putStrLn “What’s your last name?”
lastName <- getLine
let bigFirstName = map toUpper firstName
bigLastName = map toUpper lastName
putStrLn $ "hey " ++ bigFirstName ++ " " ++ bigLastName ++ “, how are you?”

I suspect it might be due to BufferMode. Try modifying your program like this:

import Data.Char
import qualified System.IO as I

main = do
        I.hSetBuffering I.stdout I.NoBuffering
        putStrLn "What's your first name?"
        -- ...

and tell us if it works.

It works for me, if I add the missing "let " before “bigLastName” and convert all the smart-quotes to real ones:

$ cat whatsinaname.hs
import Data.Char

main = do
  putStrLn "What’s your first name?"
  firstName <- getLine
  putStrLn "What’s your last name?"
  lastName <- getLine
  let bigFirstName = map toUpper firstName
  let bigLastName = map toUpper lastName
  putStrLn $ "hey " ++ bigFirstName ++ " " ++ bigLastName ++ ", how are you?"
$ ghc --make whatsinaname.hs 
[1 of 1] Compiling Main             ( whatsinaname.hs, whatsinaname.o )
Linking whatsinaname ...
$ ./whatsinaname 
What’s your first name?
Adam
What’s your last name?
Sjøgren
hey ADAM SJØGREN, how are you?
$ 

(This is on Debian GNU/Linux.)

1 Like

still doesn’t work. Maybe the reason is I’m working online with repl.it, but thanks anyway.

repl.it might have a problem with flushing. If you can, consider downloading an installer for your machine; I promise you will have a better Haskell experience.

1 Like

It seems like repl.it is somehow doubling the number of ‘enter’ keypresses, so every line entered is being followed up by an additional blank line. Here’s a workaround: Each time you use getLine, actually use it twice, and discard the second line.

import Data.Char

getLine2 = do
    x <- getLine
    _ <- getLine
    return x

main = do
    putStrLn "What's your first name?"
    firstName <- getLine2
    putStrLn "What's your last name?"
    lastName <- getLine2
    let bigFirstName = map toUpper firstName
        bigLastName = map toUpper lastName
    putStrLn $ "hey " ++ bigFirstName ++ " " ++ bigLastName ++ ", how are you?"
2 Likes

Thanks, Chris. That’s perfect.

Seems to be a long standing bug on repl.it: https://repl.it/bugs/?search=getline.