Share your repls and programming experiences

← Back to all posts
Useful User Input Functions!
KrazyOrange (25)

Hello!

I've made some useful user input functions that get data from the user. For example, instead of using

let age: Int = Int(readLine()!)

which does work, but the user can input anything, (like a string, which would give you an error), or some crazy number, like -123527. Instead, you can use:

let age = GetNumber(prompt: "How old are you?", low: 1, high: 100)

This does look a little more complicated, but is a lot safer.

The "prompt" is what is displayed in the console to prompt the user for input. "low" is the lowest number you can enter, and "high" is the highest number you can enter.
There are also other useful ones, like getting a boolean or an item from a list. I have also added a simple function for waiting until the user is ready and presses enter.

Instructions

  1. Run the project, and go through the demo, so you get an idea of how they look and work.
  2. Declare that these are great functions, and you will use them.

Now that I want to use them, how do I get them?

All you have to do is go into the project, copy what the comments tell you, and paste them into a file in one of your projects. ( Unless there is some other, easier way to do it. )

Quick note:

The functions have lots of comments to help explain what they do. The GetMenuItem() function is meant to be customized to your liking, so I have put extra comments where you should change it.Also, please give credit when you use them.

An upvote would be nice, I have worked hard on these.