Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

SWIFT: Make program wait for input

  1. Oct 10, 2016 #1
    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 (Text):
    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: Oct 10, 2016
  2. jcsd
  3. Oct 10, 2016 #2

    Jonathan Scott

    User Avatar
    Gold Member

    What execution environment? In interactive playground environments or similar you may well find that readLine() always assumes end of file.
     
  4. Oct 14, 2016 #3
    It is a playground. Thanks for the help!

    I shifted to a command line tool and everything works fine now!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: SWIFT: Make program wait for input
  1. Make a program (Replies: 2)

Loading...