SWIFT: Make program wait for input

  • Thread starter Thread starter RoboNerd
  • Start date Start date
  • Tags Tags
    Input Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 5K views
RoboNerd
Messages
410
Reaction score
11
Hi everyone.

I am working on a blackjack game using swift, and I have need to get the user's input from a console. While my code (which is attached below) works in theory, my program does not wait for the user to type some input in the console. Instead, it quickly passes over it and returns an empty string.

If anyone could provide some input that would be great.
My code is shown below:

"""
Code:
func input() -> String
{
    print("Checkpoint 1")
    let input = readLine(stripNewline: true)
    print("Checkpoint 2")
    if (input != nil)
    {
        return input!
    }
    else
    {
        return ""
    }
}

""""

My console output is as following:
"
Curent hand value is: 10. Do you want another card? (yes/no):

Checkpoint 1

Checkpoint 2
"


-----

My program calls the input() function after it asks the user "Do you want another card?" and as one can see by the printing of the checkpoint lines, the console does not stop and wait.

Could anyone please provide some input as to what is going on?

Thanks in advance.
 
Last edited by a moderator:
Physics news on Phys.org
What execution environment? In interactive playground environments or similar you may well find that readLine() always assumes end of file.
 
  • Like
Likes   Reactions: RoboNerd
It is a playground. Thanks for the help!

I shifted to a command line tool and everything works fine now!