SWIFT: Make program wait for input

In summary, the conversation discusses an issue with getting user input from a console in a blackjack game using Swift. The code provided works in theory but the program does not wait for the user to input anything. The user asks for input and provides console output, but the program does not stop and wait for the user to respond. It is suggested to use a command line tool for proper execution.
  • #1
RoboNerd
410
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:
Technology news on Phys.org
  • #2
What execution environment? In interactive playground environments or similar you may well find that readLine() always assumes end of file.
 
  • Like
Likes RoboNerd
  • #3
It is a playground. Thanks for the help!

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

Related to SWIFT: Make program wait for input

1. How do I make a program in SWIFT wait for user input?

To make a program in SWIFT wait for user input, you can use the readLine() function. This function will prompt the user to input a value and store it in a variable, which can then be used in your program.

2. Can I specify a specific data type for the user input in SWIFT?

Yes, you can specify a specific data type for the user input in SWIFT by using the readLine() function in combination with another function such as Int() or Double(). This will convert the user input into the specified data type.

3. How do I handle unexpected user input in SWIFT?

In SWIFT, you can use control flow statements such as if and guard to handle unexpected user input. These statements allow you to check the user input and perform specific actions based on the input.

4. Is it possible to make a program wait for a certain amount of time before accepting user input in SWIFT?

Yes, you can use the sleep() function in SWIFT to make a program wait for a certain amount of time before accepting user input. This function will pause the program execution for the specified amount of time.

5. How can I make my program continue running after accepting user input in SWIFT?

You can use the while loop in SWIFT to make your program continue running after accepting user input. This loop will allow your program to repeat a certain section of code until a specified condition is met, giving the user the opportunity to input multiple values.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
14
Views
31K
Replies
17
Views
8K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top